-
Notifications
You must be signed in to change notification settings - Fork 0
/
U_SimpleComPort.pas
398 lines (357 loc) · 10.6 KB
/
U_SimpleComPort.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
unit U_SimpleComPort;
interface
uses Windows, SysUtils, Classes, StrUtils;
type
TByteSize = (bs5, bs6, bs7, bs8);
TParity = (paNone, paOdd, paEven, paMark, paSpace);
TStopBits = (sb1, sb1_5, sb2);
TSComPort = class
private
FConnected: boolean;
FHandle: THandle;
FTxBufferSize: cardinal;
FBaudRate: cardinal;
FParity: TParity;
FReadInterval: integer;
FStopBits: TStopBits;
FReadTotalMultiplier: integer;
FReadTotalConstant: integer;
FWriteTotalMultiplier: integer;
FWriteTotalConstant: integer;
FByteSize: TByteSize;
FFlags: integer;
FRxBufferSize: cardinal;
FPort: string;
public
constructor Create(AOwner: TComponent); overload;
constructor Create(); overload;
destructor Destroy; override;
function Read(var Buffer; Count: integer): integer;
function ReadStream(aStream: TMemoryStream; aCount: integer): integer;
function Write(const Buffer; Count: integer): integer;
function WriteStream(aStream: TMemoryStream): integer;
procedure FlushBuffer;
function Open: boolean;
function Close: boolean;
property Connected: boolean read FConnected;
property Port: string read FPort write FPort;
property BaudRate: cardinal read FBaudRate write FBaudRate default 9600;
property ByteSize: TByteSize read FByteSize write FByteSize default bs8;
property Parity: TParity read FParity write FParity default paNone;
property Flags: integer read FFlags write FFlags default 1;
property StopBits: TStopBits read FStopBits write FStopBits default sb1;
property ReadInterval: integer read FReadInterval write FReadInterval default 1000;
property ReadTotalMultiplier: integer read FReadTotalMultiplier write FReadTotalMultiplier default 0;
property ReadTotalConstant: integer read FReadTotalConstant write FReadTotalConstant default 1000;
property WriteTotalMultiplier: integer read FWriteTotalMultiplier write FWriteTotalMultiplier default 0;
property WriteTotalConstant: integer read FWriteTotalConstant write FWriteTotalConstant default 1000;
property InBufSize: cardinal read FRxBufferSize write FRxBufferSize default 2048;
property OutBufSize: cardinal read FTxBufferSize write FTxBufferSize default 2048;
end;
procedure EnumComPorts(Ports: TStrings);
implementation
procedure EnumComPorts(Ports: TStrings);
var
KeyHandle: HKEY;
ErrCode, Index: integer;
ValueName: widestring;
Data: ansistring;
ValueLen, DataLen, ValueType: DWORD;
TmpPorts: TStringList;
begin
ErrCode := RegOpenKeyEx(HKEY_LOCAL_MACHINE, 'HARDWARE\DEVICEMAP\SERIALCOMM', 0, KEY_READ, KeyHandle);
if ErrCode <> ERROR_SUCCESS then
begin
// raise EComPort.Create(CEMess[15]);
Ports.Clear;
exit;
end;
TmpPorts := TStringList.Create;
TmpPorts.BeginUpdate;
try
Index := 0;
repeat
ValueLen := 256;
DataLen := 256;
SetLength(ValueName, ValueLen);
SetLength(Data, DataLen);
ErrCode := RegEnumValue(KeyHandle, Index, pwidechar(ValueName),
{$IFDEF VER120}
cardinal(ValueLen),
{$ELSE}
ValueLen,
{$ENDIF}
nil, @ValueType, PByte(pansichar(Data)), @DataLen);
if ErrCode = ERROR_SUCCESS then
begin
SetLength(Data, DataLen);
TmpPorts.Add(ReplaceStr(string(Data), #0, ''));
Inc(Index);
end
else
if ErrCode <> ERROR_NO_MORE_ITEMS then
begin
// raise EComPort.Create(CEMess[15]);
break;
end;
until (ErrCode <> ERROR_SUCCESS);
TmpPorts.Sort;
Ports.Assign(TmpPorts);
finally
RegCloseKey(KeyHandle);
TmpPorts.EndUpdate;
TmpPorts.Free;
end;
end;
function CloseCOMPort(var hFile: THandle): boolean; forward;
function OpenCOMPort(var hFile: THandle; const aPortName: string): boolean;
begin
{ First step is to open the communications device for read/write.
This is achieved using the Win32 'CreateFile' function.
If it fails, the function returns false.
}
hFile := CreateFile(pchar('\\.\' + aPortName),
GENERIC_READ or GENERIC_WRITE,
0,
nil,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
if hFile = INVALID_HANDLE_VALUE then
Result := False
else
begin
// Ñáðîñ áóôåðîâ ïîðòà.
Result := PurgeComm(hFile, PURGE_TXABORT or PURGE_RXABORT or PURGE_TXCLEAR or PURGE_RXCLEAR);
if (not Result) then
CloseCOMPort(hFile);
// Ñáðîñ çíà÷åíèé ðåãèñòðîâ ïîðòà.
if (hFile <> INVALID_HANDLE_VALUE) then
begin
Result := ClearCommBreak(hFile);
if (not Result) then
CloseCOMPort(hFile);
end;
end;
end;
function SetupCOMPort(hFile: THandle;
aBaudRate: cardinal = 9600; aByteSize: TByteSize = bs8; aParity: TParity = paNone;
aFlags: integer = 1;
aStopBits: TStopBits = sb1;
aReadInterval: integer = 1000;
aReadTotalMultiplier: integer = 0;
aReadTotalConstant: integer = 1000;
aWriteTotalMultiplier: integer = 0;
aWriteTotalConstant: integer = 1000;
aRxBufferSize: cardinal = 2048;
aTxBufferSize: cardinal = 2048): boolean;
var
DCB: TDCB;
// Config: string;
CommTimeouts: TCommTimeouts;
begin
{ We assume that the setup to configure the setup works fine.
Otherwise the function returns false.
wir gehen davon aus das das Einstellen des COM Ports funktioniert.
sollte dies fehlschlagen wird der Ruckgabewert auf "FALSE" gesetzt.
}
Result := SetupComm(hFile, aRxBufferSize, aTxBufferSize);
if (Result) then
begin
Result := GetCommState(hFile, DCB);
if (Result) then
begin
// define the baudrate, parity,...
// hier die Baudrate, Paritat usw. konfigurieren
// Config := 'baud=9600 parity=n data=8 stop=1';
// if not BuildCommDCB(@Config[1], DCB) then
// Result := False;
FillChar(DCB, SizeOf(TDCB), 0);
DCB.DCBlength := SizeOf(TDCB);
DCB.BaudRate := aBaudRate;
DCB.ByteSize := Ord(TByteSize(aByteSize)) + 5;
DCB.Flags := 1;
if aParity <> paNone then
DCB.Flags := DCB.Flags or 2;
DCB.Parity := Ord(TParity(aParity));
DCB.StopBits := Ord(TStopBits(aStopBits));
DCB.XonChar := #17;
DCB.XoffChar := #19;
Result := SetCommState(hFile, DCB);
if (Result) then
begin
with CommTimeouts do
begin
ReadIntervalTimeout := aReadInterval;
ReadTotalTimeoutMultiplier := aReadTotalMultiplier;
ReadTotalTimeoutConstant := aReadTotalConstant;
WriteTotalTimeoutMultiplier := aWriteTotalMultiplier;
WriteTotalTimeoutConstant := aWriteTotalConstant;
end;
Result := SetCommTimeouts(hFile, CommTimeouts);
end;
end;
end;
end;
function CloseCOMPort(var hFile: THandle): boolean;
begin
// finally close the COM Port!
// nicht vergessen den COM Port wieder zu schliessen!
Result := CloseHandle(hFile);
hFile := INVALID_HANDLE_VALUE;
end;
{
The following is an example of using the 'WriteFile' function
to write data to the serial port.
}
function WriteComPort(var hFile: THandle; const Buffer; Count: cardinal): cardinal;
var
comStat: TCOMSTAT;
e: cardinal;
begin
if not WriteFile(hFile, Buffer, Count, Result, nil) then
begin
CloseCOMPort(hFile);
end
else
ClearCommError(hFile, e, @comStat);
end;
{
The following is an example of using the 'ReadFile' function to read
data from the serial port.
}
function ReadComPort(var hFile: THandle; var aBuffer; aLength: cardinal): cardinal;
var
comStat: TCOMSTAT;
e: cardinal;
begin
Result := 0;
if not ReadFile(hFile, aBuffer, aLength, Result, nil) then
begin
{ Raise an exception }
CloseCOMPort(hFile);
end
else
ClearCommError(hFile, e, @comStat);
end;
{ TSComPort }
constructor TSComPort.Create(AOwner: TComponent);
begin
Create;
end;
function TSComPort.Close: boolean;
begin
Result := true;
if (FConnected) then
begin
FConnected := False;
Result := CloseCOMPort(FHandle)
end;
end;
constructor TSComPort.Create;
begin
inherited;
FConnected := False;
FHandle := 0;
end;
destructor TSComPort.Destroy;
begin
Close;
inherited;
end;
procedure TSComPort.FlushBuffer;
begin
FlushFileBuffers(FHandle);
end;
function TSComPort.Open: boolean;
begin
if (FConnected) then
CloseCOMPort(FHandle);
FConnected := OpenCOMPort(FHandle, FPort);
Result := FConnected;
if (FConnected) then
begin
Result := SetupCOMPort(FHandle, FBaudRate, FByteSize, FParity, FFlags,
FStopBits, FReadInterval, FReadTotalMultiplier, FReadTotalConstant,
FWriteTotalMultiplier, FWriteTotalConstant, FRxBufferSize, FTxBufferSize);
if (not Result) then
begin
CloseCOMPort(FHandle);
FConnected := False;
end;
end;
end;
function TSComPort.Read(var Buffer; Count: integer): integer;
var
zBuff: array [0 .. 4095] of byte;
begin
FConnected := FConnected and (FHandle <> INVALID_HANDLE_VALUE);
if FConnected then
begin
Result := ReadComPort(FHandle, zBuff, Count);
move(zBuff, Buffer, Count);
FConnected := FHandle <> INVALID_HANDLE_VALUE;
end
else
Result := 0;
end;
function TSComPort.ReadStream(aStream: TMemoryStream; aCount: integer): integer;
var
// zPtr: PByte;
i: integer;
aValue: byte;
begin
{ if (aCount > 0) then
begin
aStream.Size := aStream.Size + aCount;
zPtr := aStream.Memory;
Inc(zPtr, aStream.Position);
Result := Read(zPtr^, aCount);
if (Result < aCount) then
aStream.Size := aStream.Size - (aCount - Result);
end
else
Result := 0;
}
Result := 0;
for i := 0 to aCount - 1 do
if (Connected) then
begin
if (Read(aValue, 1) = 1) then
begin
aStream.Write(aValue, 1);
Inc(Result);
end
else
break;
end;
end;
function TSComPort.Write(const Buffer; Count: integer): integer;
begin
FConnected := FConnected and (FHandle <> INVALID_HANDLE_VALUE);
if FConnected then
begin
Result := WriteComPort(FHandle, Buffer, Count);
FConnected := FHandle <> INVALID_HANDLE_VALUE;
end
else
Result := 0;
end;
function TSComPort.WriteStream(aStream: TMemoryStream): integer;
var
i: integer;
aValue: byte;
begin
// Result := Write(aStream.Memory^, aStream.Size);
Result := 0;
for i := aStream.Position to aStream.Size - 1 do
if (Connected) then
begin
aStream.Read(aValue, 1);
if (Write(aValue, 1) = 1) then
Inc(Result)
else
break;
end;
end;
end.