给你看这个,我正在用的,用的是CPort
unit unJSKMonitorThread;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Contnrs, unGeneral, unPublic, unCommPub, unGreaser;
type
TJSKMonitorThread = class(TThread)
private
{ Private declarations }
FJSKList:TObjectList;
protected
procedure Execute;
override;
procedure JSKQueryCycle;
procedure ThreadTerminateX(Sender:TObject);
public
constructor Create(AList:TObjectList);
end;
implementation
uses
unMonitor;
{ TJSKMonitorThread }
constructor TJSKMonitorThread.Create(AList:TObjectList);
begin
inherited Create(True);
FJSKList:=AList;
OnTerminate:=ThreadTerminateX;
end;
procedure TJSKMonitorThread.Execute;
begin
FreeOnTerminate:=True;
JSKQueryCycle;
end;
procedure TJSKMonitorThread.JSKQueryCycle;
var
i:Integer;
jsk:TGreaserJSK;
begin
while Truedo
begin
if Terminated then
Break;
for i := 0 to FJSKList.Count -1do
begin
if Terminated then
Break;
jsk:=TGreaserJSK(FJSKList);
if jsk.Enabled then
begin
try
Synchronize(jsk.CheckState);
//这里用到主线程动态创建的串口组件
if jsk.JSKStaErr <> jeNone then
begin
SendMessage(frmMonitor.Handle,WM_JSKSTATEERROR,Ord(jsk.JSKStaErr),i);
end
else
begin
//单机发油模式
if jsk.JSKModel = jmManual then
begin
PostMessage(frmMonitor.Handle,WM_JSKMANUALPROC,0,i);
end
else
if jsk.JSKModel = jmAuto then
begin
//空闲
if jsk.JSKState = jsIdle then
begin
PostMessage(frmMonitor.Handle,WM_JSKGREASERPROC,GS_IDLE,i);
end
//申请发油
//else
if (jsk.JSKState = jsApply) and (jsk.RealTimeValue = 0) then
else
if jsk.JSKState = jsApply then
begin
SendMessage(frmMonitor.Handle,WM_JSKGREASERPROC,GS_APPLY,i);
end
//加油中
else
if jsk.JSKState = jsBusy then
begin
SendMessage(frmMonitor.Handle,WM_JSKGREASERPROC,GS_BUSY,i);
end
//加油完成
else
if jsk.JSKState = jsComplete then
begin
SendMessage(frmMonitor.Handle,WM_JSKGREASERPROC,GS_COMPLETE,i);
end;
end;
end;
except
on E:ECommStatedo
begin
SendMessage(frmMonitor.Handle,WM_JSKCOMMERROR,E.Code,i);
end;
end;
end;
//DelayEx(CycleSeprator);
Delay(0,CycleSeprator);
end;
end;
end;
procedure TJSKMonitorThread.ThreadTerminateX(Sender: TObject);
begin
PostMessage(frmMonitor.Handle,WM_JSKTHREADTERMINATE,0,0);
end;
end.