How can I get Serial.IO.Ports to be usable?

WaterDragonWaterDragon USMember
edited January 19 in Xamarin.Mac

I am new to Xamarin.Mac, and I am trying to create a basic GUI that interfaces with an Arduino via USB. I know that I can use Serial.IO.Ports with a console application, because I have been successful with this in the past. When trying to create a Mac application, the Serial.IO.Ports namespace is not available. Any suggestions?

Best Answer

Answers

  • WaterDragonWaterDragon USMember
    Thank you, Chris!
  • steviet02steviet02 USMember ✭✭
    edited October 23

    @ChrisHamons said:
    So it looks like we include that in the XM 4.5 target framework but not Mobile.

    $ monop -r:/Library/Frameworks/Xamarin.Mac.framework/Versions/Current/lib/mono/4.5/System.dll | grep Serial | grep Port
    System.IO.Ports.SerialData
    $ monop -r:/Library/Frameworks/Xamarin.Mac.framework/Versions/Current/lib/mono/Xamarin.Mac/System.dll | grep Serial | grep Port
    

    I have a Xamarin.Forms project that requires serial port accessibility. I'm using the Xamarin.Mac Modern Target for my desktop Mac Application. I can compile and run the code, however, the first time I try to scan to get the port names I get an exception that tells me the "System.IO.Ports is only supported on Windows".

    If I select to target Full (which doesn't include System.IO.Ports) I can't add the System.IO.Ports Package. I expect that considering it's apparently not supported. But I'm confused why I can add it in the Modern target but can't access it.

    I'm not sure what is wrong or if anything can be done to get this working? Any suggestions?

  • ChrisHamonsChrisHamons USXamarin Team Xamurai

    So i'm not seeing that exception in mono's code for "System.IO.Ports" and SerialPort.cs is only included in full:

    net_4_x_System.dll.sources:System.IO.Ports/SerialPort.cs

    I'm uncertain why you are seeing the behavior you are. A small sample describing your behavior would be useful.

    In addition, even on Full (or mono desktop profile) SerialPort is still somewhat limited (as you can see here https://github.com/mono/mono/blob/master/mcs/class/System/System.IO.Ports/SerialPort.cs) in places.

  • steviet02steviet02 USMember ✭✭

    @ChrisHamons said:
    So i'm not seeing that exception in mono's code for "System.IO.Ports" and SerialPort.cs is only included in full:

    net_4_x_System.dll.sources:System.IO.Ports/SerialPort.cs

    I'm uncertain why you are seeing the behavior you are. A small sample describing your behavior would be useful.

    In addition, even on Full (or mono desktop profile) SerialPort is still somewhat limited (as you can see here

    I haven't posted enough to post a link to a test project so I'll just explain what I've done.

    1 - Create a new Solution using Visual Studio for Mac (v7.2 build 636)

    2 - Select Multiplatform App -> Blank Forms App under Xamarin.Forms

    3 - Using Shared Project and XAML UI name the project anything you want.

    4 - In that new solution, create a New Project -> Mac App-> Cocoa App and leave it at Modern target.

    5 - Then I followed your colleagues blog post about Preview: Bringing macOS to Xamarin.Forms to get that project updated. I added Xamarin.forms nuget v 2.5.0-19271-pre2

    6 - This works for me, without trying to access any serial port APIs. If I add the nugget System.IO.Ports I can actually run the app and request the GetPortNames() api but it crashes there with the exception I reported above.

    7 - If I change the Target of the project to Full. My packages need to be retargeted, which I do, then remove the System.IO.Ports nuget as it's not supposed to be needed. But what happens next is that the Xamarin.Forms assemblies aren't found by the project. So the FormsApplicationDelegate class and everything related to Forms is showing as unknown.

  • ChrisHamonsChrisHamons USXamarin Team Xamurai

    nugget System.IO.Ports

    That's your problem. That nuget is very likely this:

    https://www.nuget.org/packages/System.IO.Ports/

    which from what I can tell is a type forwarder for net461:

    monodis packages/System.IO.Ports.4.4.0/ref/net461/System.IO.Ports.dll

    .class extern forwarder System.IO.Ports.SerialPort
    {
        .assembly extern 'System'
    }
    

    However, we don't ship that assembly in Modern, so you are getting runtime errors.

    And yes, from what I can tell we don't ship Xamarin.Forms on mac for full.

    We could consider extending the Modern BCL surface to include this assembly, but that wouldn't help you short term.

    There are other serial libraries available, such as this:

    https://github.com/jcurl/SerialPortStream

    which also might be useful.

  • steviet02steviet02 USMember ✭✭

    @ChrisHamons said:

    nugget System.IO.Ports

    That's your problem. That nuget is very likely this:

    which from what I can tell is a type forwarder for net461:

    monodis packages/System.IO.Ports.4.4.0/ref/net461/System.IO.Ports.dll

    .class extern forwarder System.IO.Ports.SerialPort
    {
    .assembly extern 'System'
    }

    However, we don't ship that assembly in Modern, so you are getting runtime errors.

    And yes, from what I can tell we don't ship Xamarin.Forms on mac for full.

    We could consider extending the Modern BCL surface to include this assembly, but that wouldn't help you short term.

    There are other serial libraries available, such as this:

    which also might be useful.

    Thanks for the help, you're right that was the IO package.. I'll take a look at that library.

Sign In or Register to comment.