2
votes

This is an oddity that I can't figure out. I would really appreciate any input anyone has.

I am trying to receive temperature data from an arduino, linked to my PC via a XBEE wireless card. The receiving XBEE connects directly to the COM8 serial port on my PC. I have a very simple C# program that receives the data using the serial port class. First it reads a char from the serial port

    *private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
    {
        //RxString = serialPort1.ReadExisting();
        RxRead = serialPort1.ReadChar();
        //RxRead = 0x13;
        this.Invoke(new EventHandler(DisplayText));
    }*

(ignore the commented out bits. They are other experiments!). Then, using the RxRead int to transfer the data I write in on a simple Web form.

    private void DisplayText(object sender, EventArgs e)
    {

            RxString = RxRead.ToString();
            textBox1.AppendText(RxString);
            textBox1.AppendText(", ");
    }

And it prints out fine on the Web form.

126, 0, 125, 51, 63, 0, 125, 51, 63, 0, 64, 63, 63, 82, 63, 63, 1, 49, 57, 46, 48, 48, 13, 10, 63,

Except, that the two instances in the above stream of 125, 51 are incorrect. They should be decimal 19, hex 13. I know this is the case, because the protocol is well defined, and I can see the hex data coming from the XBEE using the XCTU programme, (see below)

Receive Packet

7E 00 13 90 00 13 A2 00 40 A6 90 52 90 9A 01 31 39 2E 31 32 0D 0A B5

- Start delimiter: 7E
- Length: 00 13 (19)
- Frame type: 90 (Receive Packet)
- 64-bit source address: 00 13 A2 00 40 A6 90 52
- 16-bit source address: 90 9A
- Receive options: 01
- Received data: 31 39 2E 31 32 0D 0A
- Checksum: B5

Can anyone think of any reason why this program should pick up every byte coming in perfectly cheerfully, but have a hissy fit every time it encounters hex 13? (Oh and I have tried using readbyte instead of readchar, and it gives me the same thing.

Any thoughts very gratefully received).

2
It is the same data, just displayed differently. One in base 16 (hex), the other in base 10 (decimal). Simply a matter of how the values are printed, the byte values are the same. The 63 values you get is the ASCII code for '?'. Which you got because you cannot use ReadExisting() to read binary data. You must use Read().Hans Passant
Be careful with byte-order (big-endian or little-endian) For c#, see here: msdn.microsoft.com/en-us/library/…Iain Ballard

2 Answers

1
votes

Make sure you turn off XOn/Xoff flow control when initializing the serial port. 0x13 corresponds to XOFF, i.e. "Pause transmission", and it will be stripped from the stream you receive. I do not have precise explanation for what happens after that, but if XOn/Xoff is enabled, sre the first thing you ened to do is to disable it.

1
votes

I think you should be using ReadByte() instead of ReadChar(), since you're reading binary data. If you switch to that method, does it start printing the correct values?

Additional information:

  • Any byte with the 8th bit set is represented as 63 (0x3F, '?').
  • For some reason, the 0x13 byte is printed as two bytes: 0x7D, 0x33 ("}3").

Are you opening the serial port in 8-bit mode?