BUS INTERFACE:THE SERIAL COM PORTS.

THE SERIAL COM PORTS

The serial communications ports are COM1–COM8 in older systems and may contain any number of ports in modern systems, but most computers only have COM1 and COM2 installed. Some have a single communication port (COM1). These ports are controlled and accessed in the DOS environment as described in Chapter 11 with the 16550 serial interface component and will not be dis- cussed again. Instead, we will discuss the Windows API functions for operating the COM ports for the 16550 communications interface. USB devices are often interfaced using the HID (human interface device) as a COM port. This allows standard serial software to access USB devices.

Bus Interface-0344

Communication Control

The serial ports are accessed through any version of Windows and Visual C++ by using a few system application interface (API) functions. An example of a short C++ function that accesses the serial ports is listed in Example 15–9 for Visual Studio.net 2003. The function is called WriteComPort and it contains two parameters. The first parameter is the port, as in COM1, COM2, and so on, and the second parameter is the character to be sent through the port. A return true indicates that the character was sent and a return false indicates that a problem exists. To use the function to send the letter A through the COM1 port call it with a WriteComPort (“COM1”, “A”). This function is written to send only a single byte through the serial COM port, but it could be modified to send strings. To send 00H (no other number can be sent this way) through COM2 use Write Com Port (“COM2”, 0x00). Notice that the COM port is set to 9600 baud, but this is easily changed by changing the CBR_9600 to another acceptable value. See Table 15–9 for the allowed baud rates.

Bus Interface-0345Bus Interface-0346

The CreateFile structure creates a handle to the COM ports that can be used to write data to the port. After getting and changing the state of the port to meet the baud rate requirements, the WriteFile function sends data to the port. The parameters used with the WriteFile function are the file handle (hPort), the data to be written as a string, the number of bytes to write (1 in this example), and a place to store the number of bytes actually written to the port.

Receiving data through the COM port is a little more challenging because errors occur more frequently than with transmission. There are also many types of errors that can be detected that often should be reported to the user. Example 15–10 illustrates a C++ function that is used to read a char- acter from the serial port called ReadByte. The ReadByte function returns either the character read from the port or an error code of 0 × 100 if the port could not be opened, or 0 × 101 if the receiver detected an error. If data are not received, this function will hang because no timeouts were set.

Bus Interface-0347

If Visual Studio Express is in use, the toolbox contains the serial port control that allows access to any COM port. For some reason, this was available in Visual Studio 5, it then vanished in Visual Studio 5 and Visual Studio.net and was re-added to the 2005 Express edition. Many USB devices appear as COM ports and are accessed through the serial port control, as well as classic COM ports. The HID USB device is the main reason that Microsoft added the serial port control to Visual Studio.

Once the serial port control is added to a program, it is typically set up for communication in its properties and then an event handler is used when data are received. Sending data occurs as illustrated in the function listed in Example 15–11.

Bus Interface-0348

To receive data, install the handler for data received. Each time that information is received on the serial port, the data received event is called where the information is processed. Example 15–12 shows the data received function. What does not appear here is that the port must be open to send or receive information using the Open function in the serial port class.

Bus Interface-0349

Leave a comment

Your email address will not be published. Required fields are marked *