Q
qinzi5237
Unregistered / Unconfirmed
GUEST, unregistred user!
以下是我写的代码,不知道为什么偶尔会执行到WaitFor语句时就一直死等在那里了
//线程代码
unit EncrptKeyThread;
interface
uses
Classes,
Windows, Messages, SysUtils, Variants, StdCtrls, dialogs, Controls, Forms;
type
TZt598OpenCom = function(pi_nPort:integer;
pi_nComRate:integer;
pi_controlchar):integer;
stdcall;
//打开键盘串口
TZt598CloseCom = function():integer;
stdcall;
//关闭键盘串口
TZt598OpenKeyVoic = function(Ctl:integer):integer;
stdcall;
//激活键盘
TZt598SetTimeOut = function(pi_Second:integer):integer;
stdcall;
//设置超时
TZt598PinLoadWorkKey = function(pi_MKeyNo:integer;
pi_WKeyNo:integer;
pi_WkeyAsc:string;
pi_Mode:char):integer;
stdcall;
//载入主密钥
TZt598ActivWorkPin = function(pi_MKeyNo:integer;
pi_WKeyNo:integer):integer;
stdcall;
TZt598PinStartAdd = function(pi_PinLen:integer;
pi_DispMode:integer;
pi_AddMode:integer;
pi_PromMode:integer;
pi_TimeOut:integer;
pchOutPinchar):Integer;
stdcall;
TZt598PinReportPressed = function(po_charchar):Integer;
stdcall;
TZt598PinReadPin = function(po_strchar):integer;
stdcall;
TZt598PinUnAdd = function(pi_strchar;
po_strchar):integer;
stdcall;
TZt598PinAbord = function():integer;
stdcall;
MyEncrptKeyThread = class(TThread)
private
{ Private declarations }
DllHins:Cardinal;
//动态库句柄
checkchar:array[0..20] of char;
protected
procedure Execute;
override;
public
isStop:boolean;
IsLoad:Boolean;
//是否载入动态库
IsOpen:Boolean;
//是否打开端口
IsValid:Boolean;
//是否激活键盘
ComNum:integer;
//com端口
ShowMsg:string;
//destructor Destroy;
override;
procedure closeKeyVoic(Sender:TObject);
end;
implementation
procedure MyEncrptKeyThread.closeKeyVoic;
var
ret:integer;
begin
try
ret := TZt598OpenKeyVoic(GetProcAddress(DllHins, 'OpenKeyVoic'))(0);
ret := TZt598CloseCom(GetProcAddress(DllHins, 'gCloseCom'));
finally
freelibrary(DllHins);
IsLoad := false;
end;
end;
procedure MyEncrptKeyThread.Execute;
var
RetCode:integer;
Pcheckcharchar;
begin
RetCode := 0;
while truedo
begin
if (terminated = true) or (isStop = true) then
begin
break;
end;
//判断动态库是否已经载入,如果没有载入则打开载入
if not IsLoad then
begin
try
DllHins := LoadLibrary('598.DLL');
IsLoad := true;
except
IsLoad := false;
end;
end;
//判断是否打开了com端口,没有打开则打开端口
if (IsLoad) and (not IsOpen) then
begin
try
TZt598CloseCom(GetProcAddress(DllHins, 'gCloseCom'))();
//强制先关闭串口
finally
;
end;
RetCode := TZt598OpenCom(GetProcAddress(DllHins, 'gOpenCom'))(ComNum, 9600,
'02');
//打开端口
if RetCode = 0 then
begin
IsOpen := true;
end
else
IsOpen := false;
end;
//判断是否激活pinpad,没有打开则激活pinpad
if (IsLoad) and (IsOpen) and (not IsValid) then
begin
TZt598SetTimeOut(GetProcAddress(DllHins, 'gSetTimeOut'))(60);
//设置金属小键盘超时
RetCode := TZt598OpenKeyVoic(GetProcAddress(DllHins, 'OpenKeyVoic'))(3);
//激活金属小键盘
if RetCode = 0 then
begin
IsValid := true;
end
else
IsValid := false;
end;
if IsLoad and IsOpen and IsValid then
begin
Pcheckchar := @checkchar[0];
checkchar[0] := #0;
RetCode := TZt598PinReportPressed(GetProcAddress(DllHins,
'PinReportPressed'))(Pcheckchar);
if RetCode = 0 then
begin
if (checkchar[0] = chr($2E))or (checkchar[0] = chr($2A))or (checkchar[0] = chr($23)) then
checkchar[0] := #0;
PostMessage(Screen.ActiveControl.Handle, WM_CHAR, Ord(checkchar[0]), 0);
//将获取的键盘信息送到窗口的相关句柄中
end;
end;
end;
end;
end.
//启动线程代码段
EncrptKeyThread1 := MyEncrptKeyThread.Create(true);
EncrptKeyThread1.ComNum := comnum;
EncrptKeyThread1.isStop:=false;
EncrptKeyThread1.IsLoad := false;
EncrptKeyThread1.IsOpen := false;
EncrptKeyThread1.IsValid := false;
EncrptKeyThread1.OnTerminate := EncrptKeyThread1.closekeyvoic;
EncrptKeyThread1.Resume;
//中止线程代码段
if EncrptKeyThread11 <> nil then
begin
EncrptKeyThread11.isStop := true;
EncrptKeyThread11.Terminate;
if EncrptKeyThread11.Suspended then
EncrptKeyThread11.Resume;
EncrptKeyThread11.WaitFor;
freeandnil(EncrptKeyThread11);
end;
//线程代码
unit EncrptKeyThread;
interface
uses
Classes,
Windows, Messages, SysUtils, Variants, StdCtrls, dialogs, Controls, Forms;
type
TZt598OpenCom = function(pi_nPort:integer;
pi_nComRate:integer;
pi_controlchar):integer;
stdcall;
//打开键盘串口
TZt598CloseCom = function():integer;
stdcall;
//关闭键盘串口
TZt598OpenKeyVoic = function(Ctl:integer):integer;
stdcall;
//激活键盘
TZt598SetTimeOut = function(pi_Second:integer):integer;
stdcall;
//设置超时
TZt598PinLoadWorkKey = function(pi_MKeyNo:integer;
pi_WKeyNo:integer;
pi_WkeyAsc:string;
pi_Mode:char):integer;
stdcall;
//载入主密钥
TZt598ActivWorkPin = function(pi_MKeyNo:integer;
pi_WKeyNo:integer):integer;
stdcall;
TZt598PinStartAdd = function(pi_PinLen:integer;
pi_DispMode:integer;
pi_AddMode:integer;
pi_PromMode:integer;
pi_TimeOut:integer;
pchOutPinchar):Integer;
stdcall;
TZt598PinReportPressed = function(po_charchar):Integer;
stdcall;
TZt598PinReadPin = function(po_strchar):integer;
stdcall;
TZt598PinUnAdd = function(pi_strchar;
po_strchar):integer;
stdcall;
TZt598PinAbord = function():integer;
stdcall;
MyEncrptKeyThread = class(TThread)
private
{ Private declarations }
DllHins:Cardinal;
//动态库句柄
checkchar:array[0..20] of char;
protected
procedure Execute;
override;
public
isStop:boolean;
IsLoad:Boolean;
//是否载入动态库
IsOpen:Boolean;
//是否打开端口
IsValid:Boolean;
//是否激活键盘
ComNum:integer;
//com端口
ShowMsg:string;
//destructor Destroy;
override;
procedure closeKeyVoic(Sender:TObject);
end;
implementation
procedure MyEncrptKeyThread.closeKeyVoic;
var
ret:integer;
begin
try
ret := TZt598OpenKeyVoic(GetProcAddress(DllHins, 'OpenKeyVoic'))(0);
ret := TZt598CloseCom(GetProcAddress(DllHins, 'gCloseCom'));
finally
freelibrary(DllHins);
IsLoad := false;
end;
end;
procedure MyEncrptKeyThread.Execute;
var
RetCode:integer;
Pcheckcharchar;
begin
RetCode := 0;
while truedo
begin
if (terminated = true) or (isStop = true) then
begin
break;
end;
//判断动态库是否已经载入,如果没有载入则打开载入
if not IsLoad then
begin
try
DllHins := LoadLibrary('598.DLL');
IsLoad := true;
except
IsLoad := false;
end;
end;
//判断是否打开了com端口,没有打开则打开端口
if (IsLoad) and (not IsOpen) then
begin
try
TZt598CloseCom(GetProcAddress(DllHins, 'gCloseCom'))();
//强制先关闭串口
finally
;
end;
RetCode := TZt598OpenCom(GetProcAddress(DllHins, 'gOpenCom'))(ComNum, 9600,
'02');
//打开端口
if RetCode = 0 then
begin
IsOpen := true;
end
else
IsOpen := false;
end;
//判断是否激活pinpad,没有打开则激活pinpad
if (IsLoad) and (IsOpen) and (not IsValid) then
begin
TZt598SetTimeOut(GetProcAddress(DllHins, 'gSetTimeOut'))(60);
//设置金属小键盘超时
RetCode := TZt598OpenKeyVoic(GetProcAddress(DllHins, 'OpenKeyVoic'))(3);
//激活金属小键盘
if RetCode = 0 then
begin
IsValid := true;
end
else
IsValid := false;
end;
if IsLoad and IsOpen and IsValid then
begin
Pcheckchar := @checkchar[0];
checkchar[0] := #0;
RetCode := TZt598PinReportPressed(GetProcAddress(DllHins,
'PinReportPressed'))(Pcheckchar);
if RetCode = 0 then
begin
if (checkchar[0] = chr($2E))or (checkchar[0] = chr($2A))or (checkchar[0] = chr($23)) then
checkchar[0] := #0;
PostMessage(Screen.ActiveControl.Handle, WM_CHAR, Ord(checkchar[0]), 0);
//将获取的键盘信息送到窗口的相关句柄中
end;
end;
end;
end;
end.
//启动线程代码段
EncrptKeyThread1 := MyEncrptKeyThread.Create(true);
EncrptKeyThread1.ComNum := comnum;
EncrptKeyThread1.isStop:=false;
EncrptKeyThread1.IsLoad := false;
EncrptKeyThread1.IsOpen := false;
EncrptKeyThread1.IsValid := false;
EncrptKeyThread1.OnTerminate := EncrptKeyThread1.closekeyvoic;
EncrptKeyThread1.Resume;
//中止线程代码段
if EncrptKeyThread11 <> nil then
begin
EncrptKeyThread11.isStop := true;
EncrptKeyThread11.Terminate;
if EncrptKeyThread11.Suspended then
EncrptKeyThread11.Resume;
EncrptKeyThread11.WaitFor;
freeandnil(EncrptKeyThread11);
end;