Visual C# Developer Center >
Visual C# Forums
>
Visual C# General
>
SerialPort 0x1A character reading problem
SerialPort 0x1A character reading problem
- Hi,
I'm developing an application wich receives 32 bytes packages, by doing this:
byte[] comBuffer = new byte[32]; comPort.Read(comBuffer, 0, 32);
The problem is, when the packages contains the 0x1A char the reading operations is interrupted.
Does anyone know how may I contour this problem?
Thanks
Answers
- Lol, I know what you meant to said with "sanity check". The remainder bytes are there.
I tried a solution that has fixed my problem. It might not be a good solution but it works.
Since the problem was that the 0x1A was making the Read() method to "jump out", so I read just one byte at the time and then I proceed with rest of the operation.
Thanks you for all your help
for (int i = 0; i < 32; i++) { comPort.Read(comBuffer,i, 1); }- Marked As Answer byCristopher Pereria Thursday, June 04, 2009 4:47 PM
All Replies
- 0x1A is the Ctrl+Z character which for a text file reader means end of file. What type is the "comPort" stream above?
- comPort is a System.IO.Ports.SerialPort instance.
- What is the type of comPort.BaseStream?
- Maybe I'm going to say something absurd.
I haven't define the BaseStream, so I supose that it is the default one.
This is all what I'm doing:
portaCOMProp = new SerialPort();
comPort.BaudRate = "9600";
comPort.Parity = "0";
comPort.StopBits = "1";
comPort.DataBits = "8";
comPort.PortName = "COM5";
comPort.ReceivedBytesThreshold = 32;
comPort.ReadBufferSize = 32;
comPort.DataReceived += new SerialDataReceivedEventHandler(comPort_DataReceived);
comPort.Open();
You have set ReceivedBytesThreshold to 32, meaning that you only want the DataReceived event to fire when there are (at least) 32 bytes in the buffer to read. Are you saying that if the device sends a chunk of 32 bytes and one of those bytes is Ctrl+Z, the DataReceived event does not fire?
- Yes it fires. The problem is that the comBuffer from the position where is the 0x1A to is not filled.
For example, this is the content of the comBuffer if there isn't the 0x1A:
7E 4B 00 00 13 A0 64 1F 01 01 99 19 D3 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 F1 FF
And this is ther is 0x1A:
7E 24 00 00 13 A0 54 1A 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
- I don't know. I've never encountered this problem. It looks like you have done something odd like:
StreamReader reader = new StreamReader(comPort.BaseStream);
reader.Read(buffer, 0, 32);
Injecting a text reader over the SerialPort's native stream. Can you show me your comPort_DataReceived code? - You are using the return value from SerialPort.Read, correct? Because it might be less than count.
-Steve
Programming blog: http://nitoprograms.blogspot.com/
I will be in Chicago for the WPF training: http://blogs.msdn.com/jaimer/archive/2009/04/01/announcing-the-using-wpf-to-build-lob-applications-training-tour.aspx - I'm not using the BaseStrean to read, should I?
Here is the code from comPort_DataReceived:
void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e) { byte[] comBuffer = new byte[32]; comPort.Read(comBuffer, 0, 32); Thread.Sleep(20); comPort.DiscardInBuffer(); if (comBuffer[0] == 126){ ... create a new thread to handle the package } } - What are you connecting to? This may be a stupid question but are you sure this isn't supposed to happen? Maybe that's the end of data char for whatever you connect to.
- You're also calling DiscardInBuffer, (possibly) throwing away part of the next message from the device.
Just for a sanity check, try adding a second Read call after the first to see if the remainder of the message is there (the bytes after the 0x1a). - I'm connecting to a base station that receives those packages from wireless nodes. The 0x1A may be data, and on this particular case it belongs to the serial number from the node that sent the package.
- "sanity check" was the wrong term. What I mean is, by calling Read twice, we will know if the Read call is dropping the bytes, or if something else has.
- Lol, I know what you meant to said with "sanity check". The remainder bytes are there.
I tried a solution that has fixed my problem. It might not be a good solution but it works.
Since the problem was that the 0x1A was making the Read() method to "jump out", so I read just one byte at the time and then I proceed with rest of the operation.
Thanks you for all your help
for (int i = 0; i < 32; i++) { comPort.Read(comBuffer,i, 1); }- Marked As Answer byCristopher Pereria Thursday, June 04, 2009 4:47 PM
- same problem here!
I use a microcontroller with a usb to serial cable and a C# application to read data...
If I cut the C# application and I use hyper terminal for serial data read it's all ok....
If i use C# I've the same problem...
And it's a big problem because I've to read 800 byte (115200 baud/s) and send a new request every 200ms...If i have the 0x1a in the packet....:(
cristopher, can u write here your complete:
void comPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
....
}
If it's a serial port component problem of c#...i think it's a huge problem!
And I'm surprise that Microsoft don't fix it!
thanks

