473,508 Members | 2,344 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(&options, 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 10752
"ibwhoib" <ib*****@ibwhoib.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.programmer.misc, comp.unix.programmer, or
comp.os.linux.development.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.programmer.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.x86, embedded system processor questions may
be appropriate in comp.arch.embedded.

* 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.programming 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++.moderated.

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

news.groups.questions 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*****@ibwhoib.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*****@ibwhoib.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
2482
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...
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:...
1
3753
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....
1
1934
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...
1
3113
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...
0
1546
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. ...
6
6630
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...
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...
2
3004
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...
0
7135
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...
0
7342
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,...
0
7410
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...
1
7067
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4729
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...
0
3215
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...
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
440
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...

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.