ClearCTSEvent(); // Make sure there are no pending events
ClearCTSEvent(); // Make double sure there are no pending events
DeassertRTS(); // Assert binary command mode
bCmd |= 0x80; // Set msb for reading the register
WriteData(bCMD); // Send binary command
WaitForCTSEvent(); // CTS will frame the response
AssertRTS(); // De-assert binary command mode
ReadData(); // Retrieve the binary response from the comm port buffer
Here are some guidelines to keep in mind when developing applications to use the binary command interface:
During a binary read, the CTS signal is used to frame the binary response. We recommend monitoring the CTS line to differentiate the response from the binary command from data coming out the serial port that may have been received over-the-air.
When doing a read, be sure to clear the CTS event twice before asserting the binary command mode. This can be cleared by reading it using the WaitCommEvent (see MSDN).
Set RTS using EscapeCommFunction (see MSDN).
CTS will frame the response so wait for a CTS event before de-asserting the command mode.
If using a USB serial driver, note that it is not as responsive as a mother board serial driver. This is because the USB protocol adds additional latency because it is time slot driven (TDMA). As a result, the driver cannot get immediate responses to queries but must wait for the USB to return the result.
With that in mind, don't poll the driver rapidly (every millisecond). Try to avoid doing single character events but design comm port functions to operate on larger blocks of data when possible.(I.E. Try to read blocks of data from the comm port, not just one character. This is more efficient for the driver and the USB bus.)
Use the WaitCommEvent with timeouts to trigger on events instead of using a port polling mechanism. This programming technique allows the driver the time it needs to respond and keeps the application from overloading it with requests.
Following these guidelines will facilitate the implementation of binary commands on a PC platform using the windows operating system.
Aug 08, 2017