Search code examples
c#.netserial-port

c# SerialPort how to force ignore parity errors?


I'm connecting with a 485 serial port device, the device uses Even parity, but perhaps its parity signal isn't strong or reliable enough and sometimes can't pass c# SerialPort check, thus padding some received bytes with '?' characters.

  1. I tried different serial port debuggers, some can receive all data right, and some can't just like the c# program does. So there should be some way to ignore parity errors and just read the data, but how?
  2. I searched and found c++ has a flag to ignore parity error, but our whole program is in c#, we have not that enough time to reprogram.
  3. I tried to set the parity property to None, and manually convert the received bytes to bits, and using bit operators to discard every 9th bit and reconstruct the other bits to bytes. However it doesn't work either. I try to find the reason, and perhaps it's because serial port bit flow is not continuous and contains something like stop bits itself, so I can't just reinterpret the bits?
  4. Or if there exists some third party c# serial port library, able to ignore parity error?

Solution

  • OK, there is a simple solution, I'm stupid didn't find it previously.

    SerialPort sp; sp.Parity = Parity.Even; sp.ParityReplace = 0; Set ParityReplace to zero and it's all ok. The zero means don't replace parity-error bytes.