467,146 Members | 1,293 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,146 developers. It's quick & easy.

Reading data from serial port on Linux using a C program

Hi,
I am using a C program to write/read from a serial port. The writing
part is working perfectly fine. However, I am not able to read the
values correctly and display them. To debug this issue I am also seeing
the values on minicom. Now I have used fcntl() function and then read
refp = fcntl(fd, F_SETFL, 0);
res = read(fd,buf,1024); /* Read the value sent on the serial port
buf[res]=0; /* set end of string, so we can printf */
printf(":%s:%d\n", buf, res);

On minicom I see about more than 256 characters. I need to store all of
them as a string and then process that string. However when I run the
program, the printf statement only displays 8 or 16 characters.
Is there a fix for this issue??
BTW I have used both Canonical and non-canonical processing without any
evident results.
Thanks,
Vivek

Dec 5 '06 #1
  • viewed: 15290
Share:
8 Replies
In article <11**********************@j72g2000cwa.googlegroups .com>,
Vivek Menon <vi***********@gmail.comwrote:
>I am using a C program to write/read from a serial port. The writing
part is working perfectly fine. However, I am not able to read the
values correctly and display them. To debug this issue I am also seeing
the values on minicom. Now I have used fcntl() function and then read
refp = fcntl(fd, F_SETFL, 0);
res = read(fd,buf,1024); /* Read the value sent on the serial port
buf[res]=0; /* set end of string, so we can printf */
printf(":%s:%d\n", buf, res);
Sorry, fcntl() and read() are not part of the C language itself.
For information about their workings, you need to consult an
information source that is more specific to your platform, such as
comp.unix.programming

>On minicom I see about more than 256 characters. I need to store all of
them as a string and then process that string. However when I run the
program, the printf statement only displays 8 or 16 characters.
Is there a fix for this issue??
[Off Topic]
In POSIX systems, it is legal and common for read() of a tty
to only return a relatively small number of characters. To get all
of the characters, you need to loop until you detect that you have
read enough or that you have reached end of file.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Dec 5 '06 #2

Vivek Menon wrote:
Hi,
I am using a C program to write/read from a serial port. The writing
part is working perfectly fine. However, I am not able to read the
values correctly and display them. To debug this issue I am also seeing
the values on minicom. Now I have used fcntl() function and then read
refp = fcntl(fd, F_SETFL, 0);
res = read(fd,buf,1024); /* Read the value sent on the serial port
buf[res]=0; /* set end of string, so we can printf */
printf(":%s:%d\n", buf, res);

On minicom I see about more than 256 characters. I need to store all of
them as a string and then process that string. However when I run the
program, the printf statement only displays 8 or 16 characters.
Is there a fix for this issue??
BTW I have used both Canonical and non-canonical processing without any
evident results.
Thanks,
Vivek
Try putting the call to "read" inside of a loop. For example:

int num, offset = 0, bytes_expected = 256;
do {
num = read(fd, buf + offset, 1024);
offset += num;
} while (offset < bytes_expected);
>From the Linux man pages:
"It is not an error if this number is smaller than the number of bytes
requested; this may happen for example because fewer bytes are actually
available right now (maybe because we were close to end-of-file, or
because we are reading from a pipe, or from a terminal), or because
read() was interrupted by a signal. "

Of course if you aren't always going to be receiving 256 bytes, then
the sending app will have to tell the receiving app how many bytes to
expect (most likely sent as a header through the serial interface!).

Dec 5 '06 #3
"Vivek Menon" <vi***********@gmail.comwrites:
I am using a C program to write/read from a serial port.
[...]

Standard C has no support for serial ports. You'll need to ask in a
system-specific newsgroup, perhaps comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 5 '06 #4
Thanks for the insight.
I used the loop you mentioned and the program hangs because the number
of characters output changes every time.
Instead I used:
==================
while (STOP==FALSE) {
res = read(fd,buf,255);
buf[res]=0; /* set end of string, so we can
printf */
printf(":%s:%d\n", buf, res);
if (buf[0]=='\0') STOP=TRUE;
}
===========
My program output:
:000020822186228A:16
:35D736DB37DF38E4:16
:3BF03CF43DF83EFC:16
:20822186:8
:36DB37DF:8
:3CF43DF8:8
:D7000000:8
and then this hangs too...

My minicom output:
9E83AEC3BF03CF43DF83EFC4000772221BE9B0000000000A74 5001141E10000020822186228A238E249235D736DB37DF38E4 39E83AEC3BF03CF43DF83EFC400077282808890000B3
====
I have checked for eof, '\0' and '\n' on the terminal(minicom) and all
these conditions do not seem to work. The number of characters change,
so I cannot use a fixed count value.
Any suggestions
Thanks,
Vivek

Insik Park wrote:
Vivek Menon wrote:
Hi,
I am using a C program to write/read from a serial port. The writing
part is working perfectly fine. However, I am not able to read the
values correctly and display them. To debug this issue I am also seeing
the values on minicom. Now I have used fcntl() function and then read
refp = fcntl(fd, F_SETFL, 0);
res = read(fd,buf,1024); /* Read the value sent on the serial port
buf[res]=0; /* set end of string, so we can printf */
printf(":%s:%d\n", buf, res);

