Is there a straight-forward way to communicate with a usb device via a serial port connection?

GavinFGavinF USMember

I'm using Visual Studio for Mac targeting Xamarin.Mac Mobile Framework and I can not find a working solution to communicate with my custom usb device. I have tried using LibUsbDotNet package, and while I can get the devices info, I can't write to it without triggering an exception (MonoApiError:SubmitTransfer Ep 0x02 -5:ErrorNotFound:Unknown error:ErrorNotFound).

I abandoned that and tried another nuget package (PInvokeSerialPort), which didn't even allow me to open the serial connection (System.EntryPointNotFoundException: CreateFile
at (wrapper managed-to-native) PInvokeSerialPort.Win32PInvoke.Win32Com:CreateFile (string,uint,uint,intptr,uint,uint,intptr))

I'm back to trying the LibUsbDotNet implementation, but it's telling me to make a codeless kext which requires either signing with an approved developer ID, or disabling System Integrity Protection.

I'd probably have better luck targeting Xamarin.Mac .Net 4.5 Framework, but when I do that it breaks all of my OpenTK implementation (no longer recognizes MonoMacGameView, System.Drawing, etc).

Is there not a simpler way for sending and receiving a few strings from a simple USB control board? I feel like I have to be missing something obvious, and wanted to ask before I jump off the deep end here. Any info or tips appreciated.

Best Answer

  • GavinFGavinF US
    Accepted Answer

    Ultimately, I gave up on trying to get a usb connection going with LibUsbDotNet when targeting Xamarin.Mac Mobile framework. I was able to get my OpenTK implementation working under Xamarin.Mac .NET 4.5 framework, which then allowed me to use System.IO.Ports.SerialPort.

    If I were to continue trying to get it to work, I think my next step would have been doing a bindings project around Apple's USB HID Class Device Interface and trying to get that going.

    I don't think LibUsbDotNet is being maintained, and it did not seem to be releasing the usb interface correctly on Mac, and would in fact crash my entire system every second time I ran my code which was based on their samples.

Answers

  • GavinFGavinF USMember

    Forgot to mention, I am attempting to make a Mac desktop application.

  • JGoldbergerJGoldberger USMember, Forum Administrator, Xamarin Team, University Xamurai

    This is more of a Xamarin Mac issue than an issue with the IDE, so moving to Xamarin.Mac forum.

  • GavinFGavinF USMember
    Accepted Answer

    Ultimately, I gave up on trying to get a usb connection going with LibUsbDotNet when targeting Xamarin.Mac Mobile framework. I was able to get my OpenTK implementation working under Xamarin.Mac .NET 4.5 framework, which then allowed me to use System.IO.Ports.SerialPort.

    If I were to continue trying to get it to work, I think my next step would have been doing a bindings project around Apple's USB HID Class Device Interface and trying to get that going.

    I don't think LibUsbDotNet is being maintained, and it did not seem to be releasing the usb interface correctly on Mac, and would in fact crash my entire system every second time I ran my code which was based on their samples.

  • intvsteveintvsteve USMember ✭✭

    @GavinF : Are you using a USB<->serial device? I've written a desktop application that does serial communication based around a FTDI USB<->Serial chipset and basic communication is not too difficult. However, there have been plenty of challenges along the way - including kernel panics in the drivers unrelated to application software!

    The standard System.IO.Ports.SerialPort class can work fine with "standard" serial communication. In my case, however, a few challenges arose. For example, the device my app communicates with uses a baud rate of 2000000 -- which isn't supported in the bog-standard C API on Mac. Some p/invoke magic can get that to work. We also wanted the appliction to react to device being plugged in into or unplugged from the system.

    For better port identification and device arrival and departure, you can create bindings to IOKit. It's a bit laborious, but can be done. My work on this project started in MonoMac back in early 2014 -- so I've not learned yet about binding projects. In fact, only late last year did I finally port it to Xamarin.Mac! I've not looked into LibUsbDotNet.

    You mention that your entire system would crash -- I'm curious if you encountered kernel panics? Again, for the project I've been working on, we need to be very careful about correctly managing the serial port. The FTDI serial drivers have been a bit unstable unless we were extremely cautious with device arrival and departure and closing the serial port. In fact, merely plugging in a device and unplugging it -- with no user software even running - could induce a kernel panic! In addition, the problem seems to be OS-version-sensitive as well.

    It sounds like relying on the "standard" SerialPort class in Mono is working -- which is probably for the best. :)

Sign In or Register to comment.