发送简单的数据可以参照楼上的,发送复杂的数据可以参照我的:
type
TDateRec = packed record
StartCode: Smallint; //开始码,整型,2字节
DateTypeCode: Smallint; //数据类别代码,整型,2字节
RecordLength: Smallint; //记录长度,整型,2字节
EquipmentCode: array [0..29] of char; //设备代码,字符串,30字节,以0结尾
AimCode: array [0..9] of char; //目标代码,字符串,10字节,以0结尾
SerialNumber:Longint; //序号,整型,4字节
DateType: Shortint; //数据类别,整型,1字节
Validity: Smallint; //有效性标识,整型,2字节
Hour: Shortint; //时,整型,1字节
Minute: Shortint; //分,整型,1字节
Second: Shortint; //秒,整型,1字节
Millisecond: Smallint; //毫秒,整型,2字节
Longitude: Longint; //经度,整型,4字节
Latitude: Longint; //纬度,整型,4字节
Azimuth: Smallint; //方位,整型,2字节
Course: Smallint; //航向,整型,2字节
Speed: Smallint; //航速,整型,2字节
Warp: Smallint; //脱靶量,整型,2字节
X: Longint; //X,整型,4字节
Y: Longint; //Y,整型,4字节
Z: Word;//smallint; //Z,整型,2 节
EndCode: Smallint; //结束码,整型,2字节
end;
发送:
begin
with SendData do
begin
StartCode := $7FFE;
DateTypeCode := 1;
RecordLength := 82;
EquipmentCode := 'abcdefghijklmnopqr';
AimCode := '56789m';
SerialNumber := SerialNumber+1;
DateType := 1;
Validity := 9;
Hour := 10;
Minute := 30;
Second := 20;
Millisecond := 20000;
Longitude := 117;
Latitude := 23;
Azimuth := 12;
Course := 270;
Speed := 18;
Warp := 100;
X := X+1;//1000;
Y := 2000;
Z := 65456;
EndCode := $7FFF;
end;
comm1.writecommdata(@SendData,sizeof(SendData)) ;
接收:
procedure TFCOMM.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
ReceiveData:TDateRec;
p
byte;
begin
memo1.Clear;
p:= pbyte(Buffer);
with ReceiveData do
begin
Move(p^,StartCode,2);
memo1.lines.add('StartCode:'+inttostr(StartCode));
Inc(p);
Inc(p);
Move(p^, DateTypeCode, 2);
memo1.lines.add('DateTypeCode:'+inttostr(DateTypeCode));
Inc(p);
Inc(p);
Move(p^, RecordLength, 2);
memo1.lines.add('RecordLength:'+inttostr(RecordLength));
Inc(p);
Inc(p);
Move(p^, EquipmentCode, 30);
memo1.lines.add('EquipmentCode:'+EquipmentCode);
for i:=0 to 29 do
inc(p);
Move(p^, AimCode, 10);
memo1.lines.add('AimCode:'+AimCode);
for i:=0 to 9 do
inc(p);
Move(p^,SerialNumber,4);
memo1.lines.add('SerialNumber:'+inttostr(SerialNumber));
for i:=0 to 3 do
inc(p);
Move(p^,DateType,1);
memo1.lines.add('DateType:'+inttostr(DateType));
inc(p);
Move(p^,Validity,2);
memo1.lines.add('Validity:'+inttostr(Validity));
inc(p);
inc(p);
Move(p^,Hour,1);
memo1.lines.add('Hour:'+inttostr(Hour));
inc(p);
Move(p^,Minute,1);
memo1.lines.add('Minute:'+inttostr(Minute));
inc(p);
Move(p^,Second,1);
memo1.lines.add('Second:'+inttostr(Second));
inc(p);
Move(p^,Millisecond,2);
memo1.lines.add('Millisecond:'+inttostr(Millisecond));
inc(p);
inc(p);
Move(p^,Longitude,4);
memo1.lines.add('Longitude:'+inttostr(Longitude));
inc(p);
inc(p);
inc(p);
inc(p);
Move(p^,Latitude,4);
memo1.lines.add('Latitude:'+inttostr(Latitude));
inc(p);
inc(p);
inc(p);
inc(p);
Move(p^,Azimuth,2);
memo1.lines.add('Azimuth:'+inttostr(Azimuth));
inc(p);
inc(p);
Move(p^,Course,2);
memo1.lines.add('Course:'+inttostr(Course));
inc(p);
inc(p);
Move(p^,Speed,2);
memo1.lines.add('Speed:'+inttostr(Speed));
inc(p);
inc(p);
Move(p^,Warp,2);
memo1.lines.add('Warp:'+inttostr(Warp));
inc(p);
inc(p);
Move(p^,X,4);
memo1.lines.add('X:'+inttostr(X));
inc(p);
inc(p);
inc(p);
inc(p);
Move(p^,Y,4);
memo1.lines.add('Y:'+inttostr(Y));
inc(p);
inc(p);
inc(p);
inc(p);
Move(p^,Z,2);
memo1.lines.add('Z:'+inttostr(Z));
inc(p);
inc(p);
Move(p^,EndCode,2);
memo1.lines.add('EndCode:'+inttostr(EndCode));
end;
end;