简体   繁体   中英

Error with high baud rates using SerialPort on Mono Linux

I have to communicate in Mono with a device through FTDI RS485 connector (which has usual comport software interface) on 1000000 baud rate. OS - Linux (Ubuntu... Mint13 to be particular), mono 2.10.2.

First I run simple python test program on Linux:

 >> import serial
 >> ser = serial.Serial("/dev/ttyUSB0",1000000, timeout=0.5)
 >> ser.write(":DCS3FF8;")   # Some message to the device with crc
 >> ser.readall()
 ':CDS P0 M0 E0 L1 S07B3B;'  #This is the correct response from the device

 >> print ser
 Serial<id=0x2ce71d0, open=True>(port='/dev/ttyUSB0', baudrate=1000000, bytesize=8, parity='N', stopbits=1, timeout=0.5, xonxoff=False, rtscts=False, dsrdtr=False)

The device responds correctly. Everything works fine. This shows that everything is running fine on Linux.

Then I exit python and try to use Mono with code:

     var serialPort = new SerialPort();
     serialPort.PortName = "/dev/ttyUSB0";
     serialPort.BaudRate = 1000000;

     serialPort.Open();
     serialPort.Write(":DCS3FF8;");
     Thread.Sleep(150);
     Console.WriteLine("BytesToRead: '{0}'", serialPort.BytesToRead);
     Console.WriteLine("Existing   : '{0}'", serialPort.ReadExisting());

Compile and run it with command:

     dmcs serial_try.cs && mono serial_try.exe

And get TimeoutException, serialPort.BytesToRead is 0. At the same time I see that the connector is flashing with both lights (something is transferring).

When I run the same code on .NET on Windows , it successfully communicates with the device and receive the same message as with python.

So what is the catch with SerialPort on mono? Do I miss some setting or configuration?

The problem with reading FTDI device on mono was found to be caused by bug with baud rate .

The idea of the bug: If one sets baud rate grater than 921600 the real baud rate is set to 9600.

Funny to say (if you waste your time trying to figure out why you can't communicate with SerialPort on mono), that this is related to an older bug . Where impossibility to set baud rate 921600 was stated. So it looks like the bug was fixed right up to 921600 and not a bit above.

To workaround the bug, one can call stty after port is open to set the real baud rate. So add this function:

    private void ForceSetBaudRate(string portName, int baudRate)
    {
        if (Type.GetType ("Mono.Runtime") == null) return; //It is not mono === not linux! 
        string arg = String.Format("-F {0} speed {1}",portName , baudRate);
        var proc = new Process
            {
                EnableRaisingEvents = false,
                StartInfo = {FileName = @"stty", Arguments = arg}
            };
        proc.Start();
        proc.WaitForExit();
    }

And call right after serial open:

    _serialPort.PortName = PortName;
    _serialPort.BaudRate = BaudRate;
    _serialPort.Open();
    ForceSetBaudRate(PortName, BaudRate);

The hack is not beautiful, but finally FTDI Modem works on Mono + Linux and .Net + Windows.

Other workarounds could be in implementing your own serial port wrapper and PInvoking it or in fixing mono bug as @skolima suggested.

Update if the baud rate is non ASCII so one can't use stty, python could be used as a solution. See comment below. Thanks to @Dorian Scholz.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM