473,563 Members | 2,703 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linux Serial Programming

Hey all,

I caught a job where I have to write a linux daemon that communicates with a
device connected to a serial port. Here are some details.

I connect to the device in the standard fashion:

fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);

I set the port options:

options.c_cflag = B9600 | CS8 | CREAD | CLOCAL | HUPCL;
cfsetospeed(&op tions, B9600);
tcsetattr(fd, TCSANOW, &options);

I now want to write some data to the port. The documentation on the device
has the following information:
"The command structure consist of one start byte, one command byte, five
bytes of data, and one byte checksum"
So I need a string of bytes. I created this string by using a unsigned
character array and setting the values to a command. Like so:

char cmd[7];
cmd[0] = 42;
cmd[1] = 0;
cmd[2] = 0;
cmd[3] = 132;
cmd[4] = 177;
cmd[5] = 0;
cmd[6] = 119;
cmd[7] = 188;

I then try to write the command to the port.

int written;
written = write(fd, cmd, 7);

My first question is is this the correct way to send this command?
Unfortunately, the device doesn't seem to think it is and returns 3 bytes
that are undocumented. I would appreciate any help. If you need any further
information, please let me know.

Thanks,
ib


Nov 14 '05 #1
3 10769
"ibwhoib" <ib*****@ibwhoi b.com> writes:
I caught a job where I have to write a linux daemon that communicates with a
device connected to a serial port. Here are some details.


Your question is outside the domain of comp.lang.c, which discusses
only the standard C programming language, including the standard C
library. This is a remarkably narrow topic compared to what many
people expect.

For your convenience, the list below contains topics that are not
on-topic for comp.lang.c, and suggests newsgroups for you to explore
if you have questions about these topics. Please do observe proper
netiquette before posting to any of these newsgroups. In particular,
you should read the group's charter and FAQ, if any (FAQs are
available from www.faqs.org and other sources). If those fail to
answer your question then you should browse through at least two weeks
of recent articles to make sure that your question has not already
been answered.

* OS-specific questions, such as how to clear the screen,
access the network, list the files in a directory, or read
"piped" output from a subprocess. These questions should be
directed to OS-specific newsgroups, such as
comp.os.ms-windows.program mer.misc, comp.unix.progr ammer, or
comp.os.linux.d evelopment.apps .

* Compiler-specific questions, such as installation issues and
locations of header files. Ask about these in
compiler-specific newsgroups, such as gnu.gcc.help or
comp.os.ms-windows.program mer.misc. Questions about writing
compilers are appropriate in comp.compilers.

* Processor-specific questions, such as questions about
assembly and machine code. x86 questions are appropriate in
comp.lang.asm.x 86, embedded system processor questions may
be appropriate in comp.arch.embed ded.

* ABI-specific questions, such as how to interface assembly
code to C. These questions are both processor- and
OS-specific and should typically be asked in OS-specific
newsgroups.

* Algorithms, except questions about C implementations of
algorithms. "How do I implement algorithm X in C?" is not a
question about a C implementation of an algorithm, it is a
request for source code. Newsgroups comp.programmin g and
comp.theory may be appropriate.

* Making C interoperate with other languages. C has no
facilities for such interoperation. These questions should
be directed to system- or compiler-specific newsgroups. C++
has features for interoperating with C, so consider
comp.lang.c++ for such questions.

* The C standard, as opposed to standard C. Questions about
the C standard are best asked in comp.std.c.

* C++. Please do not post or cross-post questions about C++
to comp.lang.c. Ask C++ questions in C++ newsgroups, such
as comp.lang.c++ or comp.lang.c++.m oderated.

* Test posts. Please test in a newsgroup meant for testing,
such as alt.test.

news.groups.que stions is a good place to ask about the appropriate
newsgroup for a given topic.

