I
i_single
Unregistered / Unconfirmed
GUEST, unregistred user!
procedure Tfrm_com.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
buf:array of byte;
begin
setlength(buf,bufferlength);
move(buffer,buf,bufferlength);//数据少时(少于几百字节),正常,捎大一点就出错.
//无论怎样都出错,move(buffer^,pchar(@buf)^,bufferlength);.....
for i:=0 to bufferlength-1 do
memo1.lines.add(inttohex(buf,2));--->出错.
end;
改成这样:
procedure Tfrm_com.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
achar;
begin
a:=pchar(buffer);
memo1.lines.add(string(a));//无论buffer多大,一切正常.
end;
可按16进制发送CC 04 01 00 01 01 02 00 00 03 05 00 01 03 04 DD时
接收的数据经这样处理:
for i:=1 to 18 do
showmessage(inttohex(ord(memo1.text),2));
居然是这样:
CC 04 01 0D 0A 00 00 00 16 00 00 00 F8 00 41 00 DC 02
我想这是由于pchar转型造成00丢失的。
可又没有什么好办法.
BufferLength: Word);
var
i:integer;
buf:array of byte;
begin
setlength(buf,bufferlength);
move(buffer,buf,bufferlength);//数据少时(少于几百字节),正常,捎大一点就出错.
//无论怎样都出错,move(buffer^,pchar(@buf)^,bufferlength);.....
for i:=0 to bufferlength-1 do
memo1.lines.add(inttohex(buf,2));--->出错.
end;
改成这样:
procedure Tfrm_com.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
achar;
begin
a:=pchar(buffer);
memo1.lines.add(string(a));//无论buffer多大,一切正常.
end;
可按16进制发送CC 04 01 00 01 01 02 00 00 03 05 00 01 03 04 DD时
接收的数据经这样处理:
for i:=1 to 18 do
showmessage(inttohex(ord(memo1.text),2));
居然是这样:
CC 04 01 0D 0A 00 00 00 16 00 00 00 F8 00 41 00 DC 02
我想这是由于pchar转型造成00丢失的。
可又没有什么好办法.