再问delphi如何编写等待程序? ( 积分: 100 )

  • 主题发起人 主题发起人 panyongze
  • 开始时间 开始时间
P

panyongze

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