|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Bug in SerialPort class of .NET Framework 2.0?control a thermal printer, and have encountered some strange behavior, where the SerialPort class seems to miss or eat the first byte of data it receives from the device. The port is configured as follows: BaudRate = 38400 DataBits = 8 Handshake = Handshake.XOnXOff Parity = Parity.None Port = COM1 StopBits = StopBits.One All other values are their default. I have tried hooking the DataReceived event and reading the data using a loop similar to the following: while (port.BytesToRead > 0) { byte b = (byte) port.ReadByte(); ... } I've also tried reading all available data in one go, using the SerialPort.Read(byte[], int, int) method with the same result. I also tried using the BeginRead()/EndRead() methods on the SerialPort.BaseStream property with the same result. I hooked up a protocol analyzer (FTS SerialTestAsync 2) and verified that I was being sent the proper data by the device. I set a breakpoint on the first call to SerialPort.ReadByte(), but it is actually returning the second byte of data. The behavior is not consistent in that it doesn't happen every run of the program. Has anyone seen similar behavior? Any workarounds? Thanks, Russell Klenk Hi,
I haven't seen this (ever), and I used it with lots of different devices. I wonder if XonXoff is causing trouble? Try it without enabling handshaking. I doubt that flow control is required, but, even if it is, this may give use more information. Also, you are reading data one byte at a time. It would be much more satisfactory to real all of the available data into an array of bytes (and to process that array) than to use a loop. Your code is the slowest way possible to read data. Dick -- Richard Grier, MVP Hard & Software Author of Visual Basic Programmer's Guide to Serial Communications, Fourth Edition, ISBN 1-890422-28-2 (391 pages, includes CD-ROM). July 2004, Revised March 2006. See www.hardandsoftware.net for details and contact information. |
|||||||||||||||||||||||