-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdmSerial.Linux.pas
307 lines (268 loc) · 7.08 KB
/
dmSerial.Linux.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
unit dmSerial.Linux;
interface
uses
dmSerial.Base,
diagnostics,
FMX.Dialogs,
FMX.Memo,
FMX.Colors,
system.classes,
Winsoft.FireMonkey.FComPort,
Winsoft.FireMonkey.FComSignal,
system.UITypes,
neato.helpers,
system.SysUtils;
type
TdmSerialLinux = class(TdmSerialBase)
private
fError: String;
fErrorCode: integer;
fComFailure: boolean;
procedure FComPort1Error(ComPort: TFComPort; E: EComError; var Action: TComAction);
protected
procedure onAfterClose(ComPort: TFComPort);
procedure onAfterOpen(ComPort: TFComPort);
public
FComSignalCNX: TColorBox;
onError: TNotifyEvent;
fmemoDebug: tmemo;
Serial: TFComPort;
FComSignalRX: TFComSignal;
FComSignalTX: TFComSignal;
ComPort: String;
constructor Create;
destructor Destroy; override;
Function Open: boolean; override;
procedure Close; override;
procedure SetOnRxChar(value: TNotifyEvent);
function SendCommand(cmd: string; const readtimeout: integer = 500; const waitfor: integer = 100): string; override;
function SendCommandOnly(cmd: string): String; override; // Just send command and move on
function SendCommandAndWaitForValue(cmd: string; const readtimeout: integer = 500; const waitfor: string = '';
const count: byte = 1): string; override;
function ReadString: String; override;
function Active: boolean; override;
property Error: String read fError;
property ErrorCode: integer read fErrorCode;
property Failure: boolean read fComFailure;
end;
implementation
constructor TdmSerialWindows.Create;
begin
inherited;
Serial := TFComPort.Create(nil);
Serial.BeforeClose := onAfterClose;
Serial.BeforeOpen := onAfterOpen;
FComSignalCNX := nil;
FComSignalRX := TFComSignal.Create(Serial);
FComSignalRX.ColorOff := talphacolorrec.Maroon;
FComSignalRX.ColorOn := talphacolorrec.Red;
FComSignalRX.Signal := tsignal.siRxChar;
FComSignalTX := TFComSignal.Create(Serial);
FComSignalTX.ColorOff := talphacolorrec.Darkgreen;
FComSignalTX.ColorOn := talphacolorrec.Green;
FComSignalTX.Signal := tsignal.siTxChar;
FComSignalRX.ComPort := Serial;
FComSignalTX.ComPort := Serial;
end;
Destructor TdmSerialWindows.destroy;
begin
FComSignalRX.Free;
FComSignalTX.Free;
Serial.Free;
inherited;
end;
procedure TdmSerialWindows.onAfterClose(ComPort: TFComPort);
begin
if assigned(FComSignalCNX) then
FComSignalCNX.Color := talphacolorrec.Olive;
end;
procedure TdmSerialWindows.onAfterOpen(ComPort: TFComPort);
begin
if assigned(FComSignalCNX) then
FComSignalCNX.Color := talphacolorrec.Yellow;
end;
procedure TdmSerialWindows.SetOnRxChar(value: TNotifyEvent);
begin
Serial.OnRxChar := value;
end;
procedure TdmSerialWindows.FComPort1Error(ComPort: TFComPort; E: EComError; var Action: TComAction);
begin
if E.ErrorCode = 22 then // when loose connection
begin
fError := E.Message;
fErrorCode := E.ErrorCode;
Action := caabort;
Serial.Close;
if assigned(onError) then
onError(self);
end;
end;
Function TdmSerialWindows.Open: boolean;
begin
try
fError := '';
fErrorCode := 0;
fComFailure := false;
try
Serial.Close;
except
end;
Serial.DeviceName := '\\.\' + ComPort;
Serial.active := true;
result := true;
except
on E: Exception do
begin
result := false;
fError := E.Message;
if assigned(onError) then
onError(self);
end;
end;
end;
procedure TdmSerialWindows.Close;
begin
try
Serial.Close;
except
on E: Exception do
begin
fError := E.Message;
if assigned(onError) then
begin
tthread.Synchronize(tthread.currentthread, // Queue Syncronize
procedure
begin
onError(self);
end);
end;
end;
end;
end;
function TdmSerialWindows.SendCommandOnly(cmd: string): String;
begin
try
result := cmd;
try
result := '';
Serial.Timeouts.ReadInterval := 16000;
if assigned(fmemoDebug) then
begin
fmemoDebug.BeginUpdate;
fmemoDebug.Lines.Add(cmd);
fmemoDebug.Lines.Add('');
fmemoDebug.Lines.Add('< Call does not check response >');
fmemoDebug.GoToTextEnd;
fmemoDebug.EndUpdate;
end;
Serial.WriteAnsiString(AnsiString(cmd) + #13);
Serial.WaitForWriteCompletion;
Serial.WaitForReadCompletion;
except
on E: Exception do
begin
tthread.Synchronize(tthread.currentthread, // Queue Syncronize
procedure
begin
fError := E.Message;
fErrorCode := E.HelpContext;
onError(self);
end);
end;
end;
finally
//
end;
end;
function TdmSerialWindows.SendCommand(cmd: string; const readtimeout: integer = 500;
const waitfor: integer = 100): string;
var
sw: tstopwatch;
begin
try
try
sw := tstopwatch.Create;
result := '';
Serial.Timeouts.ReadInterval := readtimeout;
if assigned(fmemoDebug) then
fmemoDebug.Lines.Add(cmd);
Serial.WriteAnsiString(AnsiString(cmd) + #13);
Serial.WaitForWriteCompletion;
Serial.WaitForReadCompletion;
repeat
sleep(waitfor);
until (not Serial.ReadPending) or (not Serial.active);
result := string(Serial.ReadAnsiString);
if assigned(fmemoDebug) then
begin
fmemoDebug.BeginUpdate;
fmemoDebug.Lines.Add(result);
fmemoDebug.GoToTextEnd;
fmemoDebug.EndUpdate;
end;
except
on E: Exception do
begin
tthread.Synchronize(tthread.currentthread, // Queue Syncronize
procedure
begin
fError := E.Message;
fErrorCode := E.HelpContext;
onError(self);
end);
end;
end;
finally
//
end;
end;
function TdmSerialWindows.ReadString: String;
begin
result := String(Serial.ReadAnsiString);
end;
function TdmSerialWindows.SendCommandAndWaitForValue(cmd: string; const readtimeout: integer = 500;
const waitfor: string = ''; const count: byte = 1): string;
var
timeout: boolean;
sw: tstopwatch;
begin
try
result := '';
if assigned(fmemoDebug) then
fmemoDebug.Lines.Add(cmd);
Serial.WriteAnsiString(AnsiString(cmd) + #13);
Serial.Timeouts.ReadInterval := readtimeout;
timeout := false;
sw := tstopwatch.Create;
sw.Start;
repeat
result := result + String(Serial.ReadAnsiString);
if sw.ElapsedMilliseconds > readtimeout then
timeout := true;
until (not Serial.active) or (OccurrencesOfChar(result, ^z) = count) or (timeout);
result := trim(result);
if assigned(fmemoDebug) then
begin
fmemoDebug.BeginUpdate;
fmemoDebug.Lines.Add(result);
fmemoDebug.GoToTextEnd;
fmemoDebug.EndUpdate;
end;
except
on E: Exception do
begin
tthread.Synchronize(tthread.currentthread, // Queue Syncronize
procedure
begin
fError := E.Message;
fErrorCode := E.HelpContext;
onError(self);
end);
end;
end;
end;
function TdmSerialWindows.active: boolean;
begin
result := Serial.active;
end;
end.