This article was contributed by Jurgis Armanavichius.
Environment: VC6, or almost any else C++ compiler for Win32
Background:
For my new project I needed to communicate with our device, based on embedded microcontroller, using the COM2 serial port on my PC. My device have TxD and RxD lines only, so I don't want to use handshake lines. From the MS-DOS times we remember the simple GET_BYTE and PUT_BYTE functions, but how to do so in Win32 environment? Some perfect day I found the CodeGuru's URL. On this site I found the very good serial port class developed by Remon Spekreijse. It was my starting point. My goal was to write simplest serial port class for developers who is not "Guru" in Win32 programming. The working with it must be so simple like with the file: open serial port, read/write some bytes with it and close port. You even not need to know C++!
This simple functions include following:
BOOL ComPort_Open(char *name);
void ComPort_Close(void);
BOOL ComPort_PutByte(BYTE byt);
int ComPort_GetByte(DWORD dwMilliseconds);
Additional functions can be used to preset COM port parameters and check the opened port status:
void ComPort_PresetParameters(DWORD baud,BYTE
databits,BYTE parity,BYTE stopbits);
BOOL ComPort_ChangeParameters(DWORD baud,BYTE
databits,BYTE parity,BYTE stopbits);
DWORD ComPort_CheckInQueue(void);
BOOL ComPort_GetOpenedStatus(void);
For block transfers you can use block transfer functions:
DWORD ComPort_ReadBlock(LPBYTE lpBuffer,DWORD
nMax);
BOOL ComPort_WriteBlock(LPBYTE lpBuffer,DWORD
dwBufferLength);
To use this ComPort class you need simply add files "ComPort.cpp", "CComPort.cpp" and "ComPort.h" in your project and #include "ComPort.h" line in the file where you plan to work with serial port. ComPort class can be used in console applications (like old MS-DOS programs) and in the windows programs. It works well in worker threads also. Demo project shows how simple you can use ComPort class in console applications. Demo project contains two console application examples: VC6 example and GCC 2.95.2 example.
The source code of ComPort class included into demo project lets you simply modify that class for your own needs.
I like to send many thanks to Remon Spekreijse and CodeGuru team.
Download: