source: trunk/Packages/PinConnection/UCommConnector.pas

Last change on this file was 23, checked in by chronos, 12 years ago
  • Přidáno: Rozpracovaná třídy pro komunikaci s přístupovým terminálem BF-630. Přiložen popis protokolu.
File size: 5.3 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 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
51implementation
52
53function TDeviceConnector.GetConnectionType: TConnectionType;
54begin
55 Result := FConnectionType;
56end;
57
58function TDeviceConnector.GetActive: Boolean;
59begin
60 Result := FActive;
61end;
62
63function TDeviceConnector.GetPin: TCommPin;
64begin
65 Result := TCommPin(CommHub.Pins.Last);
66end;
67
68procedure TDeviceConnector.SetActive(const AValue: Boolean);
69begin
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;
90end;
91
92procedure TDeviceConnector.SetConnectionType(const AValue: TConnectionType);
93begin
94 UpdateConnectionType(AValue, FTelnetComControl);
95end;
96
97procedure TDeviceConnector.SetTelnetComControl(AValue: Boolean);
98begin
99 UpdateConnectionType(FConnectionType, AValue);
100end;
101
102procedure TDeviceConnector.UpdateConnectionType(const ConnectionType: TConnectionType;
103 const ATelnetComControl: Boolean);
104begin
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;
143end;
144
145
146constructor TDeviceConnector.Create;
147begin
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;
160end;
161
162destructor TDeviceConnector.Destroy;
163begin
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;
174end;
175
176procedure TDeviceConnector.Assign(Source: TDeviceConnector);
177begin
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);
185end;
186
187
188
189end.
190
Note: See TracBrowser for help on using the repository browser.