zoukankan      html  css  js  c++  java
  • 如何计算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   微秒;

  • 相关阅读:
    Javal连载4-注释&class与public class区别
    HTML连载21-序选择器上
    Python连载21-collections模块
    Java连载3-编译与运行阶段详解&JRE,JDK,JVM关系
    HTML连载20-并集选择器&兄弟选择器
    Python连载20-偏函数&zip函数&enumerate函数
    Java连载2-Java特性
    HTML连载19-子元素选择器&交集选择器
    [刷题] PTA 7-58 求整数序列中出现次数最多的数
    [刷题] PTA 7-56 找鞍点
  • 原文地址:https://www.cnblogs.com/worldsing/p/3216839.html
Copyright © 2011-2022 走看看