{----------------------------------------------------------------------------- The contents of this file are subject to the Mozilla Public License Version 1.1 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/MPL-1.1.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is: JvCommStatus.PAS, released on 2001-02-28. The Initial Developer of the Original Code is Sébastien Buysse [sbuysse att buypin dott com] Portions created by Sébastien Buysse are Copyright (C) 2001 Sébastien Buysse. All Rights Reserved. Contributor(s): Michael Beck [mbeck att bigfoot dott com]. You may retrieve the latest version of this file at the Project JEDI's JVCL home page, located at http://jvcl.sourceforge.net Known Issues: -----------------------------------------------------------------------------} // $Id: JvCommStatus.pas 10612 2006-05-19 19:04:09Z jfudickar $ unit JvCommStatus; {$I jvcl.inc} {$I windowsonly.inc} interface uses {$IFDEF UNITVERSIONING} JclUnitVersioning, {$ENDIF UNITVERSIONING} Windows, Classes, JvComponentBase; type TJvCommPort = 0..8; // ToDo: Port number might be up to 32 ! TJvCommWatcher = class(TThread) private FHandle: THandle; FStat: Cardinal; FOnChange: TNotifyEvent; procedure Changed; protected procedure Execute; override; end; TJvCommStatus = class(TJvComponent) private FClearToSend: Boolean; FDataSetReady: Boolean; FRing: Boolean; FReceiveLine: Boolean; FHandle: THandle; FWatcher: TJvCommWatcher; FDummy: Boolean; FPendingEvent, FPendingResume: Boolean; // Arioch FComm: TJvCommPort; FOnChanged: TNotifyEvent; procedure SetComm(const Value: TJvCommPort); procedure WatcherThreadChanged(Sender: TObject); //Arioch procedure WatcherThreadTerminated(Sender: TObject); //Arioch procedure UpdateStates(State: Cardinal); protected procedure Loaded; override; //Arioch public constructor Create(AOwner: TComponent); override; destructor Destroy; override; published { Do not store dummies } property ClearToSend: Boolean read FClearToSend write FDummy stored False; property DataSetReady: Boolean read FDataSetReady write FDummy stored False; property Ring: Boolean read FRing write FDummy stored False; property ReceiveLine: Boolean read FReceiveLine write FDummy stored False; property Comm: TJvCommPort read FComm write SetComm default 0; property OnChanged: TNotifyEvent read FOnChanged write FOnChanged; end; {$IFDEF UNITVERSIONING} const UnitVersioning: TUnitVersionInfo = ( RCSfile: '$URL: https://svn.sourceforge.net:443/svnroot/jvcl/trunk/jvcl/run/JvCommStatus.pas $'; Revision: '$Revision: 10612 $'; Date: '$Date: 2006-05-19 23:04:09 +0400 (Пт, 19 май 2006) $'; LogPath: 'JVCL\run' ); {$ENDIF UNITVERSIONING} implementation uses SysUtils; //=== { TJvCommStatus } ====================================================== constructor TJvCommStatus.Create(AOwner: TComponent); begin inherited Create(AOwner); FComm := 0; FHandle := 0; FPendingEvent := false; FPendingResume := false; if not (csDesigning in ComponentState) then begin FWatcher := TJvCommWatcher.Create(True); FWatcher.FHandle := FHandle; FWatcher.FStat := 0; FWatcher.FreeOnTerminate := True; FWatcher.OnTerminate := WatcherThreadTerminated; //Arioch FWatcher.FOnChange := WatcherThreadChanged; // Arioch end else FWatcher := nil; UpdateStates(0); //Arioch end; destructor TJvCommStatus.Destroy; begin if FWatcher <> nil then begin //Arioch FWatcher.OnTerminate := nil; // we do not want events be accidentally called after Sleep(50) FWatcher.FOnChange := nil; // when the JvCommStatus will turn into dead pointer !!! FWatcher.Terminate; FWatcher := nil; end; if FHandle <> 0 then CloseHandle(FHandle); inherited Destroy; end; procedure TJvCommStatus.UpdateStates(State: Cardinal); begin FClearToSend := (State and MS_CTS_ON) <> 0; FDataSetReady := (State and MS_DSR_ON) <> 0; FRing := (State and MS_RING_ON) <> 0; FReceiveLine := (State and MS_RLSD_ON) <> 0; end; procedure TJvCommStatus.WatcherThreadChanged(Sender: TObject); begin if (FWatcher <> nil) and (FHandle <> 0) then UpdateStates(FWatcher.FStat) else UpdateStates(0); if not (csDesigning in ComponentState) then if not (csLoading in ComponentState) // Arioch then begin if Assigned(FOnChanged) then FOnChanged(Self) end else FPendingEvent := true; end; procedure TJvCommStatus.WatcherThreadTerminated(Sender: TObject); begin FWatcher := nil; // FreeOnTerminate is set end; procedure TJvCommStatus.SetComm(const Value: TJvCommPort); var Stat: Cardinal; CommName: string; begin if FWatcher <> nil then FWatcher.FHandle := 0; // do not suspend if FHandle <> 0 then begin // <-- **** CloseHandle(FHandle); FHandle := 0; end; // <-- **** FComm := Value; // (rom) simplified through better TJvCommPort Stat := 0; // Arioch if FComm <> 0 then begin CommName := 'COM' + IntToStr(FComm); FHandle := CreateFile(PChar(CommName), 0, FILE_SHARE_READ or FILE_SHARE_WRITE, nil, OPEN_EXISTING, 0, 0); if FHandle = INVALID_HANDLE_VALUE then FHandle := 0 else if not GetCommModemStatus(FHandle, Stat) then Stat := 0; // i am not 100% sure Windows would never change var on error end; if FWatcher <> nil then begin FWatcher.FHandle := FHandle; FWatcher.FStat := Stat; // <-- ***** if FHandle <> 0 then if not (csLoading in ComponentState) // Arioch then FWatcher.Resume else FPendingResume := True; // Arioch // else // FWatcher.Suspend; // Arioch: Already suspended before end; WatcherThreadChanged(Self); end; procedure TJvCommStatus.Loaded; // Arioch begin inherited; if FPendingEvent then begin FPendingEvent := False; WatcherThreadChanged(Self); end; if FPendingResume then begin FPendingResume := false; if Assigned (FWatcher) then FWatcher.resume; end; end; //=== { TJvCommWatcher } ===================================================== procedure TJvCommWatcher.Changed; begin FOnChange(nil); end; procedure TJvCommWatcher.Execute; var Mask: Cardinal; begin // (rom) secure thread against exceptions try while not Terminated do begin if FHandle <> 0 then begin GetCommModemStatus(FHandle, Mask); if Mask <> FStat then begin FStat := Mask; Synchronize(Changed); end; Sleep(50); end else Suspend; end; except end; end; {$IFDEF UNITVERSIONING} initialization RegisterUnitVersion(HInstance, UnitVersioning); finalization UnregisterUnitVersion(HInstance); {$ENDIF UNITVERSIONING} end.