| 1 | unit CommConnector;
|
|---|
| 2 |
|
|---|
| 3 | interface
|
|---|
| 4 |
|
|---|
| 5 | uses
|
|---|
| 6 | Classes, SysUtils, CommPin, CommSerialPort, CommTCPClient, CommThread,
|
|---|
| 7 | CommHub, CommTCPServer, CommTelnet, CommTelnetComPortOption;
|
|---|
| 8 |
|
|---|
| 9 | type
|
|---|
| 10 | TConnectionType = (ctNone, ctSerialPort, ctNetworkClient, ctDemo, ctNetworkServer);
|
|---|
| 11 |
|
|---|
| 12 | { TDeviceConnector }
|
|---|
| 13 |
|
|---|
| 14 | TDeviceConnector = class
|
|---|
| 15 | private
|
|---|
| 16 | FActive: Boolean;
|
|---|
| 17 | FConnectionType: TConnectionType;
|
|---|
| 18 | FTelnetComControl: Boolean;
|
|---|
| 19 | function GetActive: Boolean;
|
|---|
| 20 | function GetPin: TCommPin;
|
|---|
| 21 | function GetConnectionType: TConnectionType;
|
|---|
| 22 | procedure SetConnectionType(const AValue: TConnectionType);
|
|---|
| 23 | procedure SetTelnetComControl(AValue: Boolean);
|
|---|
| 24 | procedure UpdateConnectionType(const ConnectionType: TConnectionType;
|
|---|
| 25 | const ATelnetComControl: Boolean);
|
|---|
| 26 | procedure CommTCPServerConnect(Sender: TCommTCPServer; Pin: TCommPin);
|
|---|
| 27 | protected
|
|---|
| 28 | procedure SetActive(const AValue: Boolean);
|
|---|
| 29 | public
|
|---|
| 30 | Name: string;
|
|---|
| 31 | TelnetOptionComPort: TTelnetOptionComPort;
|
|---|
| 32 | CommTelnet: TCommTelnet;
|
|---|
| 33 | CommSerial: TCommSerialPort;
|
|---|
| 34 | CommTCPClient: TCommTCPClient;
|
|---|
| 35 | CommTCPServer: TCommTCPServer;
|
|---|
| 36 | CommDemo: TCommThread;
|
|---|
| 37 | CommHub: TCommHub;
|
|---|
| 38 | constructor Create;
|
|---|
| 39 | destructor Destroy; override;
|
|---|
| 40 | procedure Assign(Source: TDeviceConnector); virtual;
|
|---|
| 41 | property ConnectionType: TConnectionType read GetConnectionType
|
|---|
| 42 | write SetConnectionType;
|
|---|
| 43 | property Pin: TCommPin read GetPin;
|
|---|
| 44 | property TelnetComControl: Boolean read FTelnetComControl
|
|---|
| 45 | write SetTelnetComControl;
|
|---|
| 46 | property Active: Boolean read FActive write SetActive;
|
|---|
| 47 | end;
|
|---|
| 48 |
|
|---|
| 49 |
|
|---|
| 50 | implementation
|
|---|
| 51 |
|
|---|
| 52 | function TDeviceConnector.GetConnectionType: TConnectionType;
|
|---|
| 53 | begin
|
|---|
| 54 | Result := FConnectionType;
|
|---|
| 55 | end;
|
|---|
| 56 |
|
|---|
| 57 | function TDeviceConnector.GetActive: Boolean;
|
|---|
| 58 | begin
|
|---|
| 59 | Result := FActive;
|
|---|
| 60 | end;
|
|---|
| 61 |
|
|---|
| 62 | function TDeviceConnector.GetPin: TCommPin;
|
|---|
| 63 | begin
|
|---|
| 64 | Result := TCommPin(CommHub.Pins.Last);
|
|---|
| 65 | end;
|
|---|
| 66 |
|
|---|
| 67 | procedure TDeviceConnector.SetActive(const AValue: Boolean);
|
|---|
| 68 | begin
|
|---|
| 69 | if FActive = AValue then Exit;
|
|---|
| 70 | FActive := AValue;
|
|---|
| 71 | if AValue then begin
|
|---|
| 72 | case FConnectionType of
|
|---|
| 73 | ctSerialPort: CommSerial.Active := AValue;
|
|---|
| 74 | ctNetworkClient: CommTCPClient.Active := AValue;
|
|---|
| 75 | ctNetworkServer: CommTCPServer.Active := AValue;
|
|---|
| 76 | ctDemo: CommDemo.Active := AValue;
|
|---|
| 77 | ctNone: ;
|
|---|
| 78 | end;
|
|---|
| 79 | CommTelnet.Active := AValue;
|
|---|
| 80 | end else begin
|
|---|
| 81 | CommTelnet.Active := False;
|
|---|
| 82 | CommSerial.Active := False;
|
|---|
| 83 | CommTCPClient.Active := False;
|
|---|
| 84 | CommTCPServer.Active := False;
|
|---|
| 85 | CommDemo.Active := False;
|
|---|
| 86 | end;
|
|---|
| 87 | CommHub.Active := AValue;
|
|---|
| 88 | inherited;
|
|---|
| 89 | end;
|
|---|
| 90 |
|
|---|
| 91 | procedure TDeviceConnector.SetConnectionType(const AValue: TConnectionType);
|
|---|
| 92 | begin
|
|---|
| 93 | UpdateConnectionType(AValue, FTelnetComControl);
|
|---|
| 94 | end;
|
|---|
| 95 |
|
|---|
| 96 | procedure TDeviceConnector.SetTelnetComControl(AValue: Boolean);
|
|---|
| 97 | begin
|
|---|
| 98 | UpdateConnectionType(FConnectionType, AValue);
|
|---|
| 99 | end;
|
|---|
| 100 |
|
|---|
| 101 | procedure TDeviceConnector.UpdateConnectionType(const ConnectionType: TConnectionType;
|
|---|
| 102 | const ATelnetComControl: Boolean);
|
|---|
| 103 | begin
|
|---|
| 104 | if (FTelnetComControl = ATelnetComControl) and (FConnectionType = ConnectionType) then
|
|---|
| 105 | Exit;
|
|---|
| 106 | FTelnetComControl := ATelnetComControl;
|
|---|
| 107 | FConnectionType := ConnectionType;
|
|---|
| 108 |
|
|---|
| 109 | CommSerial.Active := False;
|
|---|
| 110 | CommTCPClient.Active := False;
|
|---|
| 111 | CommTCPServer.Active := False;
|
|---|
| 112 | CommDemo.Active := False;
|
|---|
| 113 | CommTelnet.Active := False;
|
|---|
| 114 | case ConnectionType of
|
|---|
| 115 | ctSerialPort: begin
|
|---|
| 116 | TCommPin(CommHub.Pins.First).Connect(CommSerial.Pin);
|
|---|
| 117 | CommSerial.Active := FActive;
|
|---|
| 118 | end;
|
|---|
| 119 | ctNetworkClient: begin
|
|---|
| 120 | if FTelnetComControl then begin
|
|---|
| 121 | TCommPin(CommHub.Pins.First).Connect(CommTelnet.TelnetPin);
|
|---|
| 122 | CommTCPClient.Pin.Connect(CommTelnet.RawPin);
|
|---|
| 123 | CommTCPClient.Active := FActive;
|
|---|
| 124 | CommTelnet.Active := FActive;
|
|---|
| 125 | end else begin
|
|---|
| 126 | TCommPin(CommHub.Pins.First).Connect(CommTCPClient.Pin);
|
|---|
| 127 | CommTCPClient.Active := FActive;
|
|---|
| 128 | end;
|
|---|
| 129 | end;
|
|---|
| 130 | ctNetworkServer: begin
|
|---|
| 131 | CommTCPServer.OnConnect := CommTCPServerConnect;
|
|---|
| 132 | CommTCPServer.Active := FActive;
|
|---|
| 133 | end;
|
|---|
| 134 | ctDemo: begin
|
|---|
| 135 | TCommPin(CommHub.Pins.First).Connect(CommDemo.Pin);
|
|---|
| 136 | CommDemo.Active := FActive;
|
|---|
| 137 | end;
|
|---|
| 138 | ctNone: begin
|
|---|
| 139 | TCommPin(CommHub.Pins.First).Disconnect;
|
|---|
| 140 | end;
|
|---|
| 141 | end;
|
|---|
| 142 | end;
|
|---|
| 143 |
|
|---|
| 144 | procedure TDeviceConnector.CommTCPServerConnect(Sender: TCommTCPServer; Pin: TCommPin);
|
|---|
| 145 | begin
|
|---|
| 146 | TCommPin(CommHub.Pins.First).Connect(Pin);
|
|---|
| 147 | end;
|
|---|
| 148 |
|
|---|
| 149 | constructor TDeviceConnector.Create;
|
|---|
| 150 | begin
|
|---|
| 151 | inherited;
|
|---|
| 152 | TelnetOptionComPort := TTelnetOptionComPort.Create;
|
|---|
| 153 | CommTelnet := TCommTelnet.Create(nil);
|
|---|
| 154 | CommTelnet.Register(TelnetOptionComPort);
|
|---|
| 155 | CommSerial := TCommSerialPort.Create(nil);
|
|---|
| 156 | CommTCPClient := TCommTCPClient.Create(nil);
|
|---|
| 157 | CommTCPServer := TCommTCPServer.Create(nil);
|
|---|
| 158 | CommDemo := TCommThread.Create(nil);
|
|---|
| 159 | CommHub := TCommHub.Create(nil);
|
|---|
| 160 | CommHub.Pins.AddNew;
|
|---|
| 161 | CommHub.Pins.AddNew;
|
|---|
| 162 | ConnectionType := ctNone;
|
|---|
| 163 | end;
|
|---|
| 164 |
|
|---|
| 165 | destructor TDeviceConnector.Destroy;
|
|---|
| 166 | begin
|
|---|
| 167 | Active := False;
|
|---|
| 168 | FreeAndNil(CommHub);
|
|---|
| 169 | FreeAndNil(CommSerial);
|
|---|
| 170 | FreeAndNil(CommTCPClient);
|
|---|
| 171 | FreeAndNil(CommTCPServer);
|
|---|
| 172 | FreeAndNil(CommDemo);
|
|---|
| 173 | CommTelnet.Unregister(TelnetOptionComPort);
|
|---|
| 174 | FreeAndNil(TelnetOptionComPort);
|
|---|
| 175 | FreeAndNil(CommTelnet);
|
|---|
| 176 | inherited;
|
|---|
| 177 | end;
|
|---|
| 178 |
|
|---|
| 179 | procedure TDeviceConnector.Assign(Source: TDeviceConnector);
|
|---|
| 180 | begin
|
|---|
| 181 | //FActive := Source.FActive;
|
|---|
| 182 | Name := Source.Name;
|
|---|
| 183 | CommSerial.Assign(Source.CommSerial);
|
|---|
| 184 | CommTelnet.Assign(Source.CommTelnet);
|
|---|
| 185 | FConnectionType := Source.FConnectionType;
|
|---|
| 186 | FTelnetComControl := Source.FTelnetComControl;
|
|---|
| 187 | //CommSocket.Assign(Source.CommSocket);
|
|---|
| 188 | end;
|
|---|
| 189 |
|
|---|
| 190 | end.
|
|---|
| 191 |
|
|---|