SerialPort ParityReplace on Linux
I have an issue with the use of SerialPort on linux.
If parity is set to none, everything seems to work just fine. However, I need to connect to a device using a serial connection where the parity bit is used to indicate a specific byte (some embedded device stuff, there's no way to change that).
On windows, I can ignore the parity errors caused by this by setting ParityReplace to 0, however, on linux this has no effect.
It is not possible to just set parity to none, because I need to specifically set the parity bits when sending messages to the embedded devices.
From what I can tell, it appears this is not implemented. Is this correct or am I missing something here?
If this is indeed the case, are there any known workarounds for this problem? Unfortunately, I was unable to find anything about this problem.
12 Replies
Hmm
On linux, did you check the port settings itself?
like what are your /dev/ttyX settings?
So you want to change the parity, depending on the "data" (message) you are transmitting?
Setting the parity is not the main issue, but I need to suppress parity errors. On windows, this works fine but on Linux it does not.
I know that this is not exactly what this feature is supposed to do but unfortunately, it's quite popular with embedded stuff to "abuse" parity as some kind of signal bit
It appears as if dotnet overrides them once I open the port
Did you try setting ParityReplace to null?
I tried with 0, not with null
Ok, you wont be happy with the answer.
https://github.com/dotnet/runtime/blob/9d5a6a9aa463d6d10b0b0ba6d5982cc82f363dc3/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialStream.Windows.cs
This is the implementation on WindowsOS, line 288
GitHub
runtime/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialStr...
.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps. - dotnet/runtime
This is the implementaiton on UNIX bases OS:
https://github.com/dotnet/runtime/blob/9d5a6a9aa463d6d10b0b0ba6d5982cc82f363dc3/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialStream.Unix.cs
GitHub
runtime/src/libraries/System.IO.Ports/src/System/IO/Ports/SerialStr...
.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps. - dotnet/runtime
Line 354
🫡
The issue of ParityReplace being ignored on serial ports in linux often stems from the way linux handles serial port configurations. Unlike windows, linux might not support certain parity setttings or might interpret them differently. Thic can lead to unexpected behavior when trying to use parity settings like ParityReplace.
Just an idea, but you might try using ErrorReceived event additianally to DataReceived. - Maybe you will be able to get your data there and handle it further.
SerialPort.ErrorReceived Event (System.IO.Ports)
Indicates that an error has occurred with a port represented by a SerialPort object.
I cannot test this, since i do not have serial port hardware, but if i were you, i would give it a shot. - Good luck
Something like this. - e.EventType you could also find out if it is an RXParity issue.
thanks, I had a look at the source code earlier today too, at least this confirms why I'm running into these issues. The event is not always being called in order with dataReceived so that's not going to work unfortunately (tried it just now). Anyways, thanks for the help with this.