P
panyongze
Unregistered / Unconfirmed
GUEST, unregistred user!
我正在编写串口通讯程序,为保证数据完整对方先发送一个字节(要发送的字节数),然后接着发送约定好的字节数,我的程序读取第一个字节后就要进入等待状态,等一定时间后计算收到的字节数如果与约定的字节数一致进入下一个环节,否则发送一个字节(00H)要求重新发送。<br>在一个过程体中如何等待?使用sleep();或GetTickCount都未成功(且占用cpu时间),各位老大指条明路啊! 下面是我的简化的通讯类程序.<br>unit SjtxUnit;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, SPComm;<br><br>type<br><br> TSjtx = class (TObject)<br> private<br><br> FComm: TComm; //串口对象<br> FCommName: string; //窜口名称<br> FBaudRate: Integer; //通讯数率<br><br> FReadStatus: Boolean; //读取状态(True正在读取;False空闲)<br> FReadString: string; //读取的字符串<br><br> FDataLength: Integer;<br> procedure SetBaudRate(const Value: Integer);<br> procedure SetCommName(const Value: string); //准备接收的数据长度<br><br> procedure ReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word);<br> function SendData(SendStr: string): Boolean; //发送数据<br> public<br> constructor Create;<br> destructor Destroy; override;<br><br> property ReadString: string read FReadString;<br> property CommName: string read FCommName write SetCommName;<br> property BaudRate: Integer read FBaudRate write SetBaudRate;<br> property ReadStatus: Boolean read FReadStatus;<br><br> //这是我要实现但失败的函数<br> function ReadData(var S: string): Boolean;<br> end;<br><br><br>implementation<br><br>{ TSjtx }<br><br>constructor TSjtx.Create;<br>begin<br> FComm := TComm.Create(nil);<br> CommName := 'COM1';<br> BaudRate := 2400;<br> with FComm do<br> begin<br> Inx_XonXoffFlow := False;<br> Outx_XonXoffFlow := False;<br> StartComm;<br> OnReceiveData := ReceiveData;<br> end;<br>end;<br><br>destructor TSjtx.Destroy;<br>begin<br> FComm.StopComm;<br> FComm.Free;<br> inherited;<br>end;<br><br>function TSjtx.ReadData(var S: string): Boolean;<br>begin<br><br><br><br><br> //清除FReadString的内容。<br> FReadString :='';<br> //发送请求数据命令,pda收到后发送回64字节数据<br> SendData('0203');<br> //等待10秒后,检查FReadString的长度如果是64字节说明正常,如果FReadString为<br> //空则重新发送SendData('0203'),如果长度&lt;&gt;64字节则发送重发命令SendData('00');<br> //以上重复3次后如果仍无法获得正确数据应返回 Result := False;如果长度正确,<br> //则返回 Result := True; 并且 S := FReadString;<br><br><br><br><br>end;<br><br>procedure TSjtx.ReceiveData(Sender: TObject; Buffer: Pointer;<br> BufferLength: Word);<br>var<br> I: Integer;<br> FBuf: array [1..64] of Byte; //PDA发送的数据<br>begin<br> FDataLength := BufferLength;<br> Move(Buffer^, FBuf,BufferLength);<br> FReadString := '';<br> for I := 1 to FDataLength do<br> FReadString := FReadString + IntToHex(FBuf, 2);<br>end;<br><br>function TSjtx.SendData(SendStr: string): Boolean;<br>var<br> sBuf: array [1..64] of Byte;<br> I, J: Integer;<br>begin<br> J := Length(SendStr) div 2;<br> if J &gt; 0 then<br> begin<br> for I := 1 to J do<br> sBuf := StrToInt('$' + (Copy(SendStr, I * 2 - 1, 2)));<br> Result := FComm.WriteCommData(@sBuf, J);<br> end<br> else<br> Result := False;<br>end;<br><br>procedure TSjtx.SetBaudRate(const Value: Integer);<br>begin<br> if FBaudRate &lt;&gt; Value then<br> begin<br> FBaudRate := Value;<br> FComm.BaudRate := Value;<br> end;<br>end;<br><br>procedure TSjtx.SetCommName(const Value: string);<br>begin<br> if FCommName &lt;&gt; Value then<br> begin<br> FCommName := Value;<br> FComm.CommName := Value;<br> end;<br>end;<br><br>end.<br>