-
Notifications
You must be signed in to change notification settings - Fork 1
/
ubcgeneric1.pas
executable file
·222 lines (188 loc) · 5.14 KB
/
ubcgeneric1.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
unit UBCGeneric1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Math, Synaser, StrUtils, UnitDebug, Common;
type
TUBCGeneric1 = class(TInterfacedObject, IScanner)
function NumberOfBanks: Integer; virtual;
function NumberOfSlots: Integer; virtual;
function ModelAliased: String; virtual;
function Model: String; virtual;
function PortId: String; virtual;
function SupportsDescription:Boolean; virtual;
function UpdateSlot(memory_row: TScannerMemoryRow): Boolean; virtual;
function GetMemorySlots: TScannerMemory;
function Alive: SmallInt;
function ReadOnly: Boolean;
function Loaded: Boolean;
procedure Unload;
public
constructor Create;
protected
function SendAndRecieve(cmd: String): TStringList;
public
ComPort: String;
Online: Boolean;
protected
CommInProgress: Boolean;
MemIsLoaded: Boolean;
end;
implementation
function TUBCGeneric1.NumberOfBanks: Integer; begin
raise Exception.Create('Need to be overloaded');
end;
function TUBCGeneric1.NumberOfSlots: Integer; begin
raise Exception.Create('Need to be overloaded');
end;
function TUBCGeneric1.ModelAliased: String; begin
raise Exception.Create('Need to be overloaded');
end;
function TUBCGeneric1.Model: String; begin
raise Exception.Create('Need to be overloaded');
end;
function TUBCGeneric1.PortId: String; begin
raise Exception.Create('Need to be overloaded');
end;
function TUBCGeneric1.SupportsDescription:Boolean; begin
raise Exception.Create('Need to be overloaded');
end;
function TUBCGeneric1.UpdateSlot(memory_row: TScannerMemoryRow): Boolean; begin
raise Exception.Create('Need to be overloaded');
end;
constructor TUBCGeneric1.Create;
begin
Online:=false;
MemIsLoaded:=false;
CommInProgress:=false;
end;
procedure TUBCGeneric1.Unload;
begin
MemIsLoaded:=false;
end;
function TUBCGeneric1.Loaded: Boolean;
begin
Result:=MemIsLoaded;
end;
function TUBCGeneric1.ReadOnly: Boolean;
begin
Result:=false;
end;
function TUBCGeneric1.Alive: SmallInt;
var
ports: TStringList;
n: Integer;
port: String;
port_detail : String;
buf: TStringList;
m: String;
r: Boolean;
begin
if CommInProgress = true then begin
Result:=-1;
Exit;
end;
r:=false;
ports:=GetSerialPortNames;
for n := 0 to ports.Count - 1 do begin
port:=ports[n];
port_detail:=GetSerialPortFriendlyName(port);
FormDebug.Debug(port_detail);
if (NPos(PortId, port_detail, 1) > 0) then begin
ComPort:=port;
buf:=SendAndRecieve('MDL');
if buf.Count = 2 then begin
m:=buf[1];
if m = Model then begin
r:=true;
Break;
end;
end;
end;
end;
if Online <> r then begin
Online:=r;
FormDebug.Debug(Model + ' ' + IfThen(Online, 'is online on port: ' + port, 'gone offline'));
end;
Result:=IfThen(r, 1, 0);
end;
function TUBCGeneric1.GetMemorySlots: TScannerMemory;
var
i: Integer;
buf: TStringList;
buf_frq_up, buf_frq_down: String;
zeros : TSysCharSet;
row: TScannerMemoryRow;
rows: TScannerMemory;
begin
zeros:=['0'];
SetLength(rows, NumberOfSlots);
SendAndRecieve('PRG');
for i:=1 to NumberOfSlots do begin
buf:=SendAndRecieve('CIN,' + IntToStr(i));
row:=TScannerMemoryRow.Create;
row.Valid:=false;
if buf.Count >= 9 then begin
buf_frq_up:=TrimLeftSet(Copy(buf[3], 1, 4), zeros);
buf_frq_down:=Copy(buf[3], 5, Length(buf[3]));
// Jesli skaner wspiera wlasne opisy, olewam te z lookupa naspisujac je tym, co jest w skanerze
row.LookupForFrqDescriptions:=(not SupportsDescription);
row.FrqDescription:=Trim(buf[2]);
row.Model:=Model;
row.Valid:= true;
row.SetData(
Ceil(StrToInt(buf[1])/(NumberOfSlots/NumberOfBanks)),
StrToInt(buf[1]),
StrToFloat(buf_frq_up + ',' + buf_frq_down),
buf[4],
StrToInt(buf[6]),
StrToInt(buf[7])
);
end;
rows[i]:=row;
end;
SendAndRecieve('EPG');
MemIsLoaded:=True;
Result:=rows;
end;
function TUBCGeneric1.SendAndRecieve(cmd: String): TStringList;
var
ser: TBlockSerial;
lne: String;
buf: TStringList;
err: Boolean;
begin
CommInProgress:=true;
err:=true;
buf:=TStringlist.Create;
buf.Delimiter:=',';
buf.StrictDelimiter:=true;
ser:=TBlockSerial.Create;
FormDebug.Debug('-> ' + cmd);
try
ser.Connect(ComPort);
ser.config(57600, 8, 'N', SB1, False, False);
if ser.LastError = 0 then begin
ser.SendString(cmd + AnsiString(#13));
if ser.LastError = 0 then begin
ser.ConvertLineEnd:= true;
lne:=ser.Recvstring(5000);
if ser.LastError = 0 then begin
FormDebug.Debug('<- ' + lne);
if (lne <> 'ERR') and (Copy(lne,1,Length(cmd)) = cmd) then begin
buf.DelimitedText:= lne;
err:=false;
end;
end;
end;
end;
if err = true then begin
FormDebug.Debug('Error: Device: ' + ser.Device + ' Status: ' + ser.LastErrorDesc + ' ' + Inttostr(ser.LastError));
end;
finally
ser.free;
end;
CommInProgress:=false;
Result:=buf;
end;
end.