各位,本人是初学者,求一个Delphi7编写的带串口通讯的源代码程序,谢谢!!!我的邮箱是cyoucme@yahoo.cn 可以再加分,拜谢啦!!!
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Comm1: TComm;
Memo1: TMemo;
Edit1: TEdit;
Edit2: TEdit;
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
rbuf : array[1..8] of byte;
implementation
{$R *.dfm}
function HexStrToStr(const S:string):string;
//16进制字符串转换成字符串
var
t:Integer;
ts:string;
M,Code:Integer;
begin
t:=1;
Result:='';
while t<=Length(S) do
begin //xlh 2006.10.21
while (t<=Length(S)) and (not (S[t] in ['0'..'9','A'..'F','a'..'f'])) do
inc(t);
if (t+1>Length(S))or(not (S[t+1] in ['0'..'9','A'..'F','a'..'f'])) then
ts:='$'+S[t]
else
ts:='$'+S[t]+S[t+1];
Val(ts,M,Code);
if Code=0 then
Result:=Result+Chr(M);
inc(t,2);
end;
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
data,vdata : double ;
str :string;
begin
SetLength(Str,BufferLength);
move(buffer^,pchar(@rbuf[1])^,bufferlength);
memo1.lines.Add( floattostr(rBuf[1]));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Comm1.commname := 'com'+inttostr(6) ;
comm1.baudrate := 9600 ;
comm1.startcomm;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
str:string;
begin
str:= HexStrToStr(edit1.Text);
form1.comm1.writecommdata(pchar(str),length(str));
end;
procedure TForm1.Button2Click(Sender: TObject);
var
str:string;
begin
str:= HexStrToStr(edit2.Text) ;
form1.comm1.writecommdata(pchar(str),length(str));
end;
end.
看您做什么啦 如果是和下位机通讯 先用comm1连接 在发送 或者上面的代码接受
这里使用的是16进制发送
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Comm1: TComm;
Memo1: TMemo;
Edit1: TEdit;
Edit2: TEdit;
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
rbuf : array[1..8] of byte;
implementation
{$R *.dfm}
function HexStrToStr(const S:string):string;
//16进制字符串转换成字符串
var
t:Integer;
ts:string;
M,Code:Integer;
begin
t:=1;
Result:='';
while t<=Length(S) do
begin //xlh 2006.10.21
while (t<=Length(S)) and (not (S[t] in ['0'..'9','A'..'F','a'..'f'])) do
inc(t);
if (t+1>Length(S))or(not (S[t+1] in ['0'..'9','A'..'F','a'..'f'])) then
ts:='$'+S[t]
else
ts:='$'+S[t]+S[t+1];
Val(ts,M,Code);
if Code=0 then
Result:=Result+Chr(M);
inc(t,2);
end;
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
data,vdata : double ;
str :string;
begin
SetLength(Str,BufferLength);
move(buffer^,pchar(@rbuf[1])^,bufferlength);
memo1.lines.Add( floattostr(rBuf[1]));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Comm1.commname := 'com'+inttostr(6) ;
comm1.baudrate := 9600 ;
comm1.startcomm;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
str:string;
begin
str:= HexStrToStr(edit1.Text);
form1.comm1.writecommdata(pchar(str),length(str));
end;
procedure TForm1.Button2Click(Sender: TObject);
var
str:string;
begin
str:= HexStrToStr(edit2.Text) ;
form1.comm1.writecommdata(pchar(str),length(str));
end;
end.
看您做什么啦 如果是和下位机通讯 先用comm1连接 在发送 或者上面的代码接受
这里使用的是16进制发送
温馨提示:答案为网友推荐,仅供参考