procedure TfrmMain.comPLCComm(Sender: TObject);
var
S:String;
S1:String;
begin
Case comPLC.CommEvent of
ComEvReceive: //收到RTHRESHOLD个字符。用INPUT属性移除
begin
if comPLC.RThreshold = 16 then
begin
comPLC.InputLen := 1;
S:=comPLC.Input;
if S[1]=STX then
begin
comPLC.InputLen := 13;
S := comPLC.Input;
comPLC.InputLen := 2;
S1 := comPLC.Input;
if PLCCheckSum(S)=S1 then
begin
if s[8] in ['0','1','2','3','4','5','6','7','8','9'] then
begin
byts[1]:=ord(S[8])-$30;
end
else
begin
byts[1]:=ord(S[8])-$37;
end;
if s[7] in ['0','1','2','3','4','5','6','7','8','9'] then
begin
byts[2]:=ord(S[7])-$30;
end
else
begin
byts[2]:=ord(S[7])-$37;
end;
if s[6] in ['0','1','2','3','4','5','6','7','8','9'] then
begin
byts[3]:=ord(S[6])-$30;
end
else
begin
byts[3]:=ord(S[6])-$37;
end;
if s[5] in ['0','1','2','3','4','5','6','7','8','9'] then
begin
byts[4]:=ord(S[5])-$30;
end
else
begin
byts[4]:=ord(S[5])-$37;
end;
if s[12] in ['0','1','2','3','4','5','6','7','8','9'] then
begin
byts[5]:=ord(S[12])-$30;
end
else
begin
byts[5]:=ord(S[12])-$37;
end;
if s[11] in ['0','1','2','3','4','5','6','7','8','9'] then
begin
byts[6]:=ord(S[11])-$30;
end
else
begin
byts[6]:=ord(S[11])-$37;
end;
if s[10] in ['0','1','2','3','4','5','6','7','8','9'] then
begin
byts[7]:=ord(S[10])-$30;
end
else
begin
byts[7]:=ord(S[10])-$37;
end;
if s[9] in ['0','1','2','3','4','5','6','7','8','9'] then
begin
byts[8]:=ord(S[9])-$30;
end
else
begin
byts[8]:=ord(S[9])-$37;
end;
comPLC.Output:=ACK + '01FF';
isSuccess := true;
end
end
else
begin
comPLC.Output :=NAK+'01FF';
isSuccess := False;
end;
end
else
begin
comPLC.Inputlen := 5;
S := comPLC.Input ;
if S[1]= ACK then
begin
isSuccess := true;
end
else
begin
isSuccess := False;
end;
end;
ReleaseSemaPhore(hEventPLC1,1,nil);
end;
end; // end Case
end;