如何计算ModBus超时时间?

时间:2023-03-09 15:03:34
如何计算ModBus超时时间?

波特率:每秒钟通过信道传输的信息量称为位传输速率,也就是每秒钟传送的二进制位数,简称比特率比特率表示有效数据的传输速率,用b/s 、bit/s、比特/秒,读作:比特每秒。

如9600b/s:指总线上每秒可以传输9600个bit;

通常的串口桢格式为:开始位1bit + 数据位8bit + 停止位1bit

也就是说:在9600的波特率下,每秒可以传输出的桢数为:9600 / (1 + 8 + 1) = 960桢/秒,即960字节/秒;

反推:一桢或一字节所需要的时间是多少呢?

1秒 / 960 = 1.4ms

而ModBus协议中超时时间定为:3.5个桢长度为超时时间;

超时时间 = 3.5 * 1 / BaudRate / 10              秒

= 3.5 * 10 / BaudRate                   秒

= 3.5 * 10  * 2 / BaudRate  *2        秒

=  70 / BaudRate  *2                     秒

FreeModBus是这个样实现的:

   1: /* If baudrate > 19200 then we should use the fixed timer values

   2:  * t35 = 1750us. Otherwise t35 must be 3.5 times the character time.

   3:  */

   4: if( ulBaudRate > 19200 )

   5: {

   6:     usTimerT35_50us = 35;       /* 1800us. */

   7: }

   8: else

   9: {

  10:     /* The timer reload value for a character is given by:

  11:      *

  12:      * ChTimeValue = Ticks_per_1s / ( Baudrate / 11 )

  13:      *             = 11 * Ticks_per_1s / Baudrate

  14:      *             = 220000 / Baudrate

  15:      * The reload for t3.5 is 1.5 times this value and similary

  16:      * for t3.5.

  17:      */

  18:     usTimerT35_50us = ( 7UL * 220000UL ) / ( 2UL * ulBaudRate );

  19: }

波特率大于19200使用定值:1750us

波特率小于19200使用定值:usTimerT35_50us = ( 7UL * 220000UL ) / ( 2UL * ulBaudRate ); 这usTimerT35_50us 一个单位为50uS,将这个计算结果写到定时器。每中断一次为50us * usTimerT35_50us   微秒;