Q: The device I'm communicating with requires me to control the parity bit in a non-standard fashion. How can I do this?
The Windows CE DCB data structure includes the Parity field for setting automatic or manual control of the parity bit. You can force the parity bit to the MARK ("1" or -12V) or SPACE ("0" or +12V) state by changing this field, then calling SetCommState().
However, while the StrongARM UARTs implement most of the functionality of the 16550 UARTs, they do not support forcing the parity to a Mark or Space state. If you try to set the Parity field to one of these states, you will get error 87 (ERROR_INVALID_PARAMETER). The XScale UART does not have this limitation; see below for details.
If you need to set the parity bit to a specific value, you can use an ADS product that has a separate UART, like the Graphics Master, or you can set the UART's parity type to generate the parity bit you want. The rest of this post describes the latter solution.
What is Parity?
The parity bit is a simple means of error checking. The idea is this: you specify ahead of time whether the parity of the transmission is to be even or odd. Suppose the parity is chosen to be odd. The transmitter will then set the parity bit in such a way as to make an odd number of 1's among the data bits and the parity bit. If there are five 1's among the data bits, already an odd number, the parity bit will be set to 0.(*)
Setting the Parity Bit to a Specific Level
You can set a specific parity level by first adding up the number of bits that are set to "1", then setting the UART's parity type to generate the targeted parity bit. The following pseudocode illustrates how to do this: desired_parity_bit = 0 or 1
bits_set_to_one = number of bits in data byte that are set to "1"
;
if ( (bits_set_to_one & 0x01) == desired_parity_bit) )
dcb.Parity = EVENPARITY;
else
dcb.Parity = ODDPARITY;
;
SetCommState (hSerialPort, dcb);
For related information, please see the serial communications topic index.
----------------------
Drew Kidder
ADS Technology Transfer
(*) Thanks to http://www.mindspring.com/~jc1/serial/Basics/BitFormat.html (now only remembered by the google cache) for this concise explanation of parity.
Edited by akidder 10-May-2006: Update example, correcting dcb.fParity to dcb.Parity.