source: PinConnection/UCommConnector.pas

Last change on this file was 461, checked in by chronos, 11 years ago
  • Fixed: Connection iin TCommTCPServer.
File size: 5.6 KB
Line 
1unit UCommConnector;
2
3{$mode delphi}
4
5interface
6
7uses
8 Classes, SysUtils, UCommPin, UCommSerialPort, UCommTCPClient, UCommThread,
9 UCommHub, UCommTCPServer, UCommTelnet,
10 UCommTelnetComPortOption;
11
12type
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
52implementation
53
54function TDeviceConnector.GetConnectionType: TConnectionType;
55begin
56 Result := FConnectionType;
57end;
58
59function TDeviceConnector.GetActive: Boolean;
60begin
61 Result := FActive;
62end;
63
64function TDeviceConnector.GetPin: TCommPin;
65begin
66 Result := TCommPin(CommHub.Pins.Last);
67end;
68
69procedure TDeviceConnector.SetActive(const AValue: Boolean);
70begin
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;
91end;
92
93procedure TDeviceConnector.SetConnectionType(const AValue: TConnectionType);
94begin
95 UpdateConnectionType(AValue, FTelnetComControl);
96end;
97
98procedure TDeviceConnector.SetTelnetComControl(AValue: Boolean);
99begin
100 UpdateConnectionType(FConnectionType, AValue);
101end;
102
103procedure TDeviceConnector.UpdateConnectionType(const ConnectionType: TConnectionType;
104 const ATelnetComControl: Boolean);
105begin
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;
144end;
145
146procedure TDeviceConnector.CommTCPServerConnect(Sender: TCommTCPServer; Pin: TCommPin);
147begin
148 TCommPin(CommHub.Pins.First).Connect(Pin);
149end;
150
151
152constructor TDeviceConnector.Create;
153begin
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;
166end;
167
168destructor TDeviceConnector.Destroy;
169begin
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;
180end;
181
182procedure TDeviceConnector.Assign(Source: TDeviceConnector);
183begin
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);
191end;
192
193
194
195end.
196
Note: See TracBrowser for help on using the repository browser.