I’m trying to read some data on the Serial Port but I doesn’t have the same result between a Monitor Software (like 232 analyzer) and my program.
Here is what 232 analyzer reads:
001 003 001 102 000 004 165 234
001 003 006 144 000 008 068 169
001 003 005 000 000 042 196 217
001 003 001 096 000 042 197 247
001 003 001 096 000 042 197 247
001 003 014 000 000 042 198 253
And here is what I read from SerialPort:
001
003 001 102 000 004 165 234
001
003 006 144 000 008 068 169
001
003 005 000
000 042 196 217
001
003 001 096 000 042 197 247
001
003
001 096 000 042 197 247
001
003 014 000 000 042 198 253
All my frames are just “truncated” and I don’t find out why.
The SerialPort is configured in 9600, n, 8, 1 in both programs.
How can I fix this and get the same frames that in 232 analyzer?
P.S:
I get frames like that (the ReadTimeout is set to 200ms):
while(true)
{
byte[] buff = new byte[1024];
int read = sp.Read(buff, 0, 1024);
// ...
}
EDIT
When doing an Thread.Sleep(10); before reading, I get:
001
003 001 102 000 004 165 234
001
003 006 144 000 008 068 169
001
003 005 000 000 042
196 217
001
003 001 096 000 042 197 247
001
003 001 096 000
042 197 247
001
003 014 000 000 042 198 253
Serial ports are completely undelimited so whenever you read, you read what is available. That might be 1 byte, 10 bytes, or 1024 bytes. Wait a little longer and you’ll have more data, albeit of an entirely unpredictable size unless you only read a byte at a time.
The “monitor” software likely simply formats it into nice blocks for you.