function SendData(ComPort: TComPort;LEDInfo,FrmCTRL,Data: String): Integer; var Str: String; w:word; begin .... //这里去掉些无关的 ComPort. ClearBuffer(True,True); ComPort. WriteStr(GetCheckString(Data)); Str := ReadComPort(ComPort); if (str=') or (Str[1] <> #$F1) then //读取协议中定义的应答 begin if str=' then result := 255 else Result := ord(Str[1]); end; end;
function ReadComPort(ComPort: TComPort): String; var dTimeCount: DWORD; iCount: integer; Str: string; begin Result := '; dTimeCount := GetTickCount; repeat iCount := ComPort. ReadStr(Str, maxword); if iCount >= 1 then Break; Application.ProcessMessages; until ((GetTickCount - dTimeCount) div 1000) > WaitSecond; Result := Str; end;
function GetCheck(Str:string): Byte; //求累加和 var ii,icheck: integer; begin icheck := 0; for ii := 1 to Length(Str) do icheck := ord(Str[ii]) +icheck; Result := lo(icheck); end;
function GetCheckString(Str: String): String; begin Result := Str + char(GetCheck(Str)); end;