You are here

Serial communication - the programming under Windows

This article describe how to create programmes working with hardware under Windows one should use API's funntions. Here is a few indispensable functions which you need to establish communication. For example funtions to open and close connection, set the connection parameters and functions used to read or write the information from/into the port.

About programming

Unlike DOS, Windows 9x, NT, 2000, XP work with hardware in a different way. While a DOS driver could be created with asm and have direct acsess to ports, it is a bit more complicated in Windows.It is connected with the fact that Windows, unlike DOS, is a multitasking system which makes it impossible to allow every application to directly change the hardware settings, as one application may fail to 'know' about the changes made to the hardware settings by some other application. Actually one can use asm in and out 378h functions under Windows 9x, but it is undesirable due to said above. To create programmes working with hardware under Windows one should use API (application programming interface). This interface allows to use Windows system services from application programmes. API realization is at that entrusted to the drivers. Windows Driver Developer Kit (DDK)is used to create drivers (there is a separate DDK for every Windows OS). Besides API one can use IOCTL codes (this method was widely used in DOS),but we shall deal with API functions only.

Work with hardware under Windows

API standartizes work with hardware. To get access to hardware the following steps are used:

  • Get Handler of the device by calling CreateFile with the device name. For more information refer to. Windows SDK Help.
  • To control the device, call an API fuction for this device or send IOCTL(input - output control), the latter via DeviceIOCtl (for more information refer to Windows SDK Help).

In Windows all input/output ports are presented as files, so work with ports is mainly carried out via i/o functions of the file (CreateFile, CloseHandle, ReadFile, ReadFileEx, WriteFile and WriteFileEx). These functions organize the main interface for opening and closing the connection resource descriptor and carrying out read/write operations. API also includes a set of connection functions which provide access to connection resourses.

The usage of the I/O file and connection functions allows the application to perform the following tasks:

  • Getting the serial port descriptor.
  • Serial port configuration set and request.
  • Reading from or writing into the serial port.
  • Control of the given events set, which could occur for this serial port.

Sending the executive instructions to the driver of the device connected with the specified serial port; driver call-in is required for extended functions execution.

Open and Close Port

Opening a port is actually getting the descriptor of the serial port. Due to API it can be done by using CreateFile function. This function results in the creation of a file with a reserved name.It is important when getting access to the corresponding port or device.After the descriptor has been obtained the work with the port is carried out the same way it is with files.

Function SetupComm

As COM-ports are asynchronous connection devices, buffers for incoming and outgoing data are provided to make the work with the ports more effective. It is connected with the fact that the data bus baud rates greatly varies from the line baud rates, that's why to optimize the work of the system it is advisable to read data from/write data into the port by batches regardles of when they were received. One can also write data into the buffer and only then start the transmission - it is useful when the batch transmission is needed regardless of whether the system is busy. To set the size of the receiving and transmitting buffers SetupComm function is used.

Function syntax:

BOOL SetupComm(HANDLE hFile, DWORD dwInQueue, DWORD dwOutQueue);

Communications Time-outs

Another major thing affecting the work of read and write operations is time-outs. Time-outs have the following effect on read and write operations. If an operation takes longer than the calculated time-out period, the operation is finished. No error code is returned by ReadFile, WriteFile, GetOverlappedResult, or WaitForSingleObject. All indicators used to monitor the operation show that it finished successfully. The only way to tell that the operation has timed out is that the number of bytes actually transferred are lower than the number of bytes requested. So, if ReadFile returns TRUE, but fewer bytes were read than requested, the operation has timed out. If an overlapped write operation times out, the overlapped event handle is signaled and WaitForSingleObject returns WAIT_OBJECT_O. GetOverlappedResult returns TRUE, but dwBytesTransferred contains the number of bytes transferred before the time-out. The following code sample shows how to handle this in an overlapped write operation.

PurgeComm

Before starting your work with the port it is desirable to clear the buffers; sometimes there's also need to clear the buffers when working with ports. For these purposes PurgeComm function can be used. This function can also stop read and write operations.

Function syntax:

BOOL PurgeComm(HANDLE hFile, DWORD dwFlags);

Work with DCB

The port setting is carried out with the help of the DCB (Device-Control Block) structure.By filling this structure with needed values you can change the connection parameters to those needed at the moment.

To initially create the DCB structure with necessary general settings (baud rates, patity, number of bits, number of stop bits and flow control) is carried out by the BuildCommDCB function.

Function syntax:

BOOL BuildCommDCB (LPCTSTR lpDef, LPDCB lpDCB);

Read and Write Port

As work with the ports in Windows is carried out in the same way as work with files, reading from and writing into the port are carried out with the help of ReadFile and WriteFile functions correspondingly.

ReadFile function is used to read the information from the port

Function syntax:

BOOL ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped);

BOOL WriteFile(HANDLE hFile,LPCVOID lpBuffer,DWORD nNumberOfBytesToWrite,LPDWORD lpNumberOfBytesWritten,LPOVERLAPPED lpOverlapped);

Event

Win32 API provides WaitCommEvent function used to wait for events which can occur for the specified communications device. At that the set of events checked by this function is contained in the events mask connected with the given device.

Function syntax:

BOOL WaitCommEvent(HANDLE hFile, LPDWORD lpEvtMask, LPOVERLAPPED lpOverlapped);

Links:

Related articles

Hodnocení článku: