简体   繁体   English

RS-485串行端口波特率性能效率

[英]RS-485 Serial Port Baud Rate Performance Efficiency

I am developing an application in Python that communicates to a device over RS-485 two wire, half-duplex. 我正在用Python开发一个应用程序,该应用程序通过RS-485两线半双工与设备通信。 I have enough of the application working that I can perform some performance tests. 我有足够的应用程序工作,可以执行一些性能测试。 I am using a laptop with a USB to 485 converter. 我正在使用带USB到485转换器的笔记本电脑。 The communications is setup as 9600,N,8,1. 通讯设置为9600,N,8,1。

For my speed test I send a message with a total length of 10 bytes including the check byte. 为了进行速度测试,我发送了一条消息,该消息的总长度为10个字节,其中包括校验字节。 Then I wait for the reply of 13 bytes. 然后,我等待13个字节的答复。 I decode the reply as it is coming in. When the response is complete. 我对收到的答复进行解码。响应完成后。 I then send the next message. 然后,我发送下一条消息。 I repeat this 100 times as fast as possible. 我会尽快重复此操作100次。 This takes 2.895 seconds. 这需要2.895秒。

From this I calculate that I am transmitting/receiving 23 bytes * 100 iterations / 2.895 seconds = 794 bytes/s. 据此,我计算出我正在发送/接收23个字节* 100次迭代/ 2.895秒= 79​​4个字节/秒。

If I understand it correctly serial port communication of 9600 N-8-1 has 1 start bit, 8 data bits and 1 stop bit. 如果我理解正确,9600 N-8-1的串行端口通信具有1个起始位,8个数据位和1个停止位。 This means that it has a 2 bit overhead. 这意味着它有2位的开销。 So the actual theoretical transmission rate is (9600 bits / s) * (8 data bits / 10 transmission bits) * (1 Byte / 8 bits) = 960 bytes / s. 因此,实际的理论传输速率为(9600位/ s)*(8个数据位/ 10个传输位)*(1字节/ 8位)= 960字节/ s。

My program is transmitting/receiving at a combined rate of 794 bytes/s out of a possible 960 bytes / s = 82.7%. 我的程序正在以794字节/秒的组合速率发送/接收,可能是960字节/秒= 82.7%。

Should I be able to achieve near 100% of the 960 bytes/s. 我是否应该能够达到960字节/秒的接近100%的速度。 Or is it typical to have this much bandwidth un-utilized? 还是有这么多带宽未被利用是典型的?

You're going to give up some time when the direction of communication is reversed. 当通信方向相反时,您将放弃一些时间。 So there's some "dead time" between when one side receives the last stop bit and when it loads the first response byte into the UART transmitter and starts driving the first start bit. 因此,当一侧接收到最后一个停止位与将第一个响应字节加载到UART发送器并开始驱动第一个起始位之间有一段“死时间”。

I'm calculating that this dead time is 5 ms (almost 5 bit times, ie half a byte counting framing overhead) per two-way run, or 0.495 seconds of your 2.895 total seconds. 我计算得出,每次双向运行的停滞时间为5毫秒(几乎5比特时间,即计算帧开销的半个字节),即2.895总秒数的0.495秒。 This isn't bad, but it could be a little better. 这还不错,但可能会更好。 However, I'm not sure you'll get much improvement without writing your own UART driver. 但是,我不确定如果不编写自己的UART驱动程序,您是否会得到很大的改进。

(This all assumes, of course, that the clocks both computers are using are crystal accurate. This isn't always true, since UARTs at 8N1 can tolerate up to about 2% clock difference between each end.) (当然,所有这些都假设两台计算机使用的时钟都非常精确。这并不总是正确的,因为在8N1的UART可以容忍两端之间大约2%的时钟差。)

In embedded land, if we wanted to do this with absolute minimum bandwidth loss, we'd write the driver as a standard two-way full-duplex driver, with some way to know when to switch directions (eg on packet bounds). 在嵌入式领域,如果我们想在绝对最小的带宽损耗下做到这一点,我们会将驱动程序编写为标准的双向全双工驱动程序,并通过某种方式知道何时切换方向(例如,在数据包边界上)。 This driver would then only push bytes in the correct direction, leaving the other queue unused. 然后,该驱动程序将仅按正确方向推送字节,而其他队列未使用。

At the user (application) level, we'd have to make sure those queues were never starved. 在用户(应用程序)级别,我们必须确保这些队列永远不会饿死。 That means that, in your example, the 13-byte response packet would need to be ready to go before the 10-byte incoming packet is fully received. 这意味着,在您的示例中,在完全接收10字节的传入数据包之前 ,需要准备好13字节的响应数据包。 The other end would need to do the same. 另一端也需要这样做。

With larger machines, the usual practice is to "coalesce" several packets in each direction and transmit them consecutively, so as to minimize the number of times that you have to change direction. 对于大型机器,通常的做法是在每个方向上“突显”多个数据包并连续传输,以最大程度地减少更改方向的次数。 This increases latency, though, and requires more memory, which could be a problem with a small microcontroller with only a couple kB of RAM. 但是,这会增加延迟,并且需要更多的内存,这对于只有几KB RAM的小型微控制器可能是个问题。

IMHO, given your small packets, frequent direction reversals, and lack of driver-level optimizations, that bandwidth looks about right. 恕我直言,考虑到您的小数据包,频繁的方向反转以及缺乏驱动程序级别的优化,带宽看起来是正确的。

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

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