DarthAffe / OBD.NET

C#-Library to read data from car through an ELM327-/STN1170-Adapter
GNU General Public License v2.0
178 stars 82 forks source link

Move EnhancedSerialPort and SerialConnection out of OBD.Desktop #22

Open Penlane opened 2 years ago

Penlane commented 2 years ago

Hey there,

I'm using this library to read out OBD data with a Raspberry Pi on Linux.

Short Story: It works!

Long Story: There is no need for the Serial-related classes to be encapsulated in a Windows-Only .NET Framework Namespace, so why keep it there?

In order to make it work, had to manually copy the files to my own namespace and comment out the following lines:

if (!IsWindows)
{
    FieldInfo fieldInfo = BaseStream.GetType().GetField("fd", BindingFlags.Instance | BindingFlags.NonPublic);
    if (fieldInfo == null) throw new NotSupportedException("Unable to initialize SerialPort - 'fd'-field is missing.");
    _fd = (int)fieldInfo.GetValue(BaseStream);

    _disposedFieldInfo = BaseStream.GetType().GetField("disposed", BindingFlags.Instance | BindingFlags.NonPublic);
    if (_disposedFieldInfo == null) throw new NotSupportedException("Unable to initialize SerialPort - 'disposed'-field is missing.");

    fieldInfo = typeof(SerialPort).GetField("data_received", BindingFlags.Instance | BindingFlags.NonPublic);
    if (fieldInfo == null) throw new NotSupportedException("Unable to initialize SerialPort - 'data_received'-field is missing.");
    _dataReceived = fieldInfo.GetValue(this);

    _thread = new Thread(EventThreadFunction);
    _thread.Start();
}

Since it threw exceptions.

Under .NET Core or .NET 5+, Serialports work cross-platform with the System.IO.Ports package. Maybe it's time to move this out of the Desktop package?

DarthAffe commented 2 years ago

Ok i see two separate things here:

There is no need for the Serial-related classes to be encapsulated in a Windows-Only .NET Framework Namespace, so why keep it there?

There is no windows / non-windows separation. The desktop/universal split is .NET Framework or UWP. As long as you run a full framework (.net 5 or >4.6.1 under mono) it's intended to use OBD.Desktop on linux too.

Since it threw exceptions.

That's of course not intended. Without the actual message it's hard to tell but I guess they changed the internals of the serial connection in .net 5. In that case the fix would be to surround this block with a #IF NET461 to only do it for old frameworks. I can do that but I'm not able to test it.

Penlane commented 2 years ago

As long as you run a full framework (.net 5 or >4.6.1 under mono) it's intended to use OBD.Desktop on linux too.

That's neat, I didn't know the cross-platform portion (previously .net core) is considered a full framework as of .net 5. I thought OBD.Common was cross-platform and OBD.Desktop was limited to Windows or Mono; my misunderstanding.

I can do that but I'm not able to test it.

I can happily test a beta / preview on .net 5 under linux and report back.