光标阅读机OMRAPI
{
功能描述:T40S
作者:天地狂虫
日期:2007年11月23日
}
unit OMRAPI;
interface
uses SysUtils,Windows;
type
//光标阅读机通讯端口类型
TOmrPortType=(OPTCOM,OPTUSB);
//光标阅读机接口函数声明
TOmrOpenUSB=function():Integer;stdcall;
TOmrOpenComm=function(Port:Integer):Integer;stdcall;
TOmrClose=function():Integer;stdcall;
TOmrS=function(buf:pchar):Integer;stdcall;
TOmrG=function(buf:PChar):Integer;stdcall;
TOmrGetError=function(buf:PChar):Integer;stdcall;
TOmrGetErrorStr=function(buf:PChar):Integer;stdcall;
TOmrLoadFormtFile=function(buf:pchar):Integer;stdcall;
//光标阅读机功能封装类
TOmrReader=class
private
{$IFNDEF simulate}
fOmrDllHandle:HMODULE;
fErrorBuffer:array[0..1] of Char;
fDataBuffer:array[0..256] of Char;
fPortType:TOmrPortType;//端口类型
fOmrOpenUSB:TOmrOpenUSB;
fOmrOpenCOM:TOmrOpenComm;
fOmrClose:TOmrClose;
fOmrS:TOmrS;
fOmrG:TOmrG;
fOmrGetError:TOmrGetError;
fOmrGetErrorStr:TOmrGetErrorStr;
fOmrLoadFormatFile:TOmrLoadFormtFile;
{$ENDIF}
//
fConnected:Boolean;
fDataFormated:Boolean;
private
{$IFNDEF simulate}
procedure OmrGetError;
{$ENDIF}
function Get_ErrorCode:Byte;
function Get_ErrorMessage:string;
function Get_Data:string;
public
constructor Create(PortType:TOmrPortType);
destructor Destroy;override;
public
function Open(PortNumber:Byte):Boolean;
procedure Close;
function LoadFormatFile(FilePath:string):Boolean;
function PassOneCard:Boolean;
function ReadOneCard:Boolean;
public
property Connected:Boolean read fConnected;
property DataFormated:Boolean read fDataFormated;
property ErrorCode:Byte read Get_ErrorCode;
property ErrorMessage:string read Get_ErrorMessage;
property Data:string read Get_Data;
end;
implementation
{ TOmrReader }
procedure TOmrReader.Close;
begin
{$IFNDEF simulate}
fOmrClose;
{$ENDIF}
fConnected:=False;
end;
constructor TOmrReader.Create(PortType: TOmrPortType);
{$IFNDEF simulate}
const
COMDLL='OmrComm.dll';
USBDLL='OmrUSB.dll';
var
LoadFailed:Boolean;
{$ENDIF}
begin
{$IFNDEF simulate}
fPortType:=PortType;
case PortType of
OPTUSB:
fOmrDllHandle:=LoadLibrary(USBDLL);
else
fOmrDllHandle:=LoadLibrary(COMDLL);
end;
if fOmrDllHandle<=0 then raise(Exception.Create('光标阅读机驱动初始化失败'));
//
LoadFailed:=False;
case fPortType of
OPTUSB:
begin
fOmrOpenUSB:=GetProcAddress(fOmrDllHandle,'OmrInit');
if Addr(fOmrOpenUSB)=nil then LoadFailed:=True;
end;
OPTCOM:
begin
fOmrOpenCOM:=GetProcAddress(fOmrDllHandle,'OmrInit');
if Addr(fOmrOpenCOM)=nil then LoadFailed:=True;
end;
else raise(Exception.Create('光标阅读机驱动初始化失败'));
end;
fOmrClose:=GetProcAddress(fOmrDllHandle,'OmrClose');
if Addr(fOmrClose)=nil then LoadFailed:=True;
fOmrS:=GetProcAddress(fOmrDllHandle,'OmrS');
if Addr(fOmrS)=nil then LoadFailed:=True;
fOmrG:=GetProcAddress(fOmrDllHandle,'OmrG');
if Addr(fOmrG)=nil then LoadFailed:=True;
fOmrGetError:=GetProcAddress(fOmrDllHandle,'OmrGetError');
if Addr(fOmrGetError)=nil then LoadFailed:=True;
fOmrGetErrorStr:=GetProcAddress(fOmrDllHandle,'OmrGetErrorStr');
if Addr(fOmrGetErrorStr)=nil then LoadFailed:=True;
fOmrLoadFormatFile:=GetProcAddress(fOmrDllHandle,'DownLoadFormatFile');
if Addr(fOmrLoadFormatFile)=nil then LoadFailed:=True;
//
if LoadFailed then raise(Exception.Create('光标阅读机驱动初始化失败'));
{$ENDIF}
//
fConnected:=False;
fDataFormated:=False;
end;
destructor TOmrReader.Destroy;
begin
{$IFNDEF simulate}
FreeLibrary(fOmrDllHandle);
{$ENDIF}
inherited;
end;
function TOmrReader.Get_Data: string;
begin
{$IFNDEF simulate}
Result:=string(fDataBuffer);
{$ELSE}
Result:='';
{$ENDIF}
end;
function TOmrReader.Get_ErrorCode: Byte;
begin
{$IFNDEF simulate}
Result:=StrToInt(fErrorBuffer);
{$ELSE}
Result:=0;
{$ENDIF}
end;
function TOmrReader.Get_ErrorMessage: string;
begin
case ErrorCode of
0:Result:='无法通信(未联机)';
1:Result:='没有加载数据格式';
2:Result:='A传感器数据溢出';
3:Result:='双张';
4:Result:='未检测到A传感器格式标志';
5:Result:='未检测到A传感器检测点线';
6:Result:='错误的命令参数';
7:Result:='A传感器卡纸(同步信号出错)';
8:Result:='无法通信(未联机)';
9:Result:='A传感器同步框计数值超界';
11:Result:='A,B传感器匹配阅读错';
12:Result:='B传感器数据溢出';
13:Result:='未检测到B传感器格式标志';
15:Result:='未检测到B传感器检测点线';
16:Result:='无纸';
17:Result:='B传感器卡纸(同步信号出错)';
19:Result:='B传感器同步框计数值超界';
else Result:='错误号='+IntToStr(ERROR);
end;
end;
function TOmrReader.LoadFormatFile(FilePath: string): Boolean;
{$IFNDEF simulate}
var
RC:Integer;
{$ENDIF}
begin
{$IFNDEF simulate}
Result:=False;
if not Connected then Exit;
RC:=fOmrLoadFormatFile(PChar(FilePath));
if RC>0 then
begin
Result:=True;
fDataFormated:=True;
end
else
begin
fDataFormated:=False;
OmrGetError;
end;
{$ELSE}
fDataFormated:=True;
Result:=True;
{$ENDIF}
end;
{$IFNDEF simulate}
procedure TOmrReader.OmrGetError;
begin
fOmrGetError(fErrorBuffer)
end;
{$ENDIF}
function TOmrReader.Open(PortNumber:Byte): Boolean;
{$IFNDEF simulate}
var
RC:Integer;
{$ENDIF}
begin
{$IFNDEF simulate}
Result:=False;
case fPortType of
OPTCOM:RC:=fOmrOpenCOM(PortNumber);
else RC:=fOmrOpenUSB;
end;
if RC>0 then
begin
Sleep(2000);
fConnected:=True;
Result:=True
end
else OmrGetError;
{$ELSE}
fConnected:=True;
Result:=True;
{$ENDIF}
end;
function TOmrReader.PassOneCard: Boolean;
{$IFNDEF simulate}
var
RC:Integer;
{$ENDIF}
begin
{$IFNDEF simulate}
Result:=False;
if not Connected then Exit;
RC:=fOmrS('R/');
if RC>0 then Result:=True
else
begin
OmrGetError;
case ErrorCode of
0,3,5,8,16:Result:=False
else Result:=True;
end;
end;
{$ELSE}
Result:=True;
{$ENDIF}
end;
function TOmrReader.ReadOneCard: Boolean;
{$IFNDEF simulate}
var
RC:Integer;
{$ENDIF}
begin
{$IFNDEF simulate}
Result:=False;
if not Connected then Exit;
RC:=fOmrS('R/');
if RC>0 then
begin
RC:=fOmrS('r A 0001 0256');
if RC>0 then
begin
RC:=fOmrG(@fDataBuffer);
if RC>0 then Result:=True
else OmrGetError;
end
else OmrGetError;
end
else OmrGetError;
{$ELSE}
Result:=True;
{$ENDIF}
end;
end.