Reading a Serial Port Using Windows

How To Input Data From COM1 Using C++ With WIN32

© Martin Bell

Jun 11, 2009
The latest versions of Windows do not allow direct access to the serial IO ports. This article describes how to read the serial port COM1 using C++ in Windows.

The procedure for reading the serial port of a home computer is very different for the later versions of Windows. This article describes how to set up a port for communication and read from it. Error trapping and handling are not discussed. A working knowledge of C++ is assumed.

Opening COM1

The Windows library needs to be included:

#include <windows.h>

The next step is to declare and initialise a HANDLE:

HANDLE hSerial;

hSerial = CreateFile("COM1", GENERIC_READ, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

Define The Port Parameters in Windows

The serial port can now be accessed via the HANDLE that has been created. For the example being used the port set-up conditions are word length 8, one STOP bit, no parity, and baud rate of 4800:

Windows uses a struct called DCB:

DCB dcbSerialParams = {0};

dcbSerial.DCBlength=sizeof(dcbSerialParams);

dcbSerialParams.BaudRate=CBR_4800;

dcbSerialParams.ByteSize=8;

dcbSerialParams.StopBits=ONESTOPBIT;

dcbSerialParams.Parity=NOPARITY;

Now that the struct has been defined, the port is configured using:

SetCommState(hSerial, &dcbSerialParams);

Note that the baud rate is set using a special format: CBR_4800 for 4800 baud, CBR_9600 for 9600 baud etc. The ONESTOPBIT may be replaced by TWOSTOPBITS, and NOPARITY may be replaced by EVENPARITY or ODDPARITY. If there are any doubts about which settings to use, the manufacturers data sheet should be consulted.

Set Time-Outs in Windows

Although this article does not discuss error handling, it is worth noting that TIMEOUTS need to be included in any program, so that a faulty COM port or external device does not cause problems for the program. The parameters that may be defined are:

  • ReadIntervalTimeout - How many milliseconds to wait before timing out
  • ReadTotalTimeoutConstant - How many milliseconds to wait before returning
  • ReadTotalTimeoutMultiplier - How many milliseconds to wait before returning for the byte previously requested

These are set using a struct:

COMMTIMEOUTS timeouts={0};

timeouts.ReadIntervalTimeout=100;

timeouts.ReadTotalTimeoutConstant=100;

timeouts.ReadTotalTimeoutMultiplier=20;

The timeout conditions are then applied:

SetCommTimeouts(hSerial, &timeouts);

Read Data

This is the easiest part of the procedure, since all of the set-up has already been done. To read i characters from the COM1 port, the following code is used:

char szBuff[i + 1] = {0};

DWORD myBytesRead = 0;

ReadFile(hSerial, szBuff, i, &myBytesRead, NULL));

This reads a string of bytes into an array called szBuff. The string is accessed using the standard C++ commands. All that remains is to close the COM port when access to it is no longer needed.

CloseHandle(hSerial);

Summary of Using Windows To Read a Serial Port

Reading from a serial port on a home computer cannot be done directly using Windows. It is still possible to read COM1 by treating the port as a file, and using a file HANDLE instead. The code involved is easy to set up and use, and timeouts are used instead of using interrupts as they are in a DOS environment, as described in How To Read an RS-232 Port and How To Configure an RS-232 Port for reading. The hardware needed to connect to the serial cable is readily available and cheap.


The copyright of the article Reading a Serial Port Using Windows in C Programming is owned by Martin Bell. Permission to republish Reading a Serial Port Using Windows in print or online must be granted by the author in writing.


COM1 Can Be Read From Windows Using File Handles, M Bell
       


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo