PRODUITS

RESSOURCES

INFO TETRAEDRE

PLATEFORME WEB DE DEMO

Serial Communication for WIN32

Serial 1 - non-Event driven version


by Thierry Schneider

  • First Release April 8th 2001
  • revised April 14th 2001, minor corrections
  • revised April 24th 2001, modification for Visual C++
  • revised April 29th 2001, correction of bugs that occured during reconnection

Foreword

If you experience any problem using this software, please be sure that you have the latest version. Check our web site www.tetraedre.com for an updated version of the software. 
Otherwise report any bug to the address above or check the FAQ




Each programmer knows that accessing the hardware ports of a computer is getting more and more complicated. The software presented here intends to ease the development of applications using the serial port (COM) on computer running Windows 32-bits operating systems.

Allen Denver, from the Microsoft Windows Developer Support wrote one of the only technical article describing in details how to use the serial port under Win32. This article can be downloaded on Microsoft's web site. A copy of this article can also be found here.

From that article, I've written two different software packages. One for what I call "non-event-driven" applications. This means simple applications like console applications,... The other package is intended to be used in event-driven applications (see serial2).

All software and documentation described here can be downloaded directly here : serial.zip

if you have questions, don't hesitate to send me an email (developer@tetraedre.com) and/or visit our website (www.tetraedre.com) to read the FAQ (look inside the developer's corner)


Compilation with Borland C++ Builder

The ZIP file contains two Borland C++ project files (sertest.bpr and sertest2.bpr). Simply open them, compile, run and enjoy !

Compilation with Microsoft Visual C+

The project was originally created to be compiled with the Borland C++ Builder development environment. In order to permit compilation  with Microsoft's Visual C++, I've modified a little the source code. A "#ifdef __BORLANDC__" has been inserted in the header part of the sertest.cpp and sertest2.cpp files in order to skip some Borland's specific pre-compiler commands.

Software API

Once you have read Allen Denver's article everything is getting clear, so the source files provided here is simply auto-documented. I've written clear software with a very simple example that should be sufficient to understand well the software.

The following table given an overview of the functions the application can use. As we see, these one are very simple and allow the writting of small application that access the serial port.

You may download the following files:

  • tserial.h
  • Header file for the serial communication object 
  • tserial.cpp
  • C++ source file for the serial communication object
  • sertest2.cpp
  • Simple application using the serial communication object

     
    Function Description
    Tserial() Object creation
    ~Tserial() Object destruction
    int connect (char *port_arg, int rate_arg, serial_parity parity_arg) Serial port setup and start of connection
    • port_arg : "COM1", "COM2", ...
    • rate_arg : 19200, 9600, ...
    • parity_arg : spNONE, spODD, spEVEN
    Returns 0 if no error occured
    void sendChar (char c) Send a char on the serial line. Blocks until it is sent
    void sendArray (char *buffer, int len) Send an array of char on the serial line. Blocks until it is sent
    char getChar (void) Returns the next char from the input buffer. Blocks if no data is available
    int getArray (char *buffer, int len) Returns an array from the input buffer. Blocks until (some) data are available. The returned parameter indicates how many bytes have been read
    int getNbrOfBytes (void) Returns the number of bytes waiting in the input buffer
    void disconnect (void) Disconnect the serial port