delphi串口收到了16进制数据怎样处理

 我来答
龙氏风采
2017-11-16 · 知道合伙人互联网行家
龙氏风采
知道合伙人互联网行家
采纳数:5849 获赞数:12817
从事互联网运营推广,5年以上互联网运营推广经验,丰富的实战经

向TA提问 私信TA
展开全部
实例:ApdDataPacket的OnPacket(16进制收发)或OnStringPacket(字符串收发)过程 PC和数据采集器通信,收发数据为16进制实例代码:(里面的steam没有用到,没有删除而已)
unit RS485_HEX;
interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, OoMisc, AdPort, Vcl.StdCtrls, AdPacket,
SPComm;

type
TForm1 = class(TForm)
Memo1: TMemo;
Button1: TButton;
Button2: TButton;
Edit1: TEdit;
ApdComPort1: TApdComPort;
ApdDataPacket1: TApdDataPacket;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure OnReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure SendData(myarray: array of byte);
procedure Comm1ModemStateChange(Sender: TObject; ModemEvent: Cardinal);
procedure FormDestroy(Sender: TObject);
procedure ApdDataPacket1Packet(Sender: TObject; Data: Pointer;
Size: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
SenStream: TStream; // 一个流对象
RevStream: TStream;
Msg: array of byte; // 发送
rbuf: array of byte;
viewstring: string;

implementation

{$R *.dfm}

procedure TForm1.ApdDataPacket1Packet(Sender: TObject; Data: Pointer;
Size: Integer);
var
i,k:integer;
PointToData:PByte;
str:string;
begin
Memo1.Lines.Add('ApdDataPacket1Packet '+ '数据大小为:'+ inttostr(Size));
PointToData:=pByte(Data);
for k := 0 to Size-1 do
str := str + IntToHex(PointToData[k], 2) + ' ';
memo1.Lines.Add(str) ;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Msg1: array of byte;
RevMsg: array [0 .. 7] of byte;
myMsg, sendMsg: string;
i,j: Integer;
begin
SetLength(Msg1, 8);

Msg1[0] := $01;
Msg1[1] := $04;
Msg1[2] := $00;
Msg1[3] := $42;
Msg1[4] := $00;
Msg1[5] := $01;
Msg1[6] := $91;
Msg1[7] := $DE;

RevMsg[0]:=$01; //接收到的数据的帧头
RevMsg[1]:=$04;
RevMsg[2]:=$02;

ApdDataPacket1.StartString:= strpas(Pansichar(@RevMsg[0])); //收到数据的帧头
ApdDataPacket1.PacketSize:=7; //数据的大小

ApdDataPacket1.Enabled:=true;

for i := 0 to 7 do
begin
ApdComPort1.PutChar(ansichar(Msg1[i]));
end;
//ApdComPort1.Open:=false;
/// memo1.Lines.Add(inttostr(comm1.GetModemState));
// 01 04 00 42 00 01 91 DE
// self.SendData(Msg);
end;

procedure TForm1.OnReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
k: Integer;
begin
ShowMessage('收到数据');
viewstring := '';
SetLength(rbuf, BufferLength);
move(Buffer^, pchar(rbuf)^, BufferLength);
for k := 0 to BufferLength do
viewstring := viewstring + inttohex(rbuf[k], 2) + ' ';
viewstring := '接收' + viewstring;
Memo1.Lines.Add(viewstring);
Memo1.Lines.Add(' ');
end;
procedure TForm1.Comm1ModemStateChange(Sender: TObject; ModemEvent: Cardinal);
begin
Memo1.Lines.Add(inttostr(ModemEvent));
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
// Comm1.StartComm;
ApdComPort1.Open:=true;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
ApdComPort1.Open:=false;
end;
procedure TForm1.SendData(myarray: array of byte);
var
j: Integer;
commflag: Boolean;
begin
viewstring := '';
commflag := true;
viewstring := '发送' + viewstring;
Form1.Memo1.Lines.Add(viewstring);
Form1.Memo1.Lines.Add(' ');
if not commflag then
MessageDlg('发送失败', mterror, [mbyes], 0);

end;

end.
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式