[英]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. 我必须以1000000波特率通过FTDI RS485连接器(具有通常的comport软件接口)在Mono中与设备进行通信。 OS - Linux (Ubuntu... Mint13 to be particular), mono 2.10.2. 操作系统-Linux(特别是Ubuntu ... Mint13),单声道2.10.2。
First I run simple python test program on Linux: 首先,我在Linux上运行简单的python测试程序:
>> 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. 这表明Linux上一切正常。
Then I exit python and try to use Mono with code: 然后我退出python并尝试将Mono与代码一起使用:
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). 并获得TimeoutException,serialPort.BytesToRead为0。与此同时,我看到连接器同时闪烁两个指示灯(正在传输某些东西)。
When I run the same code on .NET on Windows , it successfully communicates with the device and receive the same message as with python. 当我在Windows的.NET上运行相同的代码时 ,它成功与设备通信并收到与python相同的消息。
So what is the catch with SerialPort on mono? 那么在Mono上SerialPort有什么收获呢? 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 . 发现在单声道上读取FTDI设备的问题是由波特率错误引起的。
The idea of the bug: If one sets baud rate grater than 921600 the real baud rate is set to 9600. 错误的想法:如果设置的波特率大于921600,则实际波特率设置为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 . 有趣的是(如果您浪费时间试图找出为什么无法与Mono上的SerialPort通信的原因),这与一个较旧的bug有关 。 Where impossibility to set baud rate 921600 was stated. 指出无法设置波特率921600。 So it looks like the bug was fixed right up to 921600 and not a bit above. 因此,该错误似乎已修复到921600,而上方没有修复。
To workaround the bug, one can call stty after port is open to set the real baud rate. 要解决该错误,可以在端口打开后调用stty来设置实际波特率。 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. hack并不是很漂亮,但是最终FTDI Modem可以在Mono + Linux和.Net + Windows上运行。
Other workarounds could be in implementing your own serial port wrapper and PInvoking it or in fixing mono bug as @skolima suggested. 其他解决方法可能是实现自己的串行端口包装程序并P调用它,或者按照@skolima建议修复单色错误。
Update if the baud rate is non ASCII so one can't use stty, python could be used as a solution. 如果波特率不是ASCII,则更新 ,这样就不能使用stty,可以将python用作解决方案。 See comment below. 请参阅下面的评论。 Thanks to @Dorian Scholz. 感谢@Dorian Scholz。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.