On minicom I see about more than 256 characters. I need to store all of
them as a string and then process that string. However when I run the
program, the printf statement only displays 8 or 16 characters.
Is there a fix for this issue??
BTW I have used both Canonical and non-canonical processing without any
evident results.
Thanks,
Vivek

Try putting the call to "read" inside of a loop. For example:

int num, offset = 0, bytes_expected = 256;
do {
num = read(fd, buf + offset, 1024);
offset += num;
} while (offset < bytes_expected);
From the Linux man pages:
"It is not an error if this number is smaller than the number of bytes
requested; this may happen for example because fewer bytes are actually
available right now (maybe because we were close to end-of-file, or
because we are reading from a pipe, or from a terminal), or because
read() was interrupted by a signal. "

Of course if you aren't always going to be receiving 256 bytes, then
the sending app will have to tell the receiving app how many bytes to
expect (most likely sent as a header through the serial interface!).
Dec 5 '06 #5
Keith Thompson wrote:
"Vivek Menon" <vi***********@gmail.comwrites:
>I am using a C program to write/read from a serial port.
[...]

Standard C has no support for serial ports. You'll need to ask
in a system-specific newsgroup, perhaps comp.unix.programmer.
However they are often mapped into a FILE* stream. Again, this is
system dependent.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Dec 6 '06 #6
Thanks for your suggestion, but the '/r' condition makes the program as
usual.
Any other suggestions??
I tried checking eof condition when res =0. That does not work too..
I am now trying to write these values directly to another file and see
what happens. No success so far.
Vivek

Insik Park wrote:
Vivek Menon wrote:
Hi,
I am using a C program to write/read from a serial port. The writing
part is working perfectly fine. However, I am not able to read the
values correctly and display them. To debug this issue I am also seeing
the values on minicom. Now I have used fcntl() function and then read
refp = fcntl(fd, F_SETFL, 0);
res = read(fd,buf,1024); /* Read the value sent on the serial port
buf[res]=0; /* set end of string, so we can printf */
printf(":%s:%d\n", buf, res);

On minicom I see about more than 256 characters. I need to store all of
them as a string and then process that string. However when I run the
program, the printf statement only displays 8 or 16 characters.
Is there a fix for this issue??
BTW I have used both Canonical and non-canonical processing without any
evident results.
Thanks,
Vivek

Try putting the call to "read" inside of a loop. For example:

int num, offset = 0, bytes_expected = 256;
do {
num = read(fd, buf + offset, 1024);
offset += num;
} while (offset < bytes_expected);
From the Linux man pages:
"It is not an error if this number is smaller than the number of bytes
requested; this may happen for example because fewer bytes are actually
available right now (maybe because we were close to end-of-file, or
because we are reading from a pipe, or from a terminal), or because
read() was interrupted by a signal. "

Of course if you aren't always going to be receiving 256 bytes, then
the sending app will have to tell the receiving app how many bytes to
expect (most likely sent as a header through the serial interface!).
Dec 6 '06 #7
"Vivek Menon" <vi***********@gmail.comwrites:
Thanks for your suggestion, but the '/r' condition makes the program as
usual.
Any other suggestions??
I tried checking eof condition when res =0. That does not work too..
I am now trying to write these values directly to another file and see
what happens. No success so far.
[snip]

As has already been mentioned, this question (about reading from a
serial port) is off-topic for comp.lang.c. (Yes, I know you're doing
it in C, but you're using extensions not defined by the language
standard.) You'll probably find that comp.unix.programmer is full of
helpful people eager to answer all the Unix-specific questions you can
come up with.

But you're more likely to get help there if you avoid top-posting:

http://www.caliburn.nl/topposting.html
http://www.cpax.org.uk/prg/writings/topposting.php

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 6 '06 #8


On 5 δΕΛ., 21:53, "Vivek Menon" <vivek.meno...@gmail.comwrote:
Hi,
I am using a C program to write/read from a serial port. The writing
part is working perfectly fine. However, I am not able to read the
values correctly and display them. To debug this issue I am also seeing
the values on minicom. Now I have used fcntl() function and then read
refp = fcntl(fd, F_SETFL, 0);
res = read(fd,buf,1024); /* Read the value sent on the serial port
buf[res]=0; /* set end of string, so we can printf */
printf(":%s:%d\n", buf, res);

On minicom I see about more than 256 characters. I need to store all of
them as a string and then process that string. However when I run the
program, the printf statement only displays 8 or 16 characters.
Is there a fix for this issue??
BTW I have used both Canonical and non-canonical processing without any
evident results.
Thanks,
Vivek
http://magegame.ru/?rf=626f6764616e

Dec 6 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by ^CeFoS^ | last post: by
13 posts views Thread by Rob | last post: by
16 posts views Thread by bloggsfred00@hotmail.com | last post: by
4 posts views Thread by rowan@sylvester-bradley.org | last post: by
3 posts views Thread by naveen.sabapathy@gmail.com | last post: by
9 posts views Thread by Hal Vaughan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.