L
linying
Unregistered / Unconfirmed
GUEST, unregistred user!
我用spcomm读一个串口的数据,DTE只有两根线,即“信号地”和“发送数据”,
而且DTE是不停发数据的,所以receivedata事件无法停止,我用了一个定时器来
打开串口,程序如下,不知可行否。程序运行过,可以读数,但是不是很稳定。
DTE发的数据是以7个字节为循环的,波特率为1200。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, SPComm, DdeMan;
type
TForm1 = class(TForm)
Comm1: TComm;
Memo1: TMemo;
Timer1: TTimer;
Memo2: TMemo;
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
view:string;//存读来的数据
implementation
{$R *.DFM}
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i,j:integer;
view1,view2:string;
rbuff:array[1..14] of byte;
begin
sleep(1000);
move(buffer^,pchar(@rbuff)^,bufferlength);
comm1.stopcomm;
memo2.lines.Add(inttostr(bufferlength));
if bufferlength=7 then
begin
for i:=1 to (bufferlength-1) do
begin
view1:=inttohex(rbuff,2)+ inttohex(rbuff[i+1],2);
if view1='1DE2' then j:=i;//判断要取的数据的位置,数据是跟在'1DE2'后的两个字节
end;
if j<=(bufferlength-3) then
view2:=inttohex(rbuff[j+2],2)+'.'+ inttohex(rbuff[j+3],2);
view:=view2;
end;
memo1.lines.add(view);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.stopcomm;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
comm1.stopcomm;
comm1.StartComm;
end;
end.
而且DTE是不停发数据的,所以receivedata事件无法停止,我用了一个定时器来
打开串口,程序如下,不知可行否。程序运行过,可以读数,但是不是很稳定。
DTE发的数据是以7个字节为循环的,波特率为1200。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, SPComm, DdeMan;
type
TForm1 = class(TForm)
Comm1: TComm;
Memo1: TMemo;
Timer1: TTimer;
Memo2: TMemo;
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
view:string;//存读来的数据
implementation
{$R *.DFM}
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i,j:integer;
view1,view2:string;
rbuff:array[1..14] of byte;
begin
sleep(1000);
move(buffer^,pchar(@rbuff)^,bufferlength);
comm1.stopcomm;
memo2.lines.Add(inttostr(bufferlength));
if bufferlength=7 then
begin
for i:=1 to (bufferlength-1) do
begin
view1:=inttohex(rbuff,2)+ inttohex(rbuff[i+1],2);
if view1='1DE2' then j:=i;//判断要取的数据的位置,数据是跟在'1DE2'后的两个字节
end;
if j<=(bufferlength-3) then
view2:=inttohex(rbuff[j+2],2)+'.'+ inttohex(rbuff[j+3],2);
view:=view2;
end;
memo1.lines.add(view);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.stopcomm;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
comm1.stopcomm;
comm1.StartComm;
end;
end.