Mscomm is a powerful serial communication control for Microsoft. Its powerful and simple features are not left now, and we can also use it in delphi. The following codes are processed in hexadecimal for sending and receiving.
var
senddata:array[1..10] of char;
reData:array of Variant;
sendstr:string;
restr:string;
i:longint;
Begin
mscomm1.CommPort := 1; //Specify port
mscomm1.Settings := '9600,N,8,1'; //Other parameters
mscomm1.InBufferSize := 1024; //Receive buffer
mscomm1.OutBufferSize := 1024; //Send buffer
mscomm1.InputMode := comInputModeBinary; //Receive mode
mscomm1.InputLen := 0; //Read all data at once
mscomm1.SThreshold := 0; //Send all data at once
mscomm1.InBufferCount := 0; //Clear the read buffer
mscomm1.OutBufferCount := 0; //Clear the send buffer
mscomm1.PortOpen:=true; //Open port
MSComm1.RThreshold := 16;//Set how many bytes to receive to generate oncomm event
senddata[1]:=chr($06); //The data to be sent
senddata[2]:=chr($03);
senddata[3]:=chr($00);
senddata[4]:=chr($03);
senddata[5]:=chr($10);
sendstr:='';
for i:=1 to 5 do
sendstr:=sendstr + senddata[i];
mscomm1.output:=sendstr; //Send data
i:=0;
bzw:=false;
repeat
sleep(10);
application.PRocessMessages;
i := i + 1;
If i > 30000 Then
Begin
showmessage('Send timeout!');
break;
end;
Until bzw = true;
redata:=mscomm1.Input; Receive data
restr:='';
for i:=0 to vararrayhighbound(redata,1) do
restr:=restr + inttohex(redata[i],2)+' ';
mscomm1.PortOpen:=false;
flatmemo1.Text:=restr;
end;
//oncomm event
procedure TForm1.MSComm1Comm(Sender: TObject);
Begin
case mscomm1.CommEvent of
comEvReceive: bzw := true;
end;
end;
Finally, when releasing the software, you must bring the Mscomm32.ocx file.