PortableSerialLib  v0.4
a portable serial port lib
PortableSerialLib - Portable Serial Port Library

Introduction

This library implements a platform independent C interface to access a serial port (aka Com Port aka RS232 aka Uart).

C Interface

The header PortableSerialLib.h provides an object based function interface that consists of the following logical groups:

All receive functions are blocking. Hence there is no need for polling the port → let the operating system handle the waiting for data. As a drawback you cannot do things while waiting for input. But that is no really drawback since it is better to split up the two things and use threads.
One thread waiting for data and one thread doing the rest.

Using the Library

Since the library is rather small the most simple way to use it is just adding the header location to the include path and adding the PortableSerialLib.c to the list of source files to compile.

Windows

To compile with Visual Studio a little thing is to do:
search the web for Visual Studio compatible versions of the c99 header files:

You may look at http://code.google.com/p/msinttypes/ for it. Put them somewhere where the compiler can find them. For example parallel to the PortableSerialLib.h header file - but probably to some place that you directly add to VSs standard include paths (i would encourage to use the second variant).

Linux

No additional work should be required.

Example Program

A simple test program that parses a few command line parameters for serial port settings. Then it opens the port configures it and then starts a thread that read lines and outputs them to stdout. Meanwhile the main thread writes every second a "Hello World!" with an up counting number to the port.

Building the Example Program

The PortableSerialLibTest directory contains the program and Visual Studio 2010 (Express) project files as well as a Makefile to build it on Windows and Linux

License

http://www.gnu.org/licenses/old-licenses/gpl-2.0

References

Useful Tools

For Basic Tests:
Null-modem emulator (com0com)
http://com0com.sourceforge.net/

Literature

Serial Port Programming in Windows and Linux
Maxwell Walter
http://gatech.cueyoueye.com/PopShot/res/documents/Serial_Win_02_1_.pdf

Serial Programming/termios
wikibooks
http://en.wikibooks.org/wiki/Serial_Programming/termios

Serial Communications in Win32
Microsoft
http://msdn.microsoft.com/en-us/library/ms810467

Understanding UNIX termios VMIN and VTIME
Steve Friedl's Unixwiz.net Tech Tips
http://unixwiz.net/techtips/termios-vmin-vtime.html

RS-232 for Linux and Windows
Teunis van Beelen
http://www.teuniz.net/RS-232/

Contact

For questions, comments or contributions mail me at r.sta.nosp@m.ffen.nosp@m.@gmx..nosp@m.de or preferentially ask at https://www.mikrocontroller.net/ in the thread:
https://www.mikrocontroller.net/topic/251666
Although it is a german board you may also ask questions in english.

Change Log

PortableSerialLib - Change Log
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  * fixed
  ~ changed
  + added
  - removed
    
v0.4
  ~ PSerLib_getAvailablePorts
    + added linux implementation for PSerLib_getAvailablePorts
    + added define for format of PSerLib_getAvailablePorts-Output (full path or just name)
    ~ changed interface of PSerLib_getAvailablePorts function to get additional 
      pointer parameter to output number of found devices and return a error code.\n
      --> more consistency to all other functions.
    ~ more precise description of o_name parameter PSerLib_getAvailablePorts
  ~ set default baud rate of example/test program to a standard baud rate value

v0.2
initial version