spcomm控件问题(50分)

  • 主题发起人 主题发起人 草在墙头
  • 开始时间 开始时间

草在墙头

Unregistered / Unconfirmed
GUEST, unregistred user!
本机com1发送信息,com2接受,用spcomm怎么实现???
 
spcomm有个例子你可看看(在坛子搜索),
硬件上必须准备一条连接线用以连接COM1和COM2,
或自己参照串口连线自己做一条,焊三根线就可以,
注意两边的头都应是‘公’的。
 
这个我来告诉你把,这个程序我已经调试没有问题,把核心部分给你贴出来:
接收:
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
type
iArr = ^integer;
var
RevP : array [1..4096] of byte;
i : integer;
CommRevStr : ShortString;
begin
if (recp=1)then
begin
SetLength(CommRevStr,BufferLength);
Move(IArr(buffer)^,Revp,bufferLength);
For i:=1 to BufferLength do
begin
CommrevStr := Char(Revp);
end;
Memo2.Lines.Add(CommrevStr);
发送:
comm1.WriteCommData(pchar(strsend2),length(strsend2));
没有问题的!
现在的论坛回答的人越来越少了~~,真是不见往日的繁荣哈哈
 
发送简单的数据可以参照楼上的,发送复杂的数据可以参照我的:
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:Pbyte;
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;
 
我想问一下楼上,我的程序需要接收一个不间断的循环量“abcdefghijk123456789”
用您的程序是不是就不可以了
 
怎么会不可以呢?当然是行的啦,不过你的数据是什么,只要你可以发送过来,我就可以接收。

这样还有问题吗?不管是文本、二进制都可以,这样还有问题吗?

大家可以探讨一下。
 
多人接受答案了。
 
后退
顶部