473,240 Members | 1,448 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,240 software developers and data experts.

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
8 16261
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ^CeFoS^ | last post by:
Hello to everybody, I've done an application that draws in a frame the trajectory of a robot. The robot position is readed through the serial port, and several commands are wrote through the...
1
by: kiran | last post by:
Hi all, I have a problem to communicate with serial port(COM3:). I am able to open the handle but cannot send any data. 1. I connected my motoroala handset to PC through datacable. 2. I...
13
by: Rob | last post by:
Hi all, I am fairly new to python, but not programming and embedded. I am having an issue which I believe is related to the hardware, triggered by the software read I am doing in pySerial. I...
16
by: bloggsfred00 | last post by:
I need to read incoming bytes on a COM port but I do not want to have the script hang if there is nothing to read. Is there any way to have PHP interrogate a COM port buffer to see if there is...
4
by: rowan | last post by:
I'm writing a driver in Python for an old fashioned piece of serial equipment. Currently I'm using the USPP serial module. From what I can see all the serial modules seem to set the timeout when...
3
by: naveen.sabapathy | last post by:
Hi, I am trying to use virtual serial ports to develop/test my serial communication program. Running in to trouble... I am using com0com to create the virtual ports. The virtual ports seem to...
9
by: Hal Vaughan | last post by:
I've done a fair amount of Googling for information on reading the serial port in C++ (and in Linux). Unfortunately, out of every 4 hits, 1 seems to be an unanswered question, 1 is someone saying,...
6
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...
6
by: anu29dolly | last post by:
Hello everyone... I have written a program to write and read data from serial port.... I am able to write 80(in binary)..and is expecting 1B but i am uable to read it... My code goes as...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.