Search code examples
c#.netserial-portspecial-characters

How do I set serial special characters?


In my ongoing quest to interface with some legacy equipment, I've discovered that the vendor supplied software sets the special characters to:

00 00 00 00 11 13 

But the SerialPort class of .NET set them to:

1A 00 00 1A 11 13 

I suppose that I have two questions:

  1. What do these bytes mean?
  2. How can I tell SerialPort to use a specific set of special characters?

The latter is all I really care about, but I suspect the former is going to be useful to know.


Update: The following doesn't work:

byte[] specialchars = {
    0x00,
    0x00,
    0x00,
    0x00,
    0x11,
    0x13
};

this.port.NewLine = System.Text.Encoding.ASCII.GetString(specialchars);

Update 2: As requested, here are Portmon logs for the vendor supplied app (filtered to remove the many thousands of IOCTL_SERIAL_GET_COMMSTATUS entries) and for my attempt to match even the first exchange.


Solution

  • NewLine is not what you are looking for. It's the plain old 'new line' sequence, e.g. CR LF or LF alone.

    The special characters are handled like this:

    • EOF — set to 0x1a, you cannot change it in .NET
    • ERR — set by SerialPort.ParityReplace
    • BRK — don't know
    • EVT — set to 0x1a, you cannot change it in .NET
    • XON — set to 0x11, you cannot change it in .NET, and it doesn't even usually make sesn
    • XOFF — set to 0x13, you cannot change it in .NET, and it doesn't even usually make sesn

    It may be helpful for you to study the Win32 DCB structure as well. It's used by .NET internally to set the state of the serial port.