-
Notifications
You must be signed in to change notification settings - Fork 6
/
lazserial.pas
481 lines (390 loc) · 11.8 KB
/
lazserial.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
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
{ LazSerial v0.1
Serial Port Component for Lazarus
by Jurassic Pork 03/2013
This library is Free software; you can rediStribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at your
option) any later version.
This program is diStributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; withOut even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
for more details.
You should have received a Copy of the GNU Library General Public License
along with This library; if not, Write to the Free Software Foundation,
Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. }
{ Based on }
{ SdpoSerial v0.1.4
CopyRight (C) 2006-2010 Paulo Costa
paco@fe.up.pt
}
{ Synaser library by Lukas Gebauer }
{ TcomPort component }
{ features :
Changed : baudrate values.
stop bits new value : 1.5
new event : onstatus
new property FRcvLineCRLF : if this property is true, you use RecvString
in place of RecvPacket when you read data from the port.
new procedure ShowSetupDialog to open a port settings form :
the device combobox contain the enumerated ports.
new procedure to enumerate real serial port on linux ( in synaser).
Demo : a simulator of gps serial port + serial port receiver :
you can send NMEA frames ( GGA GLL RMC°) to the opened serial port
(start gps simulator). You can change speed and heading.
In the memo you can see what is received from the opened serial port.
In the status bar you can see the status events.
}
unit LazSerial;
{$mode objfpc}{$H+}
interface
uses
{$IFDEF UNIX }
Classes,
{$IFDEF UseCThreads}
cthreads,
{$ENDIF}
{$ELSE}
Windows, Classes, //registry,
{$ENDIF}
SysUtils, synaser, LResources, Forms, Controls, Graphics, Dialogs;
type
TBaudRate=(br___110,br___300, br___600, br__1200, br__2400, br__4800,
br__9600,br_14400, br_19200, br_38400,br_56000, br_57600,
br115200,br128000, br230400,br256000, br460800, br921600);
TDataBits=(db8bits,db7bits,db6bits,db5bits);
TParity=(pNone,pOdd,pEven,pMark,pSpace);
TFlowControl=(fcNone,fcXonXoff,fcHardware);
TStopBits=(sbOne,sbOneAndHalf,sbTwo);
TModemSignal = (msRI,msCD,msCTS,msDSR);
TModemSignals = Set of TModemSignal;
TStatusEvent = procedure(Sender: TObject; Reason: THookSerialReason; const Value: string) of object;
const
ConstsBaud: array[TBaudRate] of integer=(110,
300, 600, 1200, 2400, 4800, 9600,14400, 19200, 38400,56000, 57600,
115200,128000,230400,256000, 460800, 921600);
ConstsBits: array[TDataBits] of integer=(8, 7 , 6, 5);
ConstsParity: array[TParity] of char=('N', 'O', 'E', 'M', 'S');
ConstsStopBits: array[TStopBits] of integer=(SB1,SB1AndHalf,SB2);
type
TLazSerial = class;
TComPortReadThread=class(TThread)
public
MustDie: boolean;
Owner: TLazSerial;
protected
procedure CallEvent;
procedure Execute; override;
published
property Terminated;
end;
{ TLazSerial }
TLazSerial = class(TComponent)
private
FActive: boolean;
FSynSer: TBlockSerial;
FDevice: string;
FBaudRate: TBaudRate;
FDataBits: TDataBits;
FParity: TParity;
FStopBits: TStopBits;
FSoftflow, FHardflow: boolean;
FFlowControl: TFlowControl;
FRcvLineCRLF : Boolean;
FOnRxData: TNotifyEvent;
FOnStatus: TStatusEvent;
ReadThread: TComPortReadThread;
procedure DeviceOpen;
procedure DeviceClose;
procedure ComException(str: string);
protected
procedure SetActive(state: boolean);
procedure SetBaudRate(br: TBaudRate);
procedure SetDataBits(db: TDataBits);
procedure SetParity(pr: TParity);
procedure SetFlowControl(fc: TFlowControl);
procedure SetStopBits(sb: TStopBits);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Open;
procedure Close;
// show a port settings dialog form
procedure ShowSetupDialog;
// read data from port
function DataAvailable: boolean;
function ReadData: string;
// function ReadBuffer(var buf; size: integer): integer;
// write data to port
function WriteData(data: string): integer;
function WriteBuffer(var buf; size: integer): integer;
// read pin states
function ModemSignals: TModemSignals;
function GetDSR: boolean;
function GetCTS: boolean;
function GetRing: boolean;
function GetCarrier: boolean;
// set pin states
// procedure SetRTSDTR(RtsState, DtrState: boolean);
procedure SetDTR(OnOff: boolean);
procedure SetRTS(OnOff: boolean);
// procedure SetBreak(OnOff: boolean);
published
property Active: boolean read FActive write SetActive;
property BaudRate: TBaudRate read FBaudRate write SetBaudRate; // default br115200;
property DataBits: TDataBits read FDataBits write SetDataBits;
property Parity: TParity read FParity write SetParity;
property FlowControl: TFlowControl read FFlowControl write SetFlowControl;
property StopBits: TStopBits read FStopBits write SetStopBits;
property SynSer: TBlockSerial read FSynSer write FSynSer;
property Device: string read FDevice write FDevice;
property RcvLineCRLF: Boolean read FRcvLineCRLF write FRcvLineCRLF;
property OnRxData: TNotifyEvent read FOnRxData write FOnRxData;
property OnStatus: TStatusEvent read FOnStatus write FOnStatus;
end;
procedure Register;
implementation
uses LazSerialSetup;
{ TLazSerial }
procedure TLazSerial.Close;
begin
Active:=false;
end;
procedure TLazSerial.DeviceClose;
begin
// flush device
if FSynSer.Handle<>INVALID_HANDLE_VALUE then begin
FSynSer.Flush;
FSynSer.Purge;
end;
// stop capture thread
if ReadThread<>nil then begin
ReadThread.FreeOnTerminate:=false;
ReadThread.MustDie:= true;
while not ReadThread.Terminated do begin
Application.ProcessMessages;
end;
ReadThread.Free;
ReadThread:=nil;
end;
// close device
if FSynSer.Handle<>INVALID_HANDLE_VALUE then begin
FSynSer.Flush;
FSynSer.CloseSocket;
end;
end;
constructor TLazSerial.Create(AOwner: TComponent);
begin
inherited;
//FHandle:=-1;
ReadThread:=nil;
FSynSer:=TBlockSerial.Create;
FSynSer.LinuxLock:=false;
FHardflow:=false;
FSoftflow:=false;
FFlowControl:=fcNone;
{$IFDEF LINUX}
FDevice:='/dev/ttyS0';
{$ELSE}
FDevice:='COM1';
{$ENDIF}
FRcvLineCRLF := False;;
// FBaudRate:=br115200;
end;
function TLazSerial.DataAvailable: boolean;
begin
if FSynSer.Handle=INVALID_HANDLE_VALUE then begin
result:=false;
exit;
end;
result:=FSynSer.CanReadEx(0);
end;
destructor TLazSerial.Destroy;
begin
Close;
FSynSer.Free;
inherited;
end;
procedure TLazSerial.Open;
begin
// Initialize OnStatus;
if Assigned(OnStatus) then SynSer.OnStatus := OnStatus;
Active:=true;
end;
procedure TLazSerial.ShowSetupDialog;
begin
EditComPort(self);
end;
procedure TLazSerial.DeviceOpen;
begin
FSynSer.Connect(FDevice);
if FSynSer.Handle=INVALID_HANDLE_VALUE then
raise Exception.Create('Could not open device '+ FSynSer.Device);
FSynSer.Config(ConstsBaud[FBaudRate],
ConstsBits[FDataBits],
ConstsParity[FParity],
ConstsStopBits[FStopBits],
FSoftflow, FHardflow);
// Launch Thread
ReadThread := TComPortReadThread.Create(true);
ReadThread.Owner := Self;
ReadThread.MustDie := false;
// ReadThread.Resume; --> deprecated
ReadThread.Start;
end;
function TLazSerial.ReadData: string;
begin
result:='';
if FSynSer.Handle=INVALID_HANDLE_VALUE then
ComException('can not read from a closed port.');
if FRcvLineCRLF then
result:=FSynSer.RecvString(0)
else
result:=FSynSer.RecvPacket(0);
end;
procedure TLazSerial.SetActive(state: boolean);
begin
if state=FActive then exit;
if state then DeviceOpen
else DeviceClose;
FActive:=state;
end;
procedure TLazSerial.SetBaudRate(br: TBaudRate);
begin
if FSynSer.Handle<>INVALID_HANDLE_VALUE then begin
FSynSer.Config(ConstsBaud[FBaudRate], ConstsBits[FDataBits], ConstsParity[FParity],
ConstsStopBits[FStopBits], FSoftflow, FHardflow);
end;
FBaudRate:=br;
end;
procedure TLazSerial.SetDataBits(db: TDataBits);
begin
if FSynSer.Handle<>INVALID_HANDLE_VALUE then begin
FSynSer.Config(ConstsBaud[FBaudRate], ConstsBits[FDataBits], ConstsParity[FParity],
ConstsStopBits[FStopBits], FSoftflow, FHardflow);
end;
FDataBits:=db;
end;
procedure TLazSerial.SetFlowControl(fc: TFlowControl);
begin
if fc=fcNone then begin
FSoftflow:=false;
FHardflow:=false;
end else if fc=fcXonXoff then begin
FSoftflow:=true;
FHardflow:=false;
end else if fc=fcHardware then begin
FSoftflow:=false;
FHardflow:=true;
end;
if FSynSer.Handle<>INVALID_HANDLE_VALUE then begin
FSynSer.Config(ConstsBaud[FBaudRate], ConstsBits[FDataBits], ConstsParity[FParity],
ConstsStopBits[FStopBits], FSoftflow, FHardflow);
end;
FFlowControl:=fc;
end;
{
procedure TLazSerial.SetFlowControl(fc: TFlowControl);
begin
if FHandle<>-1 then begin
if fc=fcNone then CurTermIO.c_cflag:=CurTermIO.c_cflag and (not CRTSCTS)
else CurTermIO.c_cflag:=CurTermIO.c_cflag or CRTSCTS;
tcsetattr(FHandle,TCSADRAIN,CurTermIO);
end;
FFlowControl:=fc;
end;
}
procedure TLazSerial.SetParity(pr: TParity);
begin
if FSynSer.Handle<>INVALID_HANDLE_VALUE then begin
FSynSer.Config(ConstsBaud[FBaudRate], ConstsBits[FDataBits], ConstsParity[FParity],
ConstsStopBits[FStopBits], FSoftflow, FHardflow);
end;
FParity:=pr;
end;
procedure TLazSerial.SetStopBits(sb: TStopBits);
begin
if FSynSer.Handle<>INVALID_HANDLE_VALUE then begin
FSynSer.Config(ConstsBaud[FBaudRate], ConstsBits[FDataBits], ConstsParity[FParity],
ConstsStopBits[FStopBits], FSoftflow, FHardflow);
end;
FStopBits:=sb;
end;
function TLazSerial.WriteBuffer(var buf; size: integer): integer;
begin
// if FSynSer.Handle=INVALID_HANDLE_VALUE then
// ComException('can not write to a closed port.');
result:= FSynSer.SendBuffer(Pointer(@buf), size);
end;
function TLazSerial.WriteData(data: string): integer;
begin
result:=length(data);
FSynSer.SendString(data);
end;
function TLazSerial.ModemSignals: TModemSignals;
begin
result:=[];
if FSynSer.CTS then result := result + [ msCTS ];
if FSynSer.carrier then result := result + [ msCD ];
if FSynSer.ring then result := result + [ msRI ];
if FSynSer.DSR then result := result + [ msDSR ];
end;
function TLazSerial.GetDSR: boolean;
begin
result := FSynSer.DSR;
end;
function TLazSerial.GetCTS: boolean;
begin
result := FSynSer.CTS;
end;
function TLazSerial.GetRing: boolean;
begin
result := FSynSer.ring;
end;
function TLazSerial.GetCarrier: boolean;
begin
result := FSynSer.carrier;
end;
{procedure TLazSerial.SetBreak(OnOff: boolean);
begin
// if FHandle=-1 then
// ComException('can not set break state on a closed port.');
// if OnOff=false then ioctl(FHandle,TIOCCBRK,1)
// else ioctl(FHandle,TIOCSBRK,0);
end; }
procedure TLazSerial.SetDTR(OnOff: boolean);
begin
FSynSer.DTR := OnOff;
end;
procedure TLazSerial.SetRTS(OnOff: boolean);
begin
FSynSer.RTS := OnOff;
end;
procedure TLazSerial.ComException(str: string);
begin
raise Exception.Create('ComPort error: '+str);
end;
{ TComPortReadThread }
procedure TComPortReadThread.CallEvent;
begin
if Assigned(Owner.FOnRxData) then begin
Owner.FOnRxData(Owner);
end;
end;
procedure TComPortReadThread.Execute;
begin
try
while not MustDie do begin
if Owner.FSynSer.CanReadEx(100) then
Synchronize(@CallEvent);
end;
finally
Terminate;
end;
end;
procedure Register;
begin
RegisterComponents('LazSerial', [TLazSerial]);
end;
initialization
{$i TLazSerial.lrs}
end.