简体   繁体   English

.NET SerialPort在115200 bps时是否可靠?

[英]Is .NET SerialPort reliable at 115200 bps?

I need to interface a device via RS232 at 115200 bps. 我需要通过RS232以115200 bps连接设备。 The distance is very short (less than a meter). 距离很短(不到一米)。 No hardware or software handshaking. 没有硬件或软件握手。 Messages exchanged are also short: 10 - 50 bytes. 交换的消息也很短:10-50个字节。 The frequency of message exchange - max 10 per second. 消息交换的频率-每秒最多10次。

I used to reliably handle comms in such environment in C/C++ using corresponding Win APIs. 我曾经使用相应的Win API在C / C ++中可靠地处理这种环境中的通讯。 However, this time I'd like to do it in .NET/C#. 但是,这次我想在.NET / C#中执行此操作。

Some time ago I came across an article dealing with .NET serial port. 前段时间,我遇到了一篇有关.NET串行端口的文章。 From what I recall, the author did not recommend using SerialPort class. 我记得,作者不建议使用SerialPort类。 Instead he opted for unmanaged Win APIs from a managed code. 相反,他从托管代码中选择了非托管Win API。 I'm not sure if the article referred to .NET 1.1. 我不确定本文是否提到.NET 1.1。

Does anybody know if going managed with SerialPort is a safe bet? 有人知道用SerialPort进行管理是否安全吗?

SerialPort works fine. SerialPort正常工作。 It arguably could have been implemented better, but 10 messages per second of 50 bytes each at 115k is a trivial workload for a modern PC (for comparison bear in mind that PCs are capable of receiving data off a 10Gigabit Ethernet link). 可以说它可以实现得更好,但是对于现代PC而言,每秒115条消息(每条50字节)的10条消息是微不足道的工作量(为便于比较,请记住PC能够通过10G以太网链路接收数据)。 Under the bonnet, the OS/driver buffers the data, so whether you read that buffer from C++ or C# won't make much (if any) difference. 在引擎盖下,OS /驱动程序将缓冲数据,因此无论您是从C ++还是C#读取该缓冲区,都不会有太大的不同(如果有的话)。

SerialPort doesn't work well with USB serial ports.... but that's because USB is notoriously unreliable. SerialPort在USB串行端口上不能很好地工作...。但这是因为众所周知,USB不可靠。 Plug in a PCI serial card or connect over ethernet to a Terminal server and SerialPort works fine. 插入PCI串行卡或通过以太网连接到终端服务器,然后SerialPort可以正常工作。 Use the USB-to-RS232 converter from a C++ program and the failures return. 使用C ++程序中的USB到RS232转换器,然后返回错误。 Draw your own conclusions... 得出自己的结论...

We run systems with 18 serial ports running at various speeds (several at 115k and 10hz, packets of several hundred bytes), and have never had any problems with SerialPort (only with USB-to-RS232 converters!) 我们运行的系统具有18个以各种速度运行的串行端口(分别以115k和10hz的速度运行,数百个字节的数据包),而SerialPort从来没有任何问题(仅适用于USB-to-RS232转换器!)

Give it a try... it shouldn't take long to prototype... 尝试一下...原型应该花很长时间...

It's worth noting that the important point here is not whether the code or the computer are ok to work at 115kbps, but rather whether your cables, the serial hardware, the external device, and the environment are ok to work at those speeds. 值得注意的是,这里的重点不是代码或计算机是否可以以115kbps的速度工作,而是电缆,串行硬件,外部设备和环境是否可以以这些速度工作。

If you are in an electrically quiet location with good, shielded cable over a short run then you're probably fine. 如果您在电气安静的地方使用短时间的屏蔽电缆,那可能很好。 If you are working near high-powered industrial machines that generate a lot of electrical noise and have shoddy cables then you most certainly will not be fine. 如果在高功率工业机器附近工作,这些机器会产生大量电子噪声并且电缆质量不佳,那么您肯定会不满意。 I have definitely experienced problems in electrically noisy industrial environments with high speed RS-232 communications. 在高速RS-232通讯的电气嘈杂的工业环境中,我肯定遇到了问题。 In these cases it has been necessary to use high quality cables and to reduce the transfer rate until the communication becomes stable and error-free. 在这些情况下,必须使用高质量的电缆并降低传输速率,直到通信变得稳定且没有错误。 If this is a concern, there is nothing to do but test it yourself and see. 如果这是一个问题,除了自己进行测试然后看看,别无选择。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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