-
Notifications
You must be signed in to change notification settings - Fork 1
/
ASTM1381.pas
477 lines (438 loc) · 10.9 KB
/
ASTM1381.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
unit ASTM1381;
interface
uses
HComPort, SysUtils, Classes, Forms, StrUtils, SyncObjs, TypInfo, AnsiStrings;
const
ACK = 6;
ENQ = 5;
ETB = 23;
ETX = 3;
LF = 10;
NAK = 21; //15H
STX = 2;
EOT = 4;
CR = 13;
C_ACK = AnsiChar(6);
C_ENQ = AnsiChar(5);
C_ETB = AnsiChar(23);
C_ETX = AnsiChar(3);
C_LF = AnsiChar(10);
C_NAK = AnsiChar(21);
C_STX = AnsiChar(2);
C_EOT = AnsiChar(4);
C_CR = AnsiChar(13);
type
TTransferState = (tsNeutral, tsEstabilished, tsTermination, tsTransfer, tsWaitForPacket);
TReturnState = (rsOther, rsACK, rsNAK);
TNotifyStr = procedure(s: AnsiString) of object;
TNotifyProc = procedure;
//TASTM1381Transfer = class;
{TSendThread = class(TThread)
private
FOwner: TASTM1381Transfer;
protected
procedure Execute; override;
public
constructor Create(AOwner: TASTM1381Transfer);
end;}
TASTM1381Transfer = class(TObject)
private
//FPushPop: TPushPop;
//FSendThread: TSendThread;
//FOnByteReceived: TNotifyStr;
FHSerialPort: THSerialPort;
FPrevState: TTransferState;
FTransferState: TTransferState;
FLastMessage: TStringList;
FLastPacket: AnsiString;
FOnMessageRecieved: TNotifyProc;
FOnPortError: TNotifyProc;
FLastError: AnsiString;
procedure OnRxChar(Sender: TObject; ResvSize: Integer);
procedure OnError(Sender: TObject; Msg: AnsiString; var Error: integer);
procedure OpenConnect;
function WaitForACK: TReturnState;
procedure ReadPacket;
function Checksum(const s: AnsiString): Boolean;
function GetCrc(const s: AnsiString): Word;
procedure ACKResponse;
procedure NAKResponse;
procedure PacketReceived;
procedure SetTransferState(const Value: TTransferState);
public
constructor Create;
destructor Destroy; override;
procedure CloseConnect;
function CalcCRC(const s: AnsiString): Word;
procedure SendFrame(const Frame: AnsiString);
property LastMessage: TStringList read FLastMessage;
property LastError: AnsiString read FLastError;
property OnMessageRecieved: TNotifyProc read FOnMessageRecieved write FOnMessageRecieved;
property OnPortError: TNotifyProc read FOnPortError write FOnPortError;
//property OnByteReceived: TNotifyStr read FOnByteReceived write FOnByteReceived;
property HSerialPort: THSerialPort read FHSerialPort;
property TransferState: TTransferState read FTransferState write SetTransferState;
end;
implementation
uses Math;
var
WriteLogStrCriticalSection: TCriticalSection;
procedure CreateLogFile;
var
f: textfile;
begin
if FileExists('log.txt') then
Exit;
AssignFile(f, 'log.txt');
rewrite(f);
closefile(f);
end;
procedure WriteLog(const s: AnsiString); overload;
var
f: textfile;
begin
WriteLogStrCriticalSection.Enter;
try
AssignFile(f, 'log.txt');
{$I-}
Append(f);
WriteLn(f, AnsiString(DateTimeToStr(Now) + ' ') + s);
CloseFile(f);
{$I+}
finally
WriteLogStrCriticalSection.Leave;
end;
end;
function CopyLim(const s: AnsiString; StrBeg, StrEnd: integer): AnsiString;
begin
Result := Copy(s, StrBeg, StrEnd - StrBeg + 1);
end;
function ReplReturns(const s: AnsiString): AnsiString;
begin
Result := AnsiReplaceStr(AnsiReplaceStr(s, AnsiChar($0D), AnsiString('#$0D')), AnsiChar($0A), AnsiString('#$0A'));
end;
{ TASTM1381Transfer }
procedure TASTM1381Transfer.ACKResponse;
begin
WriteLog('ACKResponse');
FHSerialPort.SendByte(ACK);
end;
function TASTM1381Transfer.CalcCRC(const s: AnsiString): Word;
var
i: Integer;
s1: AnsiString;
b: Byte;
begin
WriteLog('CalcCRC');
b := 0;
for i := 1 to Length(s) do
b := b + Ord(s[i]) mod 256;
s1 := AnsiString(IntToHex(b, 2));
Result := Ord(s1[2]) * 256 + Ord(s1[1]);
end;
//<STX> [F1] [DATA] <ETX> [C1] [C2] <CR> <LF>
function TASTM1381Transfer.Checksum(const s: AnsiString): Boolean;
var
Temp: Integer;
begin
WriteLog('Checksum');
Result := False;
Temp := Max(Pos(C_ETX, s), Pos(C_ETB, s));
if Temp = 0 then
Exit;
Result := CalcCRC(Copy(s, 1, Temp)) = GetCrc(s);
end;
procedure TASTM1381Transfer.CloseConnect;
begin
WriteLog('CloseConnect');
FHSerialPort.SendByte(EOT);
TransferState := tsNeutral;
end;
constructor TASTM1381Transfer.Create;
begin
inherited;
WriteLog('Create');
//FPushPop := TPushPop.Create;
//FSendThread := TSendThread.Create(Self);
TransferState := tsNeutral;
FLastMessage := TStringList.Create;
FHSerialPort := THSerialPort.Create;
FHSerialPort.OnRxChar := OnRxChar;
FHSerialPort.OnError := OnError;
//FHSerialPort.Open;
end;
destructor TASTM1381Transfer.Destroy;
begin
WriteLog('Destroy');
//FSendThread.Free;
//FPushPop.Free;
FHSerialPort.Free;
FLastMessage.Free;
inherited;
end;
function TASTM1381Transfer.GetCrc(const s: AnsiString): Word;
var
s1: AnsiString;
BegStr, EndStr: Integer;
begin
WriteLog(AnsiString('GetCrc ') + ReplReturns(s));
BegStr := Max(Pos(C_ETX, s), Pos(C_ETB, s));
EndStr := PosEx(C_CR, string(s), BegStr);
s1 := CopyLim(s, BegStr + 1, EndStr - 1);
Result := Ord(s1[2]) * 256 + Ord(s1[1]);
end;
procedure TASTM1381Transfer.NAKResponse;
begin
WriteLog('NAKResponse');
FHSerialPort.SendByte(NAK);
end;
procedure TASTM1381Transfer.OpenConnect;
var
Counter: Integer;
ReturnState: TReturnState;
begin
Counter := 0;
repeat
FHSerialPort.SendByte(ENQ);
ReturnState := WaitForACK;
Inc(Counter);
if ReturnState = rsNAK then
Sleep(10000);
until (TransferState = tsEstabilished) or (Counter = 7) or (ReturnState = rsOther);
end;
procedure TASTM1381Transfer.ReadPacket;
begin
WriteLog('ReadPacket');
FPrevState := TransferState;
TransferState := tsWaitForPacket;
end;
procedure TASTM1381Transfer.OnRxChar(Sender: TObject; ResvSize: Integer);
var
Temp: Byte;
s: AnsiString;
begin
FHSerialPort.ReadString(s, ResvSize);
//if Assigned(OnByteReceived) then
// OnByteReceived(s);
WriteLog(AnsiString('State on Rx: ' + GetEnumName(TypeInfo(TTransferState), Integer(TransferState))));
if Length(s) = 0 then
begin
WriteLog('Rx null');
Exit;
end;
repeat
Temp := Ord(s[1]);
case Temp of
ENQ:
begin
WriteLog('Rx ENQ');
case TransferState of
tsNeutral, tsTransfer:
begin
FLastMessage.Clear;
FLastPacket := '';
TransferState := tsEstabilished;
ACKResponse;
Delete(s, 1, 1);
end;
else
raise Exception.Create('Wrong state in ENQ!');
{tsEstabilished:
begin
FLastMessage.Clear;
FLastPacket := '';
TransferState := tsNeutral;
NAKResponse;
end;}
end;
end;
STX:
begin
WriteLog('Rx STX');
case TransferState of
tsNeutral, tsEstabilished, tsTransfer:
begin
FLastPacket := Copy(s, 2, MaxInt);
s := '';
ReadPacket;
end;
else
raise Exception.Create('Wrong state in STX!');
end;
end;
EOT:
begin
WriteLog('Rx EOT');
case TransferState of
tsTransfer, tsWaitForPacket:
begin
FLastPacket := '';
Delete(s, 1, 1);
TransferState := tsNeutral;
if Assigned(FOnMessageRecieved) and (LastMessage.Count > 0) then
FOnMessageRecieved;
end;
else
raise Exception.Create('Wrong state in EOT!');
end;
end;
else
begin
WriteLog('Rx ' + ReplReturns(s));
FLastPacket := FLastPacket + s;
s := '';
end;
end;
if (TransferState = tsWaitForPacket) and (Pos(C_CR + C_LF, FLastPacket) <> 0) then
PacketReceived;
until s = '';
end;
procedure TASTM1381Transfer.PacketReceived;
var
Temp: Integer;
begin
WriteLog('PacketReceived');
case FPrevState of
tsNeutral: NAKResponse;
tsEstabilished, tsTransfer:
begin
if Checksum(FLastPacket) then
begin
Temp := Max(Pos(C_ETX, FLastPacket), Pos(C_ETB, FLastPacket));
FLastMessage.Add(string(Copy(FLastPacket, 1, Temp)));
ACKResponse;
end
else
NAKResponse;
TransferState := tsTransfer;
end;
else
raise Exception.Create('Wrong state in PacketReceived!');
end;
end;
procedure TASTM1381Transfer.OnError(Sender: TObject; Msg: AnsiString; var Error: integer);
begin
WriteLog('OnError');
FLastError := Msg + AnsiString('. Error: ' + SysErrorMessage(Error));
if Assigned(FOnPortError) then
FOnPortError;
Error := 0;
end;
procedure TASTM1381Transfer.SendFrame(const Frame: AnsiString);
var
Counter: Integer;
ReturnState: TReturnState;
begin
{FPushPop.Push(Frame);
if FSendThread.Suspended then
FSendThread.Resume}
WriteLog('SendFrame');
case TransferState of
tsEstabilished: TransferState := tsTransfer;
tsNeutral: OpenConnect;
else
raise Exception.Create('Wrong state in SendFrame!');
end;
if TransferState = tsNeutral then
Exit;
Counter := 0;
with FHSerialPort do
repeat
SendByte(STX);
SendString(Frame);
SendByte(CR);
SendByte(ETX);
SendWord(CalcCRC(Frame + C_CR + C_ETX));
SendByte(CR);
SendByte(LF);
ReturnState := WaitForACK;
Inc(Counter);
if ReturnState <> rsACK {= rsNAK} then
Sleep(10000);
until (Counter = 7) or (ReturnState = rsACK);
if (Counter = 7) and (ReturnState <> rsACK) then
CloseConnect;
//FOwner.CloseConnect;
end;
function TASTM1381Transfer.WaitForACK: TReturnState;
var
s: AnsiString;
begin
WriteLog('WaitForACK');
Result := rsOther;
FHSerialPort.ReadString(s, 1, 10000);
//if Assigned(OnByteReceived) then
// OnByteReceived(s);
if s = C_ACK then
begin
Result := rsACK;
case TransferState of
tsNeutral: TransferState := tsEstabilished;
tsEstabilished: ;
else
raise Exception.Create('Wrong state in WaitForACK!');
end;
end
else if s = C_NAK then
Result := rsNAK;
end;
procedure TASTM1381Transfer.SetTransferState(const Value: TTransferState);
begin
WriteLog('Set state from: ' + GetEnumName(TypeInfo(TTransferState), Integer(FTransferState)) + ' to: ' +
GetEnumName(TypeInfo(TTransferState), Integer(Value)));
FTransferState := Value;
end;
{ TSendThread }
(*constructor TSendThread.Create(AOwner: TASTM1381Transfer);
begin
inherited Create(True);
FOwner := AOwner;
end;
procedure TSendThread.Execute;
var
Counter: Integer;
ReturnState: TReturnState;
s: AnsiString;
begin
repeat
if FOwner.FPushPop.Count = 0 then
begin
Sleep(1000);
Continue;
end;
s := FOwner.FPushPop.Pop;
WriteLog('SendFrame');
case FOwner.TransferState of
tsEstabilished: FOwner.TransferState := tsTransfer;
tsNeutral: FOwner.OpenConnect;
else
raise Exception.Create('Wrong state in SendFrame!');
end;
if FOwner.TransferState = tsNeutral then
Exit;
Counter := 0;
with FOwner.FHSerialPort do
repeat
SendByte(STX);
SendString(s);
SendByte(CR);
SendByte(ETX);
SendWord(FOwner.CalcCRC(s + C_CR + C_ETX));
SendByte(CR);
SendByte(LF);
ReturnState := FOwner.WaitForACK;
Inc(Counter);
if ReturnState <> rsACK{= rsNAK} then
Sleep(10000);
until (Counter = 7) or (ReturnState = rsACK);
//if (Counter = 7) and (ReturnState <> rsACK) then
// FOwner.CloseConnect;
FOwner.CloseConnect;
until Terminated;
end;*)
initialization
WriteLogStrCriticalSection := TCriticalSection.Create;
CreateLogFile;
finalization
WriteLogStrCriticalSection.Free;
end.