source: PinConnection/CommConnector.pas

Last change on this file was 575, checked in by chronos, 5 months ago
  • Modified: Remove U prefix from unit names of PinConnection package.
File size: 5.5 KB
Line 
1unit CommConnector;
2
3interface
4
5uses
6 Classes, SysUtils, CommPin, CommSerialPort, CommTCPClient, CommThread,
7 CommHub, CommTCPServer, CommTelnet, CommTelnetComPortOption;
8
9type
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
50implementation
51
52function TDeviceConnector.GetConnectionType: TConnectionType;
53begin
54 Result := FConnectionType;
55end;
56
57function TDeviceConnector.GetActive: Boolean;
58begin
59 Result := FActive;
60end;
61
62function TDeviceConnector.GetPin: TCommPin;
63begin
64 Result := TCommPin(CommHub.Pins.Last);
65end;
66
67procedure TDeviceConnector.SetActive(const AValue: Boolean);
68begin
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;
89end;
90
91procedure TDeviceConnector.SetConnectionType(const AValue: TConnectionType);
92begin
93 UpdateConnectionType(AValue, FTelnetComControl);
94end;
95
96procedure TDeviceConnector.SetTelnetComControl(AValue: Boolean);
97begin
98 UpdateConnectionType(FConnectionType, AValue);
99end;
100
101procedure TDeviceConnector.UpdateConnectionType(const ConnectionType: TConnectionType;
102 const ATelnetComControl: Boolean);
103begin
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;
142end;
143
144procedure TDeviceConnector.CommTCPServerConnect(Sender: TCommTCPServer; Pin: TCommPin);
145begin
146 TCommPin(CommHub.Pins.First).Connect(Pin);
147end;
148
149constructor TDeviceConnector.Create;
150begin
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;
163end;
164
165destructor TDeviceConnector.Destroy;
166begin
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;
177end;
178
179procedure TDeviceConnector.Assign(Source: TDeviceConnector);
180begin
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);
188end;
189
190end.
191
Note: See TracBrowser for help on using the repository browser.