--
"We put [the best] Assembler programmers in a little glass case in the hallway
near the Exit sign. The sign on the case says, `In case of optimization
problem, break glass.' Meanwhile, the problem solvers are busy doing their
work in languages most appropriate to the job at hand." --Richard Riehle
Nov 14 '05 #2
On Sun, 18 Apr 2004 00:52:40 GMT, "ibwhoib" <ib*****@ibwhoi b.com>
wrote:
I now want to write some data to the port. The documentation on the device
has the following information:
"The command structure consist of one start byte, one command byte, five
bytes of data, and one byte checksum"
That's 8 bytes.
So I need a string of bytes. I created this string by using a unsigned
character array and setting the values to a command. Like so:

char cmd[7];
unsigned char cmd[8];
cmd[0] = 42;
cmd[1] = 0;
cmd[2] = 0;
cmd[3] = 132;
cmd[4] = 177;
cmd[5] = 0;
cmd[6] = 119;
cmd[7] = 188;

I then try to write the command to the port.

int written;
written = write(fd, cmd, 7);


written = write(fd, cmd, 8);
or
written = write(fd, cmd, sizeof cmd);

Nick.
Nov 14 '05 #3
In article <sj************ *********@news4 .srv.hcvlny.cv. net>
ibwhoib <ib*****@ibwhoi b.com> writes:
fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY);
Calls to open() and ioctl() (or tcsetattr(), which in turn is
usually just an ioctl()) and write() are off-topic in comp.lang.c,
in part because the details about which options you should use
(like O_NOCTTY and/or O_NDELAY) change from one system to another.
This, however, is on topic:
So I need a string of bytes. I created this string by using a unsigned
character array and setting the values to a command. Like so:

char cmd[7];
cmd[0] = 42;
cmd[1] = 0;
cmd[2] = 0;
cmd[3] = 132;
cmd[4] = 177;
cmd[5] = 0;
cmd[6] = 119;
cmd[7] = 188;


An array of size 7 has 7 total elements, in this case named cmd[0]
through cmd[6] inclusive. You are putting eight values (go ahead,
count them, there are 8) into a seven-value-size bucket. This
causes undefined behavior, i.e., the program may behave unpredictably
(or at least in a manner that, to predict, requires details that
one would not normally even want to think about).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
2487
by: R.Marquez | last post by:
I hope I don't bore you with this personal experience. But, I hope the details are helpful for other Python and/or Linux newbies, or for those thinking about becoming such. I have been using Python on Windows for a few years now. One of the things that attracted me to Python was the fact that it offered the possibility of learning to...
2
634
by: ibwhoib | last post by:
Hey all, I caught a job where I have to write a linux daemon that communicates with a device connected to a serial port. Here are some details. I connect to the device in the standard fashion: fd = open(device, O_RDWR | O_NOCTTY | O_NDELAY); I set the port options:
1
3758
by: rkhimura | last post by:
Hi I am doing a serial programming on C++ to open read and save as .txt . i check out the web, and they are using classes(which i nv learn) but i alway have problem with their source and header file. Is there any website tat gives the correct source and header file?
1
1939
by: creativeinspiration | last post by:
Hey Everybody, Could anybody suggest a good linux kernel programming book? The best kind would be like one those where you can learn a lot fast (like those 24 hour series books). Please let me know if you know any good ones. Thanks
1
3135
by: fidel | last post by:
Hello, my target is a small application which: * Start as a small Window * Offers several input fields for RS232-related connection informations (like parity etc...) * A Send-function to submit local files via the opened RS232 connection to the target.
0
1552
by: mvjohn100 | last post by:
Hello friends, I am newbie to the world of linux network programming... I be thankful if got a information how can I start it. Is there any framework for networking programming in linux. Thanks
6
6637
by: terry | last post by:
Hi, I am trying to send a character to '/dev/ttyS0' and expect the same character and upon receipt I want to send another character. I tired with Pyserial but in vain. Test Set up: 1. Send '%' to serial port and make sure it reached the serial port. 2. Once confirmed, send another character.
0
263
by: Concepts Systems | last post by:
Hello All, Advance C and Linux System Programming are an intensive hands-on course designed by Concepts Systems to provide a detailed examination of each topic. These modules enable professionals and students to rapidly identify issues critical to their project, and provide them in-depth knowledge to add Linux support to their product...
2
3008
by: wizche | last post by:
Hi, I'm currently trying to develop a IR Remote (TV consumer) listener in C under Linux. First of all I tried with an old notebook with an integrated IR peripheral (FIR)under Debian (kernel 2.6). All seems to work fine, I can also access directly with #cat /dev/ttyS1 And my listener (written in C) work pretty fine. Problems came up with...
0
7580
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7882
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6244
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5481
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5208
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3634
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1194
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
916
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.