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