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