我写的一个串口单元。不过还有问题教高手帮我看看
unit MYCOMM;
{
This unit create by 李金浩
2004-2-27
QQ:67260745
}
interface
uses
Forms, SysUtils, Windows, Messages, Dialogs, Controls, Classes, ToolWin, ComCtrls,
ExtCtrls, StdCtrls, Math;
//----------------
(*串口号*)
const Com1=1;
const Com2=2;
const Com3=3;
const Com4=4;
const Com5=5;
const Com6=6;
const Com7=7;
const Com8=8;
const Com9=9;
const Com10=10;
(*StopBit*)
const sbit1=0;
const sbit1_5=1;
const sbit2=2;
//-------------------
Type
TMyComm=Class
//public
Function CloseCOMM(Const Com:integer):boolean;//关闭指定串口
procedure CloseCommAll;//关闭所有打开的串口
function OpenComm(const COM:integer;const My_BandRate:DWORD;
Const My_ByteSize:byte;Const My_Parity:byte;Const My_StopBits:byte):Boolean;//打开指定串口
function OpenCommDef(const COM:integer; const My_BandRate:DWORD):Boolean;//打开指定串口
//读串口函数集
Function ReadComm_string(Const Com:integer;var revcStr:string;const len:DWORD):boolean;//读取串口string
Function ReadComm_ByteArray(const Com:integer;var revcArray:array of byte;const len:DWORD):boolean;
Function ReadComm_Byte(Const Com:integer;var OneByte:byte):boolean;
//写串口口函数集
Function WriteComm_String(Const Com:integer;Const SendString:string):boolean;
Function WriteComm_ByteArray(Const Com:integer;const SendByte:array of byte;Buflong:DWORD):boolean;
Function WriteComm_Byte(Const Com:integer;const SendByte:byte):boolean;
end;
//---------------------
var
MComm:TMycomm;//创建一个TMycomm属性类
// myThread:TThread;
Timer1:TTimer;
lpDCB:TDCB;
COMHandle:array[1..10] of Thandle; //文件句柄
ComOpenFlag:array[1..10] of boolean;//文件打开标识
rCommTimeouts:COMMTIMEOUTS;
bSuccessFlag:boolean;
ipError:Dword;
lpstat:PcomStat;
abSendBuf,abRecvBuf:array[1..1024] of byte;
nBytesWrite,nBytesRead:DWORD;//设置已写/读的数量
// lpwol:pOVERLAPPED;//读写的最后一个参数,无需设置
// sMessStr,sReadMess:String;//设置输入/输出传输字符
i:integer;
//---------
implementation
//**************************************
//打开文件句柄
//格式如:MComm.OpenComm(com1,4800,8,NoParity,0)
//**************************************
function TMyComm.OpenComm(const COM:integer; const My_BandRate:DWORD;
Const My_ByteSize:byte;Const My_Parity:byte;
Const My_StopBits:byte):Boolean;//打开指定串口
var //设置格式如:4800,8,N,1
comNum:string;
begin
//如果ComOpenFlag[COM]=false 表示未打开。。。则进行操作
if not ComOpenFlag[COM] then//if ComOpenFlag[COM]=false
begin
comNum:=format('Com%d',[com]);
//第一。创键文件句柄
ComHandle[com]:=Createfile(pAnsiChar(comNum),
GENERIC_Read or GENERIC_Write,
0,nil,open_existing,
file_attribute_normal,0);
if ComHandle[com]=invalid_handle_value then
begin
// showmessage('com1打开错误!');
CloseCOMM(com); //调用关闭串口
result:=false;
exit;
end else begin
result:=True;
ComOpenFlag[COM]:=true //表示打开过
end; // if ComHandle[com]=invalid_handle_value then
//第二:设备com输入输出缓冲区
bSuccessFlag:=SetupComm(ComHandle[com],4096,4096);
if not bSuccessFlag then
begin
showmessage('设备com输入输出缓冲区错误!');
CloseCOMM(com);
result:=false;
exit;
end;
//第三:取得默认DCB
bSuccessFlag:=GetCommstate(ComHandle[com],lpDcb);
if not bSuccessFlag then
begin
// showmessage('取得默认DCB错误!');
CloseCOMM(ComHandle[com]);
result:=false;
exit;
end;
//第四,设置DCB
//4800,8,N,1
lpDCB.BaudRate:=My_BandRate;
lpDCB.ByteSize:=My_ByteSize;
lpDCB.Parity:=My_Parity;//N无奇偶校验
lpDCB.StopBits:=My_StopBits;//0,1,2===>1,1.5,2
bSuccessFlag:=SetCommstate(ComHandle[com],lpDCB);
if not bSuccessFlag then
begin
// showmessage('设置DCB错误!');
CloseCOMM(ComHandle[com]);
exit;
end;
//第五:通过PurgeComm清空指定通信口的输入输出缓冲区的所有字符
// PURGE_TXABORT = 1; { Kill the pending/current writes to the comm port. }
// PURGE_RXABORT = 2; { Kill the pending/current reads to the comm port. }
// PURGE_TXCLEAR = 4; { Kill the transmit queue if there. }
// PURGE_RXCLEAR = 8; { Kill the typeahead buffer if there. }
PurgeComm(ComHandle[com],1 or 2 or 4 or 8);
ClearCommError(ComHandle[com],iperror,lpstat);
//第六 设置TimeOut
rCommTimeouts.ReadIntervalTimeout:=0;
rCommTimeouts.ReadTotalTimeoutConstant:=250;
rCommTimeouts.ReadTotalTimeoutMultiplier:=0;
rCommTimeouts.WriteTotalTimeoutMultiplier:=0;
rCommTimeouts.WriteTotalTimeoutConstant:=250;
bSuccessFlag:=SetCommTimeOuts(ComHandle[com],rCommTimeouts);
//EV_RXCHAR A character was received and placed in the input buffer.
SetCommMask(comHandle[com],EV_RXCHAR); //设置接收消息
//------------------------------------------
if not bSuccessFlag then
begin
// showmessage('设置TimeOut出错!');
result:=false;
CloseCOMM(ComHandle[com]);
exit;
end;
end else begin
result:=false;
end;
end;
//********************
//打开文件句柄
//def形式打开
//********************
function TMyComm.OpenCommDef(const COM:integer; const My_BandRate:DWORD):Boolean;
begin
//波特率,8,N,1
result:=OpenComm(com,My_BandRate,8,NoParity,0)
end;
//********************
//关闭文件句柄
//********************
Function TMyComm.CloseComm(Const Com:integer):boolean;
var
cSucc:boolean;
begin
//关闭文件句柄
cSucc:=true;
if ComOpenFlag[COM] then //如果处于打开,就进行关闭处理
begin
cSucc:=CloseHandle(ComHandle[com]);
ComOpenFlag[COM]:=false;//设为false。表示已关闭
end;
result:=cSucc;
end;
/////////////////
//关闭所有端口
/////////////////
procedure TMyComm.CloseCommAll;
var
i:integer;
begin
for i:=1 to 10 do
begin
CloseComm(i);
end;
end;