没太明白你的第二个问题,收和发都是你做的吗?如果是,你要自己定义判断开始和结束的条件,就是说在你要发送的字符串前后都加特征码。下面是我做过的程序片断:
首先定义了收发的数据结构,共86字节:
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;
var
FCOMM: TFCOMM;
Num,Num1: integer;
SendData:TDateRec;
Ready: bool;
FirstByte,SecondByte:byte;
接收:
procedure TFCOMM.ComPortRxChar(Sender: TObject; Count: Integer);
var
ReceiveData:TDateRec;
p
byte;
Block : array[0..85] of Char;
begin
if (not Ready) then
begin
ComPort.Read(p,1); //开始码为$7FFE,接收时先收的是$FE
move(p, SecondByte,1);
if SecondByte = $FE then //如果收到的字节是$FE,那就看看下一个是不是$7F
begin
FirstByte := SecondByte;
ComPort.Read(p,1);;
move(p, SecondByte,1);
end;
end;
if (FirstByte = $FE) and (SecondByte = $7F) then
Ready := True;
if Ready and (count>=84) then
begin
memo1.Clear;
Inc(Num1);
Edit2.Text := IntToStr(Num1);
ComPort.Read(Block,84);
p:=@Block;
with ReceiveData do
begin
memo1.lines.add('StartCode:'+IntToStr($7FFE));
//Inc(p,2);
Move(p^, DateTypeCode, 2);
memo1.lines.add('DateTypeCode:'+inttostr(DateTypeCode));
Inc(p,2);
Move(p^, RecordLength, 2);
memo1.lines.add('RecordLength:'+inttostr(RecordLength));
Inc(p,2);
Move(p^, EquipmentCode, 30);
memo1.lines.add('EquipmentCode:'+EquipmentCode);
inc(p,30);
Move(p^, AimCode, 10);
memo1.lines.add('AimCode:'+AimCode);
inc(p,10);
Move(p^,SerialNumber,4);
memo1.lines.add('SerialNumber:'+inttostr(SerialNumber));
inc(p,4);
Move(p^,DateType,1);
memo1.lines.add('DateType:'+inttostr(DateType));
inc(p);
Move(p^,Validity,2);
memo1.lines.add('Validity:'+inttostr(Validity));
Inc(p,2);
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,2);
Move(p^,Longitude,4);
memo1.lines.add('Longitude:'+inttostr(Longitude));
Inc(p,4);
Move(p^,Latitude,4);
memo1.lines.add('Latitude:'+inttostr(Latitude));
Inc(p,4);
Move(p^,Azimuth,2);
memo1.lines.add('Azimuth:'+inttostr(Azimuth));
Inc(p,2);
Move(p^,Course,2);
memo1.lines.add('Course:'+inttostr(Course));
Inc(p,2);
Move(p^,Speed,2);
memo1.lines.add('Speed:'+inttostr(Speed));
Inc(p,2);
Move(p^,Warp,2);
memo1.lines.add('Warp:'+inttostr(Warp));
Inc(p,2);
Move(p^,X,4);
memo1.lines.add('X:'+inttostr(X));
Inc(p,4);
Move(p^,Y,4);
memo1.lines.add('Y:'+inttostr(Y));
Inc(p,4);
Move(p^,Z,2);
memo1.lines.add('Z:'+inttostr(Z));
Inc(p,2);
Move(p^,EndCode,2);
memo1.lines.add('EndCode:'+inttostr(EndCode));
end;
FirstByte := 0;
SecondByte := 0;
Ready := False;
end;
end;