delphi如何用串口通信发送一个类的数据?
我定义一个类:
type
ttt:=record
s1:integer;
s2:byte;
s3:string[10];
s4:string[20];
a1:double;
a2:double;
定义一个子类
SSS:ttt;
sss.s1:=12;
sss.s2:=7;
sss.s3:='123456';
sss.s4:='987654321'; sss.a1:=100;
sss.a2:=200;
comm1.WriteCommData(pchar(@sss),n);
请问 我的发送的字符串的长度 n 该定义为多少啊 ,,,?
[解决办法]
n:=sizeof(ttt);
[解决办法]
序列化为XML,到达后反序列化......
[解决办法]
StrMove(@test, @HeadArray, 6); 注意这个函数,以下代码是Socket读取 自定义结构 转换的。串口是也是一样的楼主自己改改吧。
type
UserInfo = record
IP: array[0..9] of char;
AGE: array[0..149] of char;
Port: integer;
cast1: array[0..49] of char;
cast2: array[0..49] of char;
cast3: array[0..49] of char;
cast4: array[0..49] of char;
end;
tstas = record
resulttype: byte;
result: byte;
resultlength: array[0..3] of byte;
end;
var
Test: tstas;
bbb: UserInfo;
DataLength, TmpLength, cc: integer;
vyte: byte;
tmparray1: array[0..370] of byte;
GGG: TAnalysisPackThread;
begin
try
DataLength := Socket.ReceiveBuf(Pointer(nil)^, -1);
if DataLength > 0 then Memo1.Clear;
while DataLength > 0 do
begin
FillChar(tmparray1, 370, #0);
FillChar(HeadArray, 6, #0);
if Socket.ReceiveBuf(HeadArray, 6) <= 0 then // <= 0 then
Break;
StrMove(@test, @HeadArray, 6);
Move(HeadArray, tmparray1, 6);
Memo1.Lines.Add(inttostr(Test.resulttype));
Memo1.Lines.Add(inttostr(Test.result));
TmpLength := integer(test.resultlength);
Memo1.Lines.Add(inttostr(TmpLength));
FillChar(TmpArray, 8192, #0);
TmpLength := Socket.ReceiveBuf(TmpArray, TmpLength + 1);
if TmpLength <= 0 then
Break;
StrMove(@bbb, @TmpArray, TmpLength);
Move(TmpArray, tmparray1[6], TmpLength);
vyte := Crc_8n(tmparray1, TmpLength + 5);
if vyte = tmparray1[TmpLength + 5] then rightcount := rightcount + 1
else errorcount := errorcount + 1;
Memo1.Lines.Add(bbb.IP);
Memo1.Lines.Add(bbb.AGE);
Memo1.Lines.Add(IntToStr(bbb.Port));
Memo1.Lines.Add(bbb.cast1);
Memo1.Lines.Add(bbb.cast2);
Memo1.Lines.Add(bbb.cast3);
Memo1.Lines.Add(bbb.cast4);
DataLength := DataLength - TmpLength - 6;
Memo1.Lines.Add('==================================================');
Memo1.Lines.Add('==================' + inttostr(i) + '==========================');
Memo1.Lines.Add('==================================================');
inc(i);
end;
except
end;
end;
[解决办法]