473,387 Members | 1,535 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,387 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 16308
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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
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...

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.