unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, ComCtrls;
type
TForm1 = class(TForm)
PageControl1: TPageControl;
TabSheet1: TTabSheet;
TabSheet2: TTabSheet;
RDCom: TRadioGroup;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Msend: TMemo;
MReceive: TMemo;
Label1: TLabel;
Label2: TLabel;
Button5: TButton;
RadioGroup1: TRadioGroup;
Button6: TButton;
SPDTR: TShape;
SPRTS: TShape;
Button7: TButton;
button8: TButton;
Label3: TLabel;
Label4: TLabel;
TabSheet3: TTabSheet;
Button10: TButton;
SPCD: TShape;
SPDSR: TShape;
SPCTS: TShape;
SPRi: TShape;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Button9: TButton;
Timer1: TTimer;
procedure Button1Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure opencomm;
procedure Button5Click(Sender: TObject);
procedure Button7Click(Sender: TObject);
procedure button8Click(Sender: TObject);
procedure Button9Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button10Click(Sender: TObject);
private
{ Private declarations }
DTRState:boolean;
RTSState:boolean;
doValue:dword;
public
{ Public declarations }
end;
var
Form1: TForm1;
hcomm: Thandle;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
begin
opencomm;
end;
procedure TForm1.Button3Click(Sender: TObject);
var
temp:string;
lrc:longword;
begin
if (Hcomm=0) then
exit;//检查hangle值
temp:=msend.text;
writefile(Hcomm,pchar(temp)^,length(temp),lrc,nil);
end;
procedure TForm1.Button4Click(Sender: TObject);
var
Temp:string;
inbuff:array[0..2047]of char;
nbytesRead,dwEvent,dwError:longword;
cs:tcomstat;
begin
clearcommerror(Hcomm,dwerror,@cs);//取得状态;
//数据是否大于所准备的缓冲区;
if cs.cbinque>sizeof(inbuff) then
begin
purgecomm(Hcomm,purge_rxclear); //清除通讯端口数据
exit;
end;
readfile(Hcomm,inbuff,cs.cbinque,nbytesread,nil);//接收通讯端口的数据
//转移数据到变量中
temp:=copy(inbuff,1,cs.cbinque);
mreceive.text:=temp;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
setcommmask(hcomm,$0);
closehandle(Hcomm);
close;
end;
procedure TForm1.opencomm;
var
cc:Tcommconfig;
temp:String;
begin
Temp:='COM'+inttostr(rdcom.ItemIndex+1);//选择所要的打开通讯端口
hComm:=createfile(pchar(temp),Generic_read or Generic_write,0,nil,open_existing,0,0);//打开通讯端口
if (hcomm= Invalid_Handle_Value) Then
begin
messagebox(0,'打开通讯端口错误!!','',MB_OK);
exit;
end;
GetCommState(Hcomm,cc.dcb); //得知目前通讯端口的状态
cc.dcb.BaudRate:=CBR_9600; //设置波特律为9600
CC.dcb.ByteSize:=8; // 字节为8位
CC.dcb.Parity:=Noparity; // parity为none;
CC.dcb.StopBits:=Onestopbit; // 一个停止位
if not SetcommState(hcomm,cc.dcb) then//设置通信端口的状态
begin
Messagebox(0,'通讯端口设置错误!!!','',MB_OK);
closehandle(hcomm);
exit;
end;
end;
procedure TForm1.Button5Click(Sender: TObject);
begin
if (hcomm<>0) then
begin
showmessage('通讯端口已打开!不需打开');
exit;
end;
opencomm;
EscapeCommFunction(hcomm,ClrDtr);//将DTR降为低电压
escapeCommfunction(hcomm,clrrts);//将RTS降为低电压
end;
procedure TForm1.Button7Click(Sender: TObject);
begin
if (Hcomm=0) then
begin
messagedlg('通讯端口未打开!!',mterror,[mbok],0);
exit;
end;
if DTRState then
begin
dovalue:=clrdtr;//设置DTR为低电位
ESCapecommfunction(hcomm,dovalue);//输出DTR状态;
SPDTR.Brush.Color:=clwhite;
end
else
begin
dovalue:=setdtr;//设置DTR为低电位
ESCapecommfunction(hcomm,dovalue);//输出DTR状态;
SPDTR.Brush.Color:=clred;
end;
DTRState:=(not DTRstate);
end;
procedure TForm1.button8Click(Sender: TObject);
begin
if (Hcomm=0) then
begin
messagedlg('通讯端口未打开!!',mterror,[mbok],0);
exit;
end;
if RTSState then
begin
dovalue:=clrRTS;//设置DTR为低电位
ESCapecommfunction(hcomm,dovalue);//输出DTR状态;
SPRTS.Brush.Color:=$FFFFFF;
end
else
begin
dovalue:=setRTS;//设置DTR为低电位
ESCapecommfunction(hcomm,dovalue);//输出DTR状态;
SPRTS.Brush.Color:=$00FF00;
end;
RTSstate:=(not RtsState);
end;
procedure TForm1.Button9Click(Sender: TObject);
begin
if (hcomm<>0) then
begin
showmessage('通讯端口已打开!不需打开');
exit;
end;
opencomm;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
lstatus
word;
begin
if (Hcomm=0) then
exit;
if GetCommModemStatus(Hcomm,lStatus) then
begin
if (lStatus and ms_CTS_on)=MS_CTS_on then
SPCTS.brush.color:=clred
else
SPcts.Brush.Color:=clwhite;
if (lstatus and ms_DSR_on)=ms_dsr_on then
spdsr.Brush.Color:=clred
else
spdsr.Brush.Color:=clwhite;
if (lstatus and ms_ring_on)=ms_ring_on then
spri.Brush.Color:=clred
else
spri.Brush.Color:=clwhite;
if (lstatus and ms_rlsd_on)=ms_rlsd_on then
spcd.Brush.Color:=clred
else
spcd.Brush.Color:=clwhite;
end;
end;
procedure TForm1.Button10Click(Sender: TObject);
begin
setcommmask(hcomm,$0);
closehandle(Hcomm);
close;
end;
end.
其实很简单就把串口看成一个文件就可以了,用pcomm或spcomm更加方便。