SERIAL PORT ACCESS FROM A COMPACT 7 APPLICATION
For a native application to access a serial port, it uses the same API as for a stream driver. Through standard serial port IOCTLs (implemented by all serial class drivers), additional functions can be applied to a serial port such as configuring its BAUD rate, number of stop bits, and so on. Events can also be trapped by threads using WaitForSingleObject (or WaitForMultipleObjects) as used with the Watchdog Timer. These events include when the transmit buffer is empty, when data has been received, and when a modem control pin such as CTS changes state. The Compact Framework from Version 2 has provided a Serial Port class that implements similar functionality in managed code. This class is the basis of the VS2008_SerialPortApp introduced earlier in this chapter.
In the Custom FTDI stream driver in the next section of this chapter, the stream driver “wrappers” the serial port COM0 as loaded with the FTDI VCP driver. Because the driver is native code, it can use only the native interfaces to the serial stream. (Wouldn’t it be nice to have drivers that could use managed code?) It needs to open and close the serial port, read and write with it, and configure the port. Table 44-5 shows a summary of the code. The stream as implemented does not need any event handlers.
|Open serial port||CreateFile|
|Close serial port||CloseHandle|