串行通信是终端和主机之间的主要通信方式,通信波特率一般选择1800、4800、9600和 19200等。终端的类型有很多种,其通信速率也有很多种选择。主机怎样确定终端的通信速率呢?本文给出了一种简单、易行的方法:设定主机的接收波特率(以9600波特为例),终端发送一个特定的字符(以回车符为例),主机根据接收到的字符信息就可以确定终端的通信波特率。本文对这种方法予以详述。
<p>
1 基本方法
回车符的ASCII值为0x0D。串行通信时附加一个起始位和终止位,位的传输顺序一般是 先传低位再传高位。此时回车符的二进制表示方式为:
<p>
<img src="attachments/dvbbs/2004-11/20041125181324725.gif" border="0" onload="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.alt=\'Click here to open new window\nCTRL+Mouse wheel to zoom in/out\';}" onmouseover="if(this.width>screen.width*0.7) {this.resized=true; this.width=screen.width*0.7; this.style.cursor=\'hand\'; this.alt=\'Click here to open new window\nCTRL+Mouse wheel to zoom in/out\';}" onclick="if(!this.resized) {return true;} else {window.open(\'attachments/dvbbs/2004-11/20041125181324725.gif\');}" onmousewheel="return imgzoom(this);" alt="" />
<p>
<>3 实现方式
通过以上分析,各种波特率都可以通过回车符的发送和接收信息来测定,算法实现的伪 代码在本文的最后给出。应用实践证明了这种方法的有效性。
; Pseudo code to determine what baud rate a transmitter is at,</P>
on the b asis of a single
<p>
; RETURN (0x0D) character received from it.
Initialise receive baud rate to 9600
Wait for Byte to be received
IF Byte = 0x00 THEN
Start Timer
REPEAT
UNTIL (Timer > 50 ms OR New Byte Received)
CASE Timer IN
1 ms-4 ms: