Simple C or C++ script to read serial port buffer

O

Thread Starter

Obscurius

I would like to read a text string from a device which connected to the serial port and save this string to a text file or in to a database..

My primary problem is to get the information from that buffer into a text file... I tried with the HyperTerm under windows.. It was ok I was able to read the buffer... But how can I do this with linux and c or c++?
Please help me!
 
M

Michael R. Batchelor

There are probably 10,000 examples you can find using Google in just a few minutes. There are also a lot of good books you could pick up. What you want to do isn't hard at all, but will require a little work on you part unless you just want to throw some money at it. Unless it's a really odd device someone probably already has a driver and some kind of program to talk to it. And if not, then come back here and probably 50 of us here would be happy to quote you to develop one.

But try it yourself first. You'll be glad you did.
--
Michael R. Batchelor - Industrial Informatics, Inc.
Contribute to society: http://www.distributed.net/ogr/
 
Hi, do a google for the following, I forgot where abouts on the web that I found this:

Serial Programming Guide for POSIX Operating systems

it is a pdf file from memory and will have all the details and examples that you will need.
 
Look at the system calls "CreateFile" and "WriteFile". You can create a file handle for a COM port and write data to it as if it were a file.

I pulled this out of my Win32 system Services book by Brain and Reeves.

~Ken
 
If you know how many bytes to expect you can even do this in a script. For instance, if you know you'll always get 8 bytes at a time:
dd if=/dev/ttyS0 of=data.$(date +%N) count=1 bs=8

It is also pretty easy to do this in c or c++. Use the same commands you would to open and read from a file, but use /dev/ttyS0 for the file name.
ttyS0 == Com1 ttyS1 == com2, etc...

Good luck!
 
<p>A simple Linux bash script solution:
<pre>
#/bin/bash
while true
do
read LINE < /dev/tty0S
echo $LINE
done
</pre>
 
C
For the most part, Yes. It aims at POSIX compliance but I doubt anyone has tried to get it certified. With the present state of things, I think it's more important that POSIX systems be Linux compliant. Any differences from the POSIX spec should be well documented. Remember, Linux has no secrets. If you run into problems, ask! I've done some
Linux serial work as have many lurkers.

Regards

cww
 
did you have a solution of ur problem? can u help me plz? I need also to read the contents of my serial port and write also to my serial port, how can I do it?
 
<p>solution:
<pre>
1> open the port:

fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd == -1)
{
/*
* Could not open the port.
*/
</pre>
<p>2> read the port:
<pre>
while ((nbytes = read(fd, bufptr, buffer + sizeof(buffer) - bufptr - 1)) > 0)
{
bufptr += nbytes;
if (bufptr[-1] == '\n' || bufptr[-1] == '\r')
break;
}
</pre>

<p>3> writing the port:
<pre>
if (write(fd, "AT\r", 3) < 3)
continue;
</pre>
 
you can read a data from the serial port using turbo C compiler, use inport and outport to read. if you want the code mail me. i will send you.
 
Top