1

I have a COM port that I know receive invalid parity bits and I'm using the following program, however I never seem to get the trailing 126 byte that the documentation mentions.

In the following program the console prints lots of !!!!! but no ?????, why?

Also I'm assuming that the byte with the invalid parity bit will still be included in the stream?

using (var serialPort = new SerialPort())
{
    serialPort.PortName      = "COM2";
    serialPort.BaudRate      = 562500;
    serialPort.Parity        = Parity.Space;
    serialPort.DataBits      = 8;
    serialPort.StopBits      = StopBits.One;

    serialPort.ErrorReceived += (s, e) => Console.WriteLine("!!!!!");

    serialPort.Open();

    var thread = new Thread(() =>
    {
        while (isRunning)
        {
            var b = serialPort.ReadByte();

            if (b == 126)
                Console.WriteLine("?????");
        }
    });

    thread.Start();

    Console.WriteLine("");
    Console.WriteLine("Press any key to exit.");
    Console.ReadKey(true);

    isRunning = false;
    thread.Join();
} 
ronag
  • 49,529
  • 25
  • 126
  • 221

2 Answers2

1

It is a documentation bug. The actual replacement character is '?', ASCII code 63.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

This could be an encoding codepage conversion of 126 into 63. The default codepage only passes 7 bit ASCII and knocks off the most significant 8th bit. But if it were just lopping off the msb, that would leave 62, not 63. The default codepage may convert all characters bigger than 127 into question marks. Try encoding codepage 1252 which passes all 8 bits unchanged. Note that the encoding codepage although it appears to be a property (it always reads back the value you last set) is perhaps also bit of a method, as it appears that you to need to always resend the encoding type setting (even if it reads back what you want) after any close / open cycle of the serial port, since otherwise the characters > 127 will be messed up again. In vb -

        ser_port.Encoding = System.Text.Encoding.GetEncoding(1252)