![]() |
Back | Home |
by Thierry Schneider, 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 (like GUI and other windows stuff).
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)
The ZIP file contains two Borland C++ project files (sertest.bpr and sertest2.bpr). Simply open them, compile, run and enjoy !
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.IMPORTANT
But more important. The event-driven application uses multithreading in order to generate the events. So Visual C++ projects that use multithreading must be compiled with the appropriate setting. In your project settings, you MUST choose the multithreaded run-time library, otherwise the programme will not compile
Project settings
|
*--- C/C++
|
*--- Code generation
|
*---- Use run-time library
|
*---- Multithreaded
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:
Header file for the serial communication object C++ source file for the serial communication object 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 Returns 0 if no error occured
- port_arg : "COM1", "COM2", ...
- rate_arg : 19200, 9600, ...
- parity_arg : spNONE, spODD, spEVEN
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
The non-event-driven software package is composed mainly of blocking function. In the event-driven world this is unacceptable.
You will note that this software package doesn't manage messages (or string) but works only on a byte by byte manner. You may also be surprised to discover the OnCharSent event. This is due to the fact that this software package was used in the development of embedded applications. So the OnCharSent event is similar to the "Tx buffer empty" interrupt of most of UART.You may download the following files:
Header file for the serial_event communication object C++ source file for the serial_event communication object Simple application using the serial_event communication object
Methods
Function Description Tserial_event() Object creation ~Tserial_event() Object destruction int connect (char *port_arg, int rate_arg, serial_parity parity_arg) Serial port setup and start of connection Returns 0 if no error occured
- port_arg : "COM1", "COM2", ...
- rate_arg : 19200, 9600, ...
- parity_arg : spNONE, spODD, spEVEN
void sendChar (char c) Send a char on the serial line. Once the byte is sent, the OnCharSent event is called int getNbrOfBytes (void) Returns the number of bytes waiting in the input buffer void disconnect (void) Disconnect the serial port. void setManagerOnCharArrival(type_charEvent manager) This function indicates to the serial object which function must be called when the OnCharArrival event occurs. The manager parameter is the name of the function to call void setManagerOnConnected(type_voidEvent manager) This function indicates to the serial object which function must be called when the OnConnected event occurs. The manager parameter is the name of the function to call void setManagerOnDisconnected(type_voidEvent manager) This function indicates to the serial object which function must be called when the OnDisconnected event occurs. The manager parameter is the name of the function to call void setManagerOnCharSent(type_voidEvent manager) This function indicates to the serial object which function must be called when the OnCharSent event occurs. The manager parameter is the name of the function to call
Events
Function Description void OnCharArrival (char c) This event occurs when a byte is received on the serial communication port. This function is very similar to the "RX buffer full" interrupt of some UART void OnConnected (void) This event occurs when the serial object is started void OnDisconnected (void) This event occurs when the serial object is stopped void OnCharSent (void) This event occurs when the byte has been sent. This function is very similar to the "TX buffer empty" interrupt of some UART