Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2011-05-08
8
Medium Priority
?
2,593 Views
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.
0
Comment
Question by:gilzu
  • 3
  • 3
7 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 35716092
Here's an article from National Instruments that tells how to do it: http://digital.ni.com/public.nsf/websearch/3BDC7FF03541F772862564990057F919?OpenDocument

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: http://rs485.bocc.de/rs485_desc1.htm
0
 

Author Comment

by:gilzu
ID: 35716490
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
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 35716790
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 http://www.sealevel.com/store/serial/synchronous-serial/pci.html but they've gotten very expensive.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:gilzu
ID: 35718149
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
0
 

Accepted Solution

by:
gilzu earned 0 total points
ID: 35733421
Solution documented here: http://gilzu.com/?p=6
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 35734307
Glad you found it.
0
 
LVL 59

Expert Comment

by:LeeTutor
ID: 35929262
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Suggested Courses

810 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