{ Управление интерфейсом MathCAD и визуальным интерфейсом } {--------------------------------------------------------------------------- The control program for mass-spectrometer MI1201-AGM (c) Copyright Aleksandrov O.E., 2001 Molecular Physics department, USTU, Ekaterinsburg, K-2, 620002, RUSSIA phone 75-47-15 E-mail: aleks@dpt.ustu.ru Программа управления масс-спектрометром МИ1201-АГМ (c) Собственность Александрова О.Е., 2001 620002, Екатеринбург, К-2, УГТУ, Кафедра молекулярной физики тел. 75-47-15 E-mail: aleks@dpt.ustu.ru ----------------------------------------------------------------------------} unit MCAD_MI1201_Interface; interface Uses Windows, SysUtils, Forms, MCAD_MI1201_FormThread, MCAD_MI1201_Thread0, MCAD_MI1201_Thread, MCAD_MI1201_Errors; function MsThread:tMsThread; function FormThread:TMainFormThread; function LoadMs:tErrorCode; function LoadInterface:tErrorCode; function UnloadInterface:tErrorCode; function UnloadVisualInterface:tErrorCode; function UnloadMS:tErrorCode; procedure Init; function TryInit:LResult; function CheckHardwareError:boolean; function Wait:tErrorCode; procedure SetReady; implementation USES SyncObjs, MCAD_MI1201_FormABOUT; var gMsThread:tMsThread=NIL; gFormThread:TMainFormThread=NIL; function MsThread:tMsThread; begin Result:=gMsThread; end; function FormThread:TMainFormThread; begin Result:=gFormThread; end; type tFlag=(fInitDone, fLogoShown); tFlags=set of tFlag; var gFlags:tFlags; function CheckHardwareError:boolean; begin Result:=gMsThread.CheckHardwareError; if Result then begin SetGlobalErrorCode(ecHardware_Error); end else if GlobalErrorCode=ecHardware_Error then begin SetGlobalErrorCode(ecOK); end; end; function TryInit:LResult; begin try Init; except SetGlobalErrorCode(ecUnknownError); end; Result:=Ord(GlobalErrorCode); end; function Wait:tErrorCode; begin Result:=GlobalErrorCode; if (Result=ecOK) and not (gMsThread.WaitReady) then begin Result:=ecTIMEOUT; CheckHardwareError; end; end; procedure SetReady; begin gMsThread.SetReady; end; procedure Init; const cInitCounter:byte=0; begin if LoadMS<>ecOK then begin SetGlobalErrorCode(ecInterfaceOff); Exit; end; if gMsThread.IsON then begin CheckHardwareError; Exit; end; if (cInitCounter>10) then Exit; Inc(cInitCounter); try SetGlobalErrorCode(ecOK); gMsThread.SetON; if not CheckHardwareError and gMsThread.IsON then begin Include(gFlags,fInitDone); end else begin Exclude(gFlags,fInitDone); end; except SetUnhandledErrorCode; SetReady; gMsThread.Suspend; end; end; function UnloadMS:tErrorCode; begin Result:=ecOK; if Assigned(gMsThread) then try gMsThread.TerminateAndWait; except MessageBox(0,'Ошибка при остановке витка МИ-1201-АГМ.','МИ-1201 АГМ',MB_OK+MB_ICONINFORMATION); SetGlobalErrorCode(ecHardware_Error); Result:=ecUnknownError; end; try FreeAndNil(gMsThread); except end; end; function UnloadVisualInterface:tErrorCode; begin Result:=ecOK; if Assigned(gFormThread) then try gFormThread.TerminateAndWait; except MessageBox(0,'Ошибка при остановке витка окна МИ-1201-АГМ.','МИ-1201 АГМ',MB_OK+MB_ICONINFORMATION); Result:=ecUnknownError; end; try FreeAndNil(gFormThread); except end; end; function UnloadInterface:tErrorCode; begin if Assigned(gMsThread) then gMsThread.Terminate; if Assigned(gFormThread) then gFormThread.Terminate; Result:=UnloadVisualInterface; if Result=ecOK then Result:=UnloadMS else UnloadMS; end; function LoadMs:tErrorCode; procedure UnableLoadInterface; begin MessageBox(0, 'Не удается создать поток.'#13'Интерфейс к MI-1201АГМ недоступен.', 'МИ-1201 АГМ', MB_OK+MB_ICONINFORMATION ); end; begin if Assigned(gMsThread) then begin Result:=ecOK; end else begin gFlags:=[]; try gMsThread:=tMsThread.Create(cMainTreadName); except gMsThread:=NIL; end; if Assigned(gMsThread) then begin if gMsThread.WaitForStart(20000)<>wrSignaled then FreeAndNil(gMsThread); end; if Assigned(gMsThread) then begin Result:=ecOK end else begin SetGlobalErrorCode(ecCantLoadInterface); Result:=ecCantLoadInterface; UnableLoadInterface; end; end; end; procedure ShowLogo; begin if not (fLogoShown in gFlags) then AboutShowForTimeEx({$IfDef DEBUG} 500{$ELSE} 2000{$EndIf},FALSE); end; procedure HideLogo; begin if not (fLogoShown in gFlags) then begin AboutClose; Include(gFlags, fLogoShown) ; end; end; function LoadInterface:tErrorCode; procedure UnableLoadInterface; begin MessageBox(0, 'Не удается создать поток.'#13'Визуальный интерфейс к MI-1201АГМ недоступен.', 'МИ-1201 АГМ', MB_OK+MB_ICONINFORMATION ); end; begin if Assigned(gFormThread) then begin Result:=ecOK; end else if LoadMs=ecOK then begin // ShowLogo; gFormThread:=TMainFormThread.Create(gMsThread); if Assigned(gFormThread) then begin if gFormThread.WaitForStart(20000)<>wrSignaled then FreeAndNil(gFormThread); end; if Assigned(gFormThread) then begin Result:=ecOK; end else begin Result:=ecCantLoadInterface; UnableLoadInterface; end; // HideLogo; end else begin Result:=ecCantLoadInterface end; end; INITIALIZATION FINALIZATION try UnloadInterface; except end; end.