我没有用空间,我是自己写的
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Timer1: TTimer;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function Init_RS232(com_name
Char):boolean; //初始化RS232
end;
var
Form1: TForm1;
hCom:Thandle;
successflag:Boolean;
lpol
overlapped;
implementation
uses
SPComm;
{$R *.dfm}
function TForm1.Init_RS232(com_name
Char):boolean; //初始化RS232
var
lpdcb:TDCB;
CommTimeOut : TCOMMTIMEOUTS;
begin
hCom:=CreateFile(com_name
,(GENERIC_READ or GENERIC_WRITE)
,0
,nil
,OPEN_EXISTING
,FILE_ATTRIBUTE_NORMAL , 0);//打开串行口
if hCom=invalid_handle_value then
begin
ShowMessage('Can not Open COM!');
CloseHandle(hCom);
Result:=false;
exit;
end;
if GetFileType( hCom ) <> FILE_TYPE_CHAR then
begin
CloseHandle( hCom );
raise ECommsError.Create( 'File handle is not a comm handle ' )
end;
CommTimeOut.ReadIntervalTimeout := MAXDWORD;
CommTimeOut.ReadTotalTimeoutMultiplier := 0;
CommTimeOut.ReadTotalTimeoutConstant := 0;
SetCommTimeouts(hCom, CommTimeOut);
successflag:=SetupComm(hCom,4096,4096); //设置COM输入,输出缓冲区皆为4096字节
PurgeComm(hCom, PURGE_TXABORT and PURGE_RXABORT and PURGE_TXCLEAR and PURGE_RXCLEAR ) ;
if not successflag then
begin
ShowMessage('Can not setup COM!');
CloseHandle(hCom);
Result:=false;
exit;
end;
successflag:=GetCommState(hCom,lpdcb); //获取DCB当前默认设置
if not successflag then
begin
ShowMessage('Can not get DCB!');
CloseHandle(hCom);
Result:=false;
exit;
end;
lpdcb.baudrate:=9600;
lpdcb.ByteSize:=8;
lpdcb.Parity:=NoParity; //偶校验
lpdcb.StopBits:=OneStopBit;
successflag:=SetCommState(hCom,lpdcb);
if not successflag then
begin
ShowMessage('Can not setup DCB!');
CloseHandle(hCom);
Result:=false;
exit;
end;
successflag:=SetCommMask(hCom,ev_rxchar or ev_txempty); //指定串行口事件为接收到字符;
if not successflag then
begin
ShowMessage('Can not setup Event!');
CloseHandle(hCom);
Result:=false;
exit;
end;
PurgeComm(hCom,PURGE_RXCLEAR or PURGE_TXCLEAR);
Result:=true;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
Init_RS232(pchar('com1'));
end;
procedure TForm1.Button1Click(Sender: TObject);
var
VV : LongWord;
dwLength : DWORD;
Buf : pChar;
SS : string;
begin
Buf :='123123';
dwLength := Length(Trim(Buf));
successflag:=False;
successflag:=WriteFile(hCom,Buf[0],dwLength,VV,nil);
if successflag then
memo1.Lines.Add('写入成功:'+Buf);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
fReadStat:Boolean;
InChar : array[0..255] of Char;
ComStat : PComStat;
dwErrorFlags,dwLength : DWORD;
ReadNumber
Word;
begin
timer1.Enabled:=False;
fillchar(lpol,sizeof(toverlapped),0);
try
if hCom>0 then
begin
GetMem(ComStat,SizeOf(TComStat));
ClearCommError(hCom, dwErrorFlags, ComStat);
if (dwErrorFlags > 0) then begin
PurgeComm(hCom,(PURGE_RXABORT and PURGE_RXCLEAR));
end;
dwLength := ComStat.cbInQue;
fReadStat:=False;
dwLength:=6;
if dwLength>0 then
fReadStat := ReadFile(hCom, InChar, dwLength,readnumber, lpol);
if fReadStat then
begin
memo1.Lines.Add('读到'+inttostr(readnumber)+'个数据:'+trim(InChar));
end;
end;
finally
timer1.Enabled:=true;
end;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
closehandle(hCom);
end;
end.