B
bluemoon986
Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.btnSendClick(Sender: TObject);
var
//传送及接收要用的动态数组声明
ByteSend:array of Byte;
ByteReceive:array of Byte;
CountWORD;
PTByte; //字节指针
i:Integer;
str:string;
begin
try
if not Comm1.PortOpen then
begin
MessageBox(0,'请先打开串口','串口错误',MB_OK);
exit;
end;
//指定欲传送的字节
SetLength(ByteSend,mSend.Lines.Count);
for i:=0 to mSend.Lines.Count-1 do
ByteSend:=StrToInt(trim(mSend.Lines));
Comm1.OutputByte(ByteSend);//串口传送出去
TimeDelay(100); //延迟100毫秒
//接收字节数据,包括数目及地址
Count:=Comm1.ReadInputByte(PT);
//接收的数据指定给接收数组
SetLength(ByteReceive,Count);
for i:=0 to Count do
begin
ByteReceive:=PT^;
//结果显示在Memo组件中(Append方式)
str:=str+inttohex(bytereceive,3) + chr(13)+ chr(10);
mReceive.Text:=trim(str);
Inc(PT);
end;
except
begin
MessageBox(0,'数据传送失败,请检查串口','数据错误',MB_OK);
exit;
end;
end;
end;
var
//传送及接收要用的动态数组声明
ByteSend:array of Byte;
ByteReceive:array of Byte;
CountWORD;
PTByte; //字节指针
i:Integer;
str:string;
begin
try
if not Comm1.PortOpen then
begin
MessageBox(0,'请先打开串口','串口错误',MB_OK);
exit;
end;
//指定欲传送的字节
SetLength(ByteSend,mSend.Lines.Count);
for i:=0 to mSend.Lines.Count-1 do
ByteSend:=StrToInt(trim(mSend.Lines));
Comm1.OutputByte(ByteSend);//串口传送出去
TimeDelay(100); //延迟100毫秒
//接收字节数据,包括数目及地址
Count:=Comm1.ReadInputByte(PT);
//接收的数据指定给接收数组
SetLength(ByteReceive,Count);
for i:=0 to Count do
begin
ByteReceive:=PT^;
//结果显示在Memo组件中(Append方式)
str:=str+inttohex(bytereceive,3) + chr(13)+ chr(10);
mReceive.Text:=trim(str);
Inc(PT);
end;
except
begin
MessageBox(0,'数据传送失败,请检查串口','数据错误',MB_OK);
exit;
end;
end;
end;