Search code examples
c#cserial-porttermios

Equivalent to setting termios c_iflag=IGNPAR using C# SerialPort class


I'm attempting to port an old C application which talks to a serial device into C#. C applications sets the following fields when setting up serial communication:

newtio.c_iflag = IGNPAR;
newtio.c_oflag = 0;

I do not see a way to replicate this in C# and I believe this causes the serial device to behave differently when "9C" is sent.


Solution

  • https://msdn.microsoft.com/en-us/library/system.io.ports.parity(v=vs.110).aspx

    Public  enum Parity 
    Even    Sets the parity bit so that the count of bits set is an even number.
    Mark    Leaves the parity bit set to 1.
    None    No parity check occurs.
    Odd     Sets the parity bit so that the count of bits set is an odd number.
    Space   Leaves the parity bit set to 0.
    

    You should be interested in None

    create the port with this constructor:
    https://msdn.microsoft.com/en-us/library/system.io.ports.serialport(v=vs.110).aspx

    public SerialPort(
        string portName,
        int baudRate,
        Parity parity
    )
    

    set the parity value with this method:
    https://msdn.microsoft.com/en-us/library/system.io.ports.serialport.parity(v=vs.110).aspx

    _serialPort.Parity = SetPortParity(_serialPort.Parity);