Programming serial port

Hello!
Can someone advice me which method I will use if I want to communicate with device via RS232 serial port with this requirements:

  1. Serial port is opened in non-canonical mode.
  2. All the time I need to check is there something to read.
  3. If I have to write something I need to write it immediately.
  4. Overhead of CPU have to be minimum.

Thanks!

Jvrlic

I forget to tell I programming with C on debian with posix standard.

You need to do soime reading. This is the best guide out there for POSIX serial port programming:

Serial Programming Guide for POSIX Operating Systems - Michael R Sweet

Hello,

I worked on a simple oil rig safety device using an RS-232 a few weeks back. I used code based on this class (CSerial). It seems pretty good...

Information
------------
http://www.codeguru.com/cpp/i-n/network/serialcommunications/article.php/c2503

Download
---------
http://www.codeguru.com/code/legacy/network/serial_src.zip

Hope this helps!

Karsten

jim: I asked on this forum: High Level Programming!

kpedersen: Thansk, but I work with C not C++.

There's little difference when the relevant system calls will work in both. Just tear apart classes and make members functions instead.

The Michael R Sweet reference looks really good - I'll keep this handy. But, it shows only the C interface. The samples demonstrate the critical components, and understanding it is a necessary first step before building your object oriented design.

If your target supports standard exception handling, you can take advantage of C++ streams. Here is an example http://www.webkruncher.com/speedstreams.h that demonstrates using C++ iostreams for a local file. I've used the same method to interface with serial ports and sockets. It can be adapted to virtually any IO. Even if your IO needs to be fully optimized, you can still take advantage of the OO structure.

The goal is really to have one, straight forward C++ header file that defines and fully implements everything needed to communicate with your target port. Then, your protocol manager sees your target device as if it were a simple file. So, when you initialize the system that interfaces with your target device, the source code could look something like this....

SerialStream io("COM1");
while(!io.eof())
{
string line;
getline(io,line);
}

io<<"hello"<<endl;

Then you build an object to manage the negotiations required by your protocol and put it to use.

Hope that helps.
-Jmt