-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathuuart_deprecated.pas
More file actions
429 lines (390 loc) · 12.8 KB
/
uuart_deprecated.pas
File metadata and controls
429 lines (390 loc) · 12.8 KB
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
(******************************************************************************)
(* uuart_deprecated 31.03.2012 *)
(* *)
(* Version : 0.08 *)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* Support : www.Corpsman.de *)
(* *)
(* Description : This Unit implements a Control Class for the Synaser *)
(* TBlockSerial, which allows you to easily access to the Uart *)
(* device. *)
(* *)
(* License : See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(* Warranty : There is no warranty, neither in correctness of the *)
(* implementation, nor anything other that could happen *)
(* or go wrong, use at your own risk. *)
(* *)
(* Known Issues: none *)
(* *)
(* History : 0.01 - Initial version *)
(* 0.02 - Added Compilerswitches *)
(* Added RecvByteArr, Connected *)
(* 0.03 - Added SizeRecvBuffer, RecvByte *)
(* 0.04 - Added uuart.inc *)
(* 0.05 - Added Flush *)
(* 0.06 - Added GetSerialPortNames for Linux *)
(* 0.07 - Added Stoppbit forwarding from synaser *)
(* 0.08 - Added Purge *)
(* *)
(******************************************************************************)
Unit uuart_deprecated;
{$MODE objfpc}{$H+}
(*
* Soll die uuart.pas als Library genutzt werden, dann sollte die uuart.inc
* aus dem Verzeichnis der uuart.pas gelöscht werden. Für jedes Projekt, welches
* uuart.pas einbindet. Muss dann im Projektordner eine seperate uuart.inc
* angelegt werden.
*
* Folgende Defines können/sollten definiert werden:
*
* UseLCL : Wenn die uuart Klasse unter der LCL läuft (ausgabe von Fehlern via Dialog)
* UseConsole : Wenn die uuart Klasse fehler auf die Konsole loggen soll.
*
*)
{$I uuart.inc}
Interface
Uses
Classes, SysUtils, synaser
{$IFDEF UseLCL}
, dialogs //debug
{$ENDIF}
;
Const
SB1 = synaser.SB1;
SB1andHalf = synaser.SB1andHalf;
SB2 = synaser.SB2;
Type
{ TUart }
TUart = Class
private
freceivebuffer: integer;
fserial: TBlockSerial; // Zur Kommunikation ( Uart Schnittstelle )
Function getconnected: Boolean;
Function GetLastError: integer;
Function GetReceivebuffer: integer;
Procedure setReceifebuffer(value: integer);
public
Property Connected: Boolean read getconnected; // True wenn verbunden
Property SizeRecvBuffer: integer read GetReceivebuffer write setReceifebuffer;
Property LastError: integer read GetLastError;
Constructor create; virtual; Deprecated 'Do not use this class anymore!';
Destructor destroy; override;
Function connect(Port: String; Baudrate, Bits: Integer; Parity: Char; Stop: Integer; softflow, hardflow: boolean): Boolean;
Procedure disconnect(); // Schliest eine Verbindung
Procedure Close; // Schliest eine Verbindung
Function Read2(CharCount: integer; Timeout: integer = 10): String; // Liest wenn Möglich von der Uart, aber nur CharCount Zeichen
Function Read(Timeout: Integer = 10): String; // Liest wenn möglich von der Uart
Function RecvTerminated(Timeout: Integer; Const Terminator: String): String; // Empfängt bis daten mit Terminator angekommen sind oder ein Timeout Stattfand.
Procedure RecvByteArr(Out Data: TBytes; Timeout: Integer = 10); // Empfängt beliebig viele Bytes bis Timeout Lang nichts mehr kommt
Function RecvByte(Out Error: Boolean; Timeout: Integer = 10): Byte; // Empfängt ein Byte, Error = True => Keine Daten innerhalb von Timeout empfangen
Procedure Write(Const Value: String); // Schreibt einen String
Procedure WriteByteArr(Const Data: TBytes); // Sendet einen Byte Datanstrom
(*
* Do not use this in a while loop !
* The internal buffers are updated via "?" (simply calling the Application.ProcessMessages / Application.HandleMessage / sleep does not do the trick)
*
* Only Known working method is polling via Timer or handling in OnIdle
*)
Function WaitingData(): integer;
Procedure Flush(); // Empties the Read Buffer
Procedure Purge(); // Unconditional Empty of all buffers
End;
Function GetSerialPortNames(): String; Deprecated 'use version from uuart.pas';
Implementation
Function GetSerialPortNames(): String;
{$IFDEF WINDOWS}
Begin
result := synaser.GetSerialPortNames();
{$ELSE}
Var
sl: TStringlist;
Var
Info: TSearchRec;
hdl: THandle;
b: Boolean;
Begin
sl := TStringlist.create;
If FindFirst('/dev/tty*', faSysFile, Info) = 0 Then Begin
Repeat
b := true;
Try
hdl := FileOpen('/dev/' + info.Name, fmOpenReadWrite);
If hdl = -1 Then Begin
b := false;
End;
Except
b := false;
End;
If hdl >= 0 Then Begin
FileClose(hdl);
End;
If b Then Begin
sl.Add('/dev/' + info.Name);
End;
Until FindNext(info) <> 0;
End;
FindClose(Info);
result := sl.CommaText;
sl.free;
{$ENDIF}
End;
{ TUart }
Function ErrorCodeToString(ErrorCode: integer): String;
Begin
// Die Bedeutung der Errorcodes ist hier :
// http://synapse.ararat.cz/doc/help/synaser.html
// bzw. hier :
// http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382%28v=vs.85%29.aspx
(*
* Generell bei Fehlern unter Linux, prüfen ob der User in der "tty" bzw. "dialout" gruppe ist !
*)
Case ErrorCode Of
// Es sind beileibe nicht alle aufgeführt, nur die die bisher mal auftraten ...
0: result := 'ERROR_SUCCESS';
2: result := '2 = ERROR_FILE_NOT_FOUND';
5: result := '5 = ERROR_ACCESS_DENIED';
13: result := '13 = The data is invalid.';
87: result := '87 = ERROR_INVALID_PARAMETER';
9991: Begin
result := '9991 = ERROR_ALREADY_OWNED';
{$IFDEF LINUX}
result := result + LineEnding;
result := result + 'See in "' + LockfileDirectory + '" for lockfile informations.';
{$ENDIF}
End
Else
result := inttostr(ErrorCode);
End;
End;
Function TUart.getconnected(): Boolean;
Begin
result := Assigned(fserial);
// TODO: Hier könnte man noch mehr machen ..
End;
Function TUart.GetLastError: integer;
Begin
If assigned(fserial) Then Begin
result := fserial.LastError;
End
Else Begin
result := 1;
End;
End;
Function TUart.GetReceivebuffer: integer;
Begin
result := freceivebuffer;
End;
Procedure TUart.setReceifebuffer(value: integer);
Begin
{$IFDEF LINUX}
{$IFDEF UseLCL}
showmessage('SizeRecvBuffer only valid under Windows');
{$ENDIF}
{$IFDEF UseConsole}
writeln('SizeRecvBuffer only valid under Windows');
{$ENDIF}
{$ELSE}
If assigned(fserial) Then Begin
Raise Exception.Create('Error set receivebuffer while connected.');
End
Else Begin
freceivebuffer := value;
End;
{$ENDIF}
End;
Constructor TUart.create;
Begin
Inherited create;
fserial := Nil;
freceivebuffer := 4096; // Default value from Synaser
End;
Destructor TUart.destroy;
Begin
close;
End;
Function TUart.connect(Port: String; Baudrate, Bits: Integer; Parity: Char;
Stop: Integer; softflow, hardflow: boolean): Boolean;
Begin
result := false;
If assigned(fserial) Then fserial.free;
fserial := TBlockSerial.Create;
{$IFDEF Windows}
fserial.SizeRecvBuffer := freceivebuffer;
{$ENDIF}
fserial.Connect(Port);
Sleep(100); // Das Device kann zum Teil recht langsam sein, mit dem Sleep sorgen wir dafür, dass auch sicher connected ist.
If (fserial.LastError <> 0) Then Begin
// Raise Exception.Create('Error, could not connect to device error number : ' + ErrorCodeToString(fserial.LastError));
{$IFDEF UseLCL}
showmessage('Error, could not connect to device error number : ' + ErrorCodeToString(fserial.LastError));
{$ENDIF}
{$IFDEF UseConsole}
writeln('Error, could not connect to device error number : ' + ErrorCodeToString(fserial.LastError));
{$ENDIF}
close();
exit;
End;
// fserial.Config(Baudrate, 8, 'N', SB1, false, false);
fserial.Config(Baudrate, Bits, Parity, Stop, softflow, hardflow);
Sleep(100); // Das Device kann zum Teil recht langsam sein, mit dem Sleep sorgen wir dafür, dass auch sicher connected ist.
If (fserial.LastError <> 0) Then Begin
// Raise Exception.Create(format('Error, could not config device erronumber : %d', [fserial.LastError]));
{$IFDEF UseLCL}
showmessage('Error, could not config device error number : ' + ErrorCodeToString(fserial.LastError));
{$ENDIF}
{$IFDEF UseConsole}
writeln('Error, could not config device error number : ' + ErrorCodeToString(fserial.LastError));
{$ENDIF}
close();
Exit;
End;
result := true;
End;
Procedure TUart.disconnect();
Begin
close();
End;
Function TUart.Read2(CharCount: integer; Timeout: integer): String;
Var
b: Boolean;
c: Char;
Begin
result := '';
If Not assigned(fserial) Then exit;
b := true;
While b Do Begin
c := chr(fserial.RecvByte(Timeout));
If fserial.LastError = 0 Then Begin
result := result + c;
CharCount := CharCount - 1;
If Charcount <= 0 Then
exit;
End
Else Begin
b := false;
End;
End;
End;
Function TUart.Read(Timeout: Integer): String;
Var
b: Boolean;
c: Char;
Begin
result := '';
If Not assigned(fserial) Then exit;
b := true;
While b Do Begin
c := chr(fserial.RecvByte(Timeout));
If fserial.LastError = 0 Then Begin
result := result + c;
End
Else Begin
b := false;
End;
End;
End;
Procedure TUart.WriteByteArr(Const Data: TBytes);
Var
i: integer;
Begin
If Not assigned(fserial) Then exit;
For i := 0 To high(data) Do
fserial.SendByte(data[i]);
End;
Function TUart.WaitingData(): integer;
Begin
result := 0;
If assigned(fserial) Then result := fserial.WaitingData;
End;
Procedure TUart.Flush();
Var
i: integer;
Begin
// Alles was Da ist Lesen und Wegschmeisen
If assigned(fserial) Then Begin
For i := 0 To fserial.WaitingData - 1 Do
fserial.RecvByte(0);
End;
End;
Procedure TUart.Purge();
Begin
If assigned(fserial) Then Begin
fserial.Purge;
End;
End;
Function TUart.RecvTerminated(Timeout: Integer; Const Terminator: String
): String;
Begin
If assigned(fserial) Then Begin
result := fserial.RecvTerminated(Timeout, Terminator);
End
Else
result := '';
End;
Procedure TUart.RecvByteArr(Out Data: TBytes; Timeout: Integer);
Var
b: Boolean;
c: Byte;
cnt: integer;
Begin
If Not assigned(fserial) Then Begin
setlength(data, 0);
exit;
End;
cnt := 0;
setlength(data, 100);
b := true;
While b Do Begin
c := fserial.RecvByte(Timeout);
If fserial.LastError = 0 Then Begin
data[cnt] := c;
inc(cnt);
If cnt > high(data) Then Begin
setlength(data, high(data) + 101);
End;
End
Else Begin
b := false;
End;
End;
setlength(data, cnt);
End;
Function TUart.RecvByte(Out Error: Boolean; Timeout: Integer): Byte;
Begin
If assigned(fserial) Then Begin
result := fserial.RecvByte(Timeout);
error := fserial.LastError <> 0;
End
Else Begin
error := true;
result := 0;
End;
End;
Procedure TUart.Write(Const Value: String);
Var
data: TBytes;
i: Integer;
Begin
setlength(data, length(value));
For i := 0 To high(data) Do Begin
data[i] := ord(value[i + 1]);
End;
WriteByteArr(data);
setlength(data, 0);
End;
Procedure TUart.Close;
Begin
If assigned(fserial) Then Begin
fserial.free;
End;
fserial := Nil;
End;
End.