Reputation: 466
Tearing my hair out with an RS232 programming issue.
I have a char[7] buffer with some characters in it which I am writing to a serial port. I am using a Serial Port Monitor program to monitor incomings and outgoings from the port.
The buffer I am sending to the port contains
a5 00 01 00 0b 4a
when I get to the monitor program the monitor program indicates that it can see:
3f 00 01 00 0b 4a
So the header character is either being monitored wrong or being changed on the way out of the program.
I'm stumped and not sure how to get around this.
this is the code I am using to populate the buffer.
char[]cmdBuffer=createCommandBuffer(0, 4, 1);
cmdBuffer[4] = Convert.ToChar(what);
placeChecksums(cmdBuffer);
serialPort.Write(cmdBuffer, 0, cmdBuffer.Length);
I have checked the contents of cmdBuffer in a breakpoint before calling serialPort.Write
and they are correct. I can only conclude that the monitor program is wrong, but then when I use it to monitor other software running at the same BAUD rate - it monitors correctly.
Anyone else had this same issue?
Upvotes: 1
Views: 494
Reputation: 1500225
You're handling characters, but you've expressed values as bytes.
I would personally encourage you to use binary data everywhere you can, to simplify things - but the reason a5
is being converted to 3f
is almost certainly due to the Encoding
property of SerialPort
, which defaults to ASCII. The value 0xA5 doesn't exist in ASCII, so it's converted to '?' instead.
If this is genuinely meant to be text, then change the encoding appropriately - but if it's meant to be binary data, then use binary APIs instead (which deal in bytes and byte arrays instead of characters, character arrays and strings).
Upvotes: 3