1

I followed the instructions here to disable Bluetooth so that /dev/ttyAMA0 is available, which it is.

I wrote a small Python script to open /dev/ttyAMA0 and send some random data, and I could receive this data just fine on another computer.

Using .NET Framework and Mono, however, was not as successful.

I initially wrote a simple console application which just lists available ports with SerialPort.GetPortNames(). This just displays /dev/ttyS0, which is the Mini UART port with all its limitations.

I tried connecting to /dev/ttyAMA0, even though it doesn't appear in the list, and this seems to work and I can write and read from it. Though, because it doesn't appear in GetPortNames() I can't use some validation code to check that the port name the user has entered actually exists. And I'm sure this will come back to bite me at some point with something else that suddenly doesn't work as expected.

Why can't Mono see /dev/ttyAMA0?

1

If this really is the current code then it would seem that public static string [] GetPortNames () is only probing for /dev/ttyS* or /dev/ttyUSB* as naming schemes:

// Are we on Unix?
if (p == 4 || p == 128 || p == 6) {
    string[] ttys = Directory.GetFiles("/dev/", "tty*");
    bool linux_style = false;

    //
    // Probe for Linux-styled devices: /dev/ttyS* or /dev/ttyUSB*
    // 
    foreach (string dev in ttys) {
        if (dev.StartsWith("/dev/ttyS") || dev.StartsWith("/dev/ttyUSB") || dev.StartsWith("/dev/ttyACM")) {
            linux_style = true;
            break;
        }
    }

    foreach (string dev in ttys) {
        if (linux_style){
            if (dev.StartsWith("/dev/ttyS") || dev.StartsWith("/dev/ttyUSB") || dev.StartsWith("/dev/ttyACM"))
                serial_ports.Add (dev);
        } else {
            if (dev != "/dev/tty" && dev.StartsWith ("/dev/tty") && !dev.StartsWith ("/dev/ttyC"))
                serial_ports.Add (dev);
        }
}

Here is a workaround for that:

The most viable way to implement this method is to copy how mono does it, which is to just find any files in /dev/ that match the pattens ttyS*, ttyUSB*, or ttyACM* (I'll also add 'ttyAMA*' and tty.usb*). The mono implementation already misses ports that start with ttyAMA, so you can see the issue with this method, but it's probably the best method we'll get. Any additional filename patterns can be added as separate bugs later.

Since you already know the ports name though I would guess that using PortName's setter to that end before using open() (which is backed by the edit to the question that it works well to open that port).

  • Thanks for the explanation. I expected GetPortNames() to be a bit more low-level than checking for the existence of /dev/tty* items, but seeing as they're not I can easily create me own version of it. Thanks :) – GTHvidsten Sep 13 '18 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.