C
C#•2w ago
floribe2000

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
Hannsen
Hannsen•2w ago
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?
floribe2000
floribe2000OP•2w ago
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
Hannsen
Hannsen•7d ago
Did you try setting ParityReplace to null?
floribe2000
floribe2000OP•7d ago
I tried with 0, not with null
Hannsen
Hannsen•6d ago
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
Hannsen
Hannsen•6d ago
Hannsen
Hannsen•6d ago
Line 354
Hannsen
Hannsen•6d ago
No description
Hannsen
Hannsen•6d ago
🫡 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.
Hannsen
Hannsen•6d ago
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
c#

internal class Program
{
static void Main(string[] args)
{
var sp = new SerialPort();

sp.DataReceived += Sp_DataReceived;
sp.ErrorReceived += Sp_ErrorReceived;
}

private static void Sp_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Error Data Received:");
Console.Write(indata);
}

private static void Sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Data Received:");
Console.Write(indata);
}
}
c#

internal class Program
{
static void Main(string[] args)
{
var sp = new SerialPort();

sp.DataReceived += Sp_DataReceived;
sp.ErrorReceived += Sp_ErrorReceived;
}

private static void Sp_ErrorReceived(object sender, SerialErrorReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Error Data Received:");
Console.Write(indata);
}

private static void Sp_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
Console.WriteLine("Data Received:");
Console.Write(indata);
}
}
Something like this. - e.EventType you could also find out if it is an RXParity issue.
floribe2000
floribe2000OP•6d ago
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.
Want results from more Discord servers?
Add your server