-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComConnector.pas
321 lines (285 loc) · 8.58 KB
/
ComConnector.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
unit ComConnector;
interface
uses Windows, CPort, PacketParcer, Vodopad.Timer, SysUtils, Classes, ComConnectorInterface;
type
TComConnector = class abstract(TComponent, IComConnector)
private
FAsyncPtr: PAsync;
fPort : byte;
fComPort : TComport;
fPacketParser : TPacketParcer;
fChallengeTimer, fTimeOutTimer : TvdTimer;
fDSR : TDSR;
fFlowControl : boolean;
protected
function PortWrite(const Buffer; Count : LongWord) : integer; stdcall;
function GetDSR: pDSR; stdcall;
procedure onResultDataPacket(const Buffer; Count : Word); virtual;
procedure DoChallenge(Sender: TObject); virtual;
procedure OnTimeOut(Sender : TObject); virtual;
procedure OnDSRChange(Sender: TObject; OnOff: Boolean); virtual;
function GetSyncMethod: TPortSyncMethod; stdcall;
procedure SetSyncMethod(const Value: TPortSyncMethod);stdcall;
public
constructor Create(AOwner : TComponent); override;
destructor Destroy; override;
property ComPort : TComport read fComPort;
property SyncMethod: TPortSyncMethod read GetSyncMethod write SetSyncMethod;
function FlowControl: boolean;stdcall;
function Port: Byte; stdcall;
property DSR : pDSR read GetDSR;
procedure ConfigPort(APort : byte; ABaud : LongWord;
AdtrEnable, ArtsEnable, AFlowControl : boolean); stdcall;
procedure ConfigParser(const StartMarker; StartSize : byte;
const StopMarker; StopSize : byte;
AIncludeMarkers : boolean = false); stdcall;
procedure SetBufferSize(AInput, AOutput : LongWord); stdcall;
function PortOpenOk : boolean; stdcall;
procedure PortClose; virtual; stdcall;
function IsConnected : boolean; stdcall;
function CallChallenge(const After : LongWord) : boolean; virtual; stdcall;
procedure SetTimeOutMode(const ATimeOut : LongWord); stdcall;
procedure StopTimeOut; stdcall;
procedure StopTimers; stdcall;
end;
procedure EnumComPorts(Ports: TStringList);
function ComNameToByte(const ComName :String):byte;
function StringListComparePorts(List: TStringList; Index1, Index2: Integer): Integer;
implementation
function ComNameToByte(const ComName :String):byte;
var
tmpName : string;
R : integer;
begin
result := 0;
R := 0;
tmpName := trim(ComName);
if (pos('COM', tmpName) = 1) and (Length(tmpName) > 3)then
begin
Delete(tmpName, 1, 3);
if (Length(tmpName) > 0) and (Length(tmpName) < 3) then
try
R := StrToInt(tmpName);
except
end;
end;
if (R > 0) and (R < 255) then
result := byte(R);
end;
function StringListComparePorts(List: TStringList; Index1, Index2: Integer): Integer;
begin
Result :=
ComNameToByte(List.Strings[index1])-ComNameToByte(List.Strings[Index2]);
end;
procedure EnumComPorts(Ports: TStringList);
begin
CPort.EnumComPorts(Ports);
Ports.CustomSort(StringListComparePorts);
end;
{ TComConnector }
constructor TComConnector.Create(AOwner : TComponent);
begin
inherited Create(AOwner);
VCLComObject := nil;
fChallengeTimer := TvdTimer.Create(self);
fChallengeTimer.OnTimer := DoChallenge;
fChallengeTimer.Enabled := false;
fTimeOutTimer := TvdTimer.Create(self);
fTimeOutTimer.OnTimer := OnTimeOut;
fTimeOutTimer.Enabled := false;
fComPort := TComport.Create(self);
fDSR.State := true;
fFlowControl := false;
SyncMethod := ComConnectorInterface.smThreadSync;
fComPort.BaudRate := br38400;
fPacketParser := TPacketParcer.Create(ComPort);
fPacketParser.OnPacket := onResultDataPacket;
FComPort.OnDSRChange := OnDSRChange;
New(FAsyncPtr);
FillChar(FAsyncPtr^.Overlapped, SizeOf(TOverlapped), 0);
FAsyncPtr^.Overlapped.hEvent := CreateEvent(nil, True, True, nil);
end;
destructor TComConnector.Destroy;
begin
StopTimers;
CloseHandle(FAsyncPtr^.Overlapped.hEvent);
Dispose(FAsyncPtr);
FAsyncPtr := nil;
FreeAndNil(fPacketParser);
inherited Destroy;
end;
procedure TComConnector.DoChallenge(Sender: TObject);
begin
end;
function TComConnector.FlowControl: Boolean;
begin
result := fFlowControl;
end;
function TComConnector.GetDSR: pDSR;
begin
Result := @fDSR;
end;
function TComConnector.GetSyncMethod: TPortSyncMethod;
begin
Result := TPortSyncMethod(Self.fComPort.SyncMethod);
end;
function TComConnector.Port: Byte;
begin
result := fPort;
end;
function TComConnector.CallChallenge(const After : LongWord) : boolean;
begin
result := false;
if fTimeOutTimer.Enabled or fChallengeTimer.Enabled then
exit;
fChallengeTimer.Enabled := false;
fChallengeTimer.Interval := After;
fChallengeTimer.Enabled := IsConnected;
result := fChallengeTimer.Enabled;
end;
procedure TComConnector.ConfigParser(const StartMarker; StartSize : Byte;
const StopMarker; StopSize : Byte;
AIncludeMarkers : Boolean = false);
begin
fPacketParser.SetStartMarker(StartMarker, StartSize);
fPacketParser.SetStopMarker(StopMarker, StopSize);
fPacketParser.IncludeMarkers := AIncludeMarkers;
end;
procedure TComConnector.ConfigPort(APort: byte; ABaud: LongWord;
AdtrEnable, ArtsEnable, AFlowControl : Boolean);
var vTestHandle : THandle;
begin
if IsConnected then
exit;
fPort := APort;
try
vTestHandle := CreateFile(PWideChar(Format('\\.\COM%d',[fPort])),
GENERIC_READ or GENERIC_WRITE,
0,
nil,
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if vTestHandle = INVALID_HANDLE_VALUE then
Exit;
finally
CloseHandle(vTestHandle);
end;
FComPort.Port := Format('COM%u', [fPort]);
case ABaud of
110: fComPort.BaudRate := br110;
300: fComPort.BaudRate := br300;
600: fComPort.BaudRate := br600;
1200: fComPort.BaudRate := br1200;
2400: fComPort.BaudRate := br2400;
4800: fComPort.BaudRate := br4800;
9600: fComPort.BaudRate := br9600;
14400: fComPort.BaudRate := br14400;
19200: fComPort.BaudRate := br19200;
34800: fComPort.BaudRate := br38400;
56000: fComPort.BaudRate := br56000;
57600: fComPort.BaudRate := br57600;
115200: fComPort.BaudRate := br115200;
128000: fComPort.BaudRate := br128000;
256000: fComPort.BaudRate := br256000;
else
begin
fComPort.BaudRate := brCustom;
fComPort.CustomBaudRate := ABaud;
end;
end;
if AdtrEnable then
FComport.FlowControl.ControlDTR := dtrEnable
else
FComport.FlowControl.ControlDTR := dtrDisable;
if ArtsEnable then
fComPort.FlowControl.ControlRTS := rtsEnable
else
fComPort.FlowControl.ControlRTS := rtsDisable;
fFlowControl := AFlowControl;
if fFlowControl then
fDSR.State := True;
end;
function TComConnector.IsConnected: Boolean;
begin
result := FComPort.Connected;
end;
procedure TComConnector.OnDSRChange(Sender: TObject; OnOff: Boolean);
begin
fDSR.State := OnOff;
if fFlowControl and not fDSR.State then
fDSR.OffTime := now;
end;
procedure TComConnector.onResultDataPacket(const Buffer; Count: Word);
begin
end;
procedure TComConnector.OnTimeOut(Sender: TObject);
begin
end;
procedure TComConnector.StopTimeOut;
begin
fTimeOutTimer.Enabled := false;
end;
procedure TComConnector.StopTimers;
begin
fChallengeTimer.Enabled := false;
fTimeOutTimer.Enabled := false;
end;
procedure TComConnector.PortClose;
begin
StopTimers;
FComPort.Close;
end;
function TComConnector.PortOpenOk: Boolean;
var
vTestHandle : THandle;
begin
try
try
vTestHandle := CreateFile(PWideChar('\\.\' + FComPort.Port),
GENERIC_READ or GENERIC_WRITE,
0,
nil,
OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
result := vTestHandle <> INVALID_HANDLE_VALUE;
if not Result then
Exit;
finally
CloseHandle(vTestHandle);
end;
FComPort.Open;
result := true;
except
result := false;
end;
end;
function TComConnector.PortWrite(const Buffer; Count: LongWord) : integer;
begin
try
FAsyncPtr^.Data := nil;
FAsyncPtr^.Size := 0;
FComPort.WriteAsync(Buffer, Integer(Count), FAsyncPtr);
Result := FComPort.WaitForAsync(FAsyncPtr);
finally
if FAsyncPtr^.Data <> nil then
FreeMem(FAsyncPtr^.Data);
FAsyncPtr^.Data := nil;
FAsyncPtr^.Size := 0;
end;
end;
procedure TComConnector.SetBufferSize(AInput, AOutput: LongWord);
begin
fComPort.Buffer.InputSize := AInput;
fComPort.Buffer.OutputSize := AOutput;
end;
procedure TComConnector.SetSyncMethod(const Value: TPortSyncMethod);
begin
Self.fComPort.SyncMethod := TSyncMethod(Value);
end;
procedure TComConnector.SetTimeOutMode(const ATimeOut: LongWord);
begin
fPacketParser.ResetBuffer;
fTimeOutTimer.Enabled := false;
fTimeOutTimer.Interval := ATimeOut;
fTimeOutTimer.Enabled := true;
fChallengeTimer.Enabled := false;
end;
end.