delphi7 spcomm开发串口调试助手详细步骤

2025-03-11 08:51:43
推荐回答(2个)
回答1:

function TForm1.StartComm: boolean;
begin
comm.CommPort := 端口号;
comm.BaudRate := 波特率;
//comm.ByteSize := 数据位;
//comm.Parity := 检验位;
//comm.StopBits := 停止位;
Comm.InBufferSize:=1024;
Comm.InputLen:=1024;
Comm.ReadIntervalTimeout:=100;
Comm.ReadTotalTimeoutMultiplier:=0;
Comm.ReadTotalTimeoutConstant:=0;
Comm.ReplacedChar:=#0;
Comm.XoffChar:=#19;
Comm.XoffLimit:=500;
Comm.XonChar:=#17;
Comm.XonLimit:=500;
comm.StartComm;
comm.PortOpen:=true;
if comm.PortOpen then
begin
Result := True;
end
else
begin
Result := false;
end;
end;

procedure TForm1.StopComm;
begin
Comm.StopComm;
end;

procedure TForm1.ReceiveData(Sender: TObject; Buffer: PAnsiChar; BufferLength: Word);
var
ByteArr: TByteArrData;
// 上面定义 type TByteArrData = array[0..1023] of Byte;
Pstr:string;
begin
Move(Buffer^, ByteArr, BufferLength); //接收数据写入ByteArr数组

Pstr:=GetProtocolHexStr(ByteArr,BufferLength); //转为16进制字符串
Memo1.lines.add(Pstr); //显示

end;

function GetHexStr(ByteArr: TByteArrData;Len:integer):String;
var
I, J: Integer;
Str: string;
begin
for I := 1 to Len do
begin
J := ByteArr[I - 1];
Str := Str + IntToHex(J, 2) + ' ';
end;
Result:=Str;
end;

回答2:

网上例子一大堆