波特率:每秒鐘經過信道傳輸的信息量稱爲位傳輸速率,也就是每秒鐘傳送的二進制位數,簡稱比特率。比特率表示有效數據的傳輸速率,用b/s 、bit/s、比特/秒,讀做:比特每秒。this
如9600b/s:指總線上每秒能夠傳輸9600個bit;spa
一般的串口楨格式爲:開始位1bit + 數據位8bit + 中止位1bitcode
也就是說:在9600的波特率下,每秒能夠傳輸出的楨數爲:9600 / (1 + 8 + 1) = 960楨/秒,即960字節/秒;htm
反推:一楨或一字節所須要的時間是多少呢?blog
1秒 / 960 = 1.4msget
而ModBus協議中超時時間定爲:3.5個楨長度爲超時時間;it
超時時間 = 3.5 * 1 / BaudRate / 10 秒class
= 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 微秒;