Writing a Serial Communication Library for Windows - Opening the Selected Serial Port
(Page 2 of 6 )
The CreateFile () function opens a communications port. There are two ways to call CreateFile () to open the port:
• Overlapped: Overlapped operations enable a thread to execute a time-consuming I/O operation in the background, leaving the thread free to perform other tasks. Now while reading or writing to the opened device in overlapped mode of operation, the calling thread must specify a pointer to an OVERLAPPED structure. This mode is mostly used to monitor the activity on the port or to do I/O intensive operations and this library is written to handle Non-Overlapped operations so I’d suggest to interested readers to peek in the MSDN documentation for more details.
• Non-Overlapped: One may open a Communication Port for either Overlapped IO operation or Non-Overlapped IO operation. This class/library is written for Non-Overlapped IO Operation. So we’re not going to discuss about the overlapped mode.
Configuring the Selected Serial Port
When the CreateFile() function opens a handle to a serial communications resource, the system initializes and configures the resource according to the values set up the last time the resource was opened. Preserving the previous settings enables the user to retain the settings specified through a mode command when the device is reopened. The values inherited from the previous open operation include the configuration settings of the device control block (a DCB structure) and the time-out values used in I/O operations. If the device has never been opened, it is configured with the system defaults.
One may use the GetCommState and SetCommState APIs to determine the initial configuration of a serial communications resource or to set a new configuration.
The most critical part in serial communication programming is configuring the port settings with the DCB structure. The DCB structure defines the control setting for a serial communications device. Initializing the DCB structure incorrectly is a common problem among beginners. When a serial communications function does not produce the expected results, the DCB structure must be the first place to check. A call to the CreateFile() function opens a serial port with default port settings. Usually, you’d need to change the defaults as per your requirements. You must set the Baud rate for communication, Parity functions, number of Stop Bits, etc. in accordance with the requirements of the interfaced hardware device by calling appropriate Win32 API Functions.
Configuring Time-Out value
An application must always set communication time-outs by using the COMMTIMEOUTS structure each time it opens a communication port. If this structure is not configured, the port uses default time-outs supplied by the driver, or time-outs from a previous communication application as explained above. By assuming specific time-out settings when the settings are actually different, an application can have read/write operations that never complete or complete too often. You must configure the read & write time-outs by calling the appropriate Win32 API Functions.
Reading/Writing to the Serial Port
The WriteFile() function transfers data through the serial connection to another device. Before calling this function, an application must open and configure a serial port.
An application calls the ReadFile() function to receive data from a device at the other end of a serial connection.
Closing a Serial Port
You must close the communications port after serial transmission in order to make this port available for other applications which use this resource. As long as you are working with a port (i.e. the port is in an open state), other threads or applications will not be able to access to this port till you close the handle to that port in NON-OVERLAPPED IO Operation. Call the CloseHandle() function to close the serial port. CloseHandle() has one parameter, which is the handle returned by the CreateFile() call that opened the port.
Next: CSerialComm Class >>
More Code Examples Articles
More By Digvijay Chauhan