how to implement 9-bit communication under C#.NET & Win32

Posted on 2011-05-08
Last Modified: 2012-05-11
I've been trying desperatly for the past couple of weeks to implement a 9-bit protocol (a.k.a multidrop) under c#.

just to make clear:
bytes under the protocol are 8 bits, where the first byte of the packet has a parity of 1 (Mark) and addtional bits have the parity of 0 (Space).

writing packets are the easy part, simply setting the parity for each byte.

reading the packets is the problem and here lies my problem:

.NET SerialPort class only signlas that there was a parity error, but not in which bit it was occured. Only closest solution is to use the ParityReplace member - which causes 2 new problems: first byte information is lost and we have no way of identifying whether it is the first byte or a part of the information.

I turned to importing kernel.dll functions such as ReadFile/WriteFile which .NET SerialPort class implements under the hood. The reasonable solution is to use the Space parity settings, set fAbortOnError flag to true, so when a byte with the 1 (Mark) parity bit will be recived, communication will stop until i'll renew it - and so it works.

So what's my problem?

Appearantly, bytes I have yet to recieve, are stored in an internal win32 buffer which only saves the 8-bit data and not the parity bit along it's side. So when my program is dealing with other matters, every byte - whether its parity is 0 or 1 - is inserted to the buffer, causing to the program to read more than a packet length.

consider the code:

while (true)
WaitCommEvent(mFileHandle, out comEvent, IntPtr.Zero);
ClearCommError(mFileHandle, out errors, out comstat);
ReadFile(mFileHandle, tempBuffer, cMaxPacketSizeInBytes, out actualBytesRead, IntPtr.Zero);

I configured the port to wait for a parity error (mask = parity event).

WaitCommEvent waits for the first parity error to occur (first byte of the packet is 1), ClearCommError releases the closest error and ReadFile (Should) read until the next error (Parity 1 when expected 0).

however ReadFile gives me ALL of the recieved bytes = 15 packets where I made sure to send them with the first byte Parity Marked.

I have scanned the web desparately and I KNOW there is some solution to this other then reprogramming serial.sys driver as there are software packages who implement this protocol.

any help will be most appreciated.
Question by:gilzu
    LVL 82

    Expert Comment

    by:Dave Baldwin
    Here's an article from National Instruments that tells how to do it:

    The problem is that 9-bit communication is not natively supported in PC uarts like the 8250/15550 and the clones.  Here is another page showing uarts and chips that do natively support it:

    Author Comment

    Thanks for the reply, however:

    link 1: discusses what i've already said and the solution i tried which failed due to the buffer. it also uses Labview and not .NET.

    link 2: solves it by hardware, I'm looking for a software solution as i dont intend to build a special microcontroler
    LVL 82

    Expert Comment

    by:Dave Baldwin
    The software solution on a "modern PC" probably requires you to bypass the Windows serial port driver and write your own that can manipulate the bits.  And it's one byte at a time.  You can't use the buffering because then the 9th bit won't track with the correct byte.  In the 'old days', we used to use a Zilog 82530 chip for this because it had the hardware built in.  Sealevel Systems still makes such things but they've gotten very expensive.

    Author Comment

    As far as i've gotten through searching the web, some threads end with suggesting to get the Rx FIFO buffer to 1 or to keep a hi-priority thread that will keep pool bytes from the serial port.

    none which i've been able to achieve

    Accepted Solution

    Solution documented here:
    LVL 82

    Expert Comment

    by:Dave Baldwin
    Glad you found it.
    LVL 59

    Expert Comment

    This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Can I legally transfer my OEM version of Windows to another PC?  (AKA - Can I put a new systemboard in my OEM PC?) Few of us are both IT and legal experts but we all have our own views of Microsoft's licensing rules and how they apply.  There are…
    This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now