R
Russell.Klenk
I've been using the SerialPort class new to the .NET Framework 2.0 to
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
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