-
Notifications
You must be signed in to change notification settings - Fork 1
/
ggscout.py
1288 lines (988 loc) · 47.6 KB
/
ggscout.py
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
"""
ggscout.py - GeigerLog commands to handle the Gamma-Scout Geiger counter
"""
###############################################################################
# This file is part of GeigerLog.
#
# GeigerLog is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# GeigerLog 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GeigerLog. If not, see <http://www.gnu.org/licenses/>.
###############################################################################
__author__ = "ullix"
__copyright__ = "Copyright 2016, 2017, 2018, 2019, 2020"
__credits__ = [""]
__license__ = "GPL3"
"""
Gamma-Scout_Communication_Interface_V1.7.txt
from: https://www.gamma-scout.com/wp-content/uploads/Gamma-Scout_Communication_Interface_V1.7.txt
The contents of the protocol memory consists of either 2 byte pulse entries or
special codes. It must be interpreted byte by byte. Special codes start with a
byte with its high-nibble being 0xF. If the high-nibble is not 0xF, this byte
will be the first byte of a 2 byte pulse entry.
This 2 byte pulse entry represents the number of pulses collected during the
last protocol interval (or the number of pulse during an out-of-band protocol
interval, see below). The highest 5 bits contain the exponent, the lower 11
bits the mantissa. E.g.:
0x3E27 = %0011111000100111 = 2^7 (exponent) * 1575 (mantissa) = 201600
Gamma-Scout tube: LND712
https://www.lndinc.com/products/geiger-mueller-tubes/712/
GAMMA SENSITIVITY CO60 (CPS/mR/HR) : 18
MAXIMUM BACKGROUND SHIELDED 50MM PB + 3MM AL (CPM) : 10
Gamma Sensitivity : 18 CPS/mR/h = 1080 CPM/mR/h = 1080 CPM/10µSv/h = 108 CPM/µSv/h
invers: : 1/108 = 0.00926 µSv/h/CPM
Compare with M4011: (0.00926 / 0.0065 = 1.42 (1/1.42=0.70)
: LND712 has only 70% of sensitivity of M4011 for gamma
"""
from gutils import *
import gsql # database handling
#
# Private Constants
#
# intervals in sec as used in GammaScout
_protocol_interval = {
0x00 : 7 * 24 * 60 * 60, # User selected a protocol interval of 1 week
0x01 : 3 * 24 * 60 * 60, # User selected a protocol interval of 3 days
0x02 : 1 * 24 * 60 * 60, # User selected a protocol interval of 1 day
0x03 : 12 * 60 * 60, # User selected a protocol interval of 12 hours
0x04 : 2 * 60 * 60, # User selected a protocol interval of 2 hours
0x05 : 1 * 60 * 60, # User selected a protocol interval of 1 hour
0x06 : 30 * 60, # User selected a protocol interval of 30 minutes
0x07 : 10 * 60, # User selected a protocol interval of 10 minutes
0x08 : 5 * 60, # User selected a protocol interval of 5 minutes
0x09 : 2 * 60, # User selected a protocol interval of 2 minutes
0x0A : 1 * 60, # User selected a protocol interval of 1 minute
0x0B : 30, # User selected a protocol interval of 30 seconds
0x0C : 10, # User selected a protocol interval of 10 seconds
}
def printTestValues():
# test values for floating formatting
testraws = [0x3e27, # = 201600 # example from Gamma Scout company
0x00aa, # = 170
0x01bb, # = 443
0x02cc, # = 716
0x03dd, # = 989
0x04ee, # = 1262
0x05ff, # = 1535
0x1234, # = 3176
0b0000011111111110, # = 2046
0b0000011111111111, # = 2047
0b0000111111111111, # = 4094
0b0001111111111111, # = 16376
0xabcd, # = 2094006272
0x7fff, # max des Gerätes 0xF signalisiert Funktion
0xffff, # max test
0x0DAE, # aus *.dat
]
for raw in testraws:
print("raw: 0x {:04X}, 0b {:016b}, value: {:17,d}".format(raw, raw, _getValue(raw)))
print("\n")
#
# Private Functions
#
def _getValue(raw):
"""Gamma-Scout_Communication_Interface; raw is a 2 byte value. The highest
5 bits contain the exponent, the lower 11 bits the mantissa. E.g.:
0x3E27 = %0011 1 110 0010 0111 = 2^7 (exponent) * 1575 (mantissa) = 201600
"""
exponent = raw >> 11
mantissa = raw & 0b0000011111111111
counts = mantissa * (2 ** exponent)
#print("raw: {:6d} 0x{:04X} 0b{:016b} {} {}".format(raw, raw, raw, "{:016b}".format(raw)[0:5], "{:016b}".format(raw)[5:]), end= " ")
#print("exponent: {:3d}, mantissa: {:5d}".format(exponent, mantissa ), end =" ")
#print("counts: {:17,d}".format(counts))
return counts
def _getDateByte(raw):
"""hex value to be interpreted as alphanumeric: 0x37 = decimal 37!"""
rdate = 10 * (raw >> 4) + (raw & 0x0f)
return "{:02d}".format(rdate)
def _num2datstr(timestamp):
dt = datetime.datetime.fromtimestamp(timestamp)
return str(dt)
def _parseCommentAdder(i, rectime, dbtype):
datalist = [None] * 4 # 4 x None
datalist[0] = i # byte index
datalist[1] = rectime # Date&Time
datalist[2] = "0 hours" # the modifier for julianday; here: no modification
datalist[3] = dbtype # Date&Time Stamp Info
#print("#{:5d}, {:19s}, {:}".format(i, rectime, dbtype))
gglobs.HistoryCommentList.append(datalist)
def _parseValueAdder(i, rectime, counts, parsecomment, cpm_calc, interval):
"""Add the parse results to the *.his list and commented *.his.parse list"""
# create the data for the database; datalist covers:
# Index, DateTime, <modifier>, CPM, CPS, CPM1st, CPS1st, CPM2nd, CPS2nd, CPM3rd, CPS3rd, Temp, Press, Humid, X
# 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
datalist = [None] * (gglobs.datacolsDefault + 2) # (13 + 2)=15 x None
datalist[0] = i # byte index
datalist[1] = rectime # Date&Time
datalist[2] = "0 hours" # the modifier for julianday; here: no modification
datalist[5] = counts # the counts in whatever the interval had been
datalist[7] = round(cpm_calc, 1) # the cpm CALCULATED from counts and interval
datalist[14] = interval # the interval of counting
#print("_parseValueAdder: datalist: ", datalist)
gglobs.HistoryDataList.append (datalist)
gglobs.HistoryParseList.append([i, parsecomment])
def _getParsedHistory(hisbytes, maxbytes=0xFFFF):
"""Parse the history as bytes dump and create CSV *.his file"""
vprint("_getParsedHistory:")
setDebugIndent(1)
try:
index = hisbytes.index(0xF5) # =245 =start location of history
except Exception as e:
dprint("Error: Exception: {}: Start of history with byte value 0xF5 (=245) not found in binary data!".format(e))
return
parsecounter = 0
parsecountmax= 10 # max number of count records to be printed
interval = 0
countertime = 0
timestamp = 0
datestr = ""
while True:
raw = hisbytes[index] # raw is SINGLE byte!
# Special code - Dose overflow
if raw == 0xFA:
dbtype = "{:20s}: 0x{:02X} ({:3d}): Dose rate overflowed (> 1000 uSv/h) during the current protocol interval at least once.".format("Special Code", raw, raw)
wprint(dbtype)
rectime = _num2datstr(countertime)
_parseCommentAdder(index, rectime, dbtype)
index += 1
# Out-of-band protocol interval
# relevant only for firmware up to 6.016 (5.43 < fw <= 6.016, and for fw <= 5.43)
# https://www.gamma-scout.com/wp-content/uploads/Gamma-Scout_Communication_Interface_V1.7.txt
# elif raw == 0xFF:
# print("{:20s} @{:<5d}: 0x{:02X} ({:3d}): ".format(" Out-of-band protocol interval, see above. ", index, raw, raw))
# nextbyte1 = hisbytes[index + 1]
# nextbyte2 = hisbytes[index + 2]
# writeFileA(filepath_his, "#{:5d}, {:19s}, 0x{:02X} : OLD VERSION Out-of-band protocol interval, next two bytes: 0x{:02X}{:02X}".format(index, _num2datstr(countertime), raw, nextbyte1, nextbyte2) )
# index += 1 + 2
# Special code - Flag for more
elif raw == 0xF5:
dbtype = "{:20s}: 0x{:02X} ({:3d}): Generic special code. The following byte determines the meaning:".format("Special Code", raw, raw)
wprint(dbtype)
rectime = _num2datstr(countertime)
_parseCommentAdder(index, rectime, dbtype)
index += 1
raw = hisbytes[index] # raw is SINGLE byte!
# interval
if raw >= 0x00 and raw <= 0x0c:
interval = _protocol_interval[raw]
dbtype = "{:20s}: 0x{:02X} ({:3d}): ".format(" Protocol Interval", raw, raw) + "Protocol Interval: {:<5d} sec".format(interval)
wprint(dbtype)
rectime = _num2datstr(countertime)
_parseCommentAdder(index, rectime, dbtype)
index += 1
# ignore debug
elif raw >= 0xF0 and raw <= 0xFE:
dbtype = "{:20s} must be ignored: 0x{:02X} ({:3d}): ".format("debug flags", raw, raw)
wprint(dbtype)
rectime = _num2datstr(countertime)
_parseCommentAdder(index, rectime, dbtype)
index += 1
# out of band
elif raw == 0xEE:
dbtype = "{:20s}: 0x{:02X} ({:3d}): ".format(" Out-of-band protocol interval.", raw, raw)
nextbyte1 = hisbytes[index + 1] # nextbyte1 and 2 give number of 10 sec intervalls to be added to time
nextbyte2 = hisbytes[index + 2]
wprint(dbtype)
rectime = _num2datstr(countertime)
_parseCommentAdder(index, rectime, dbtype)
addedtime = (nextbyte2 * 256 + nextbyte1) * 10 # multiples of 10 sec
raw1 = hisbytes[index + 3]
raw2 = hisbytes[index + 4]
count = _getValue(raw1 << 8 | raw2)
dbtype = "{:20s}: 0x{:02X}{:02X} count:{}".format(" next: pulse entry bytes", raw1, raw2, count)
wprint(dbtype)
rectime = _num2datstr(countertime)
_parseCommentAdder(index, rectime, dbtype)
parsecounter += 1
if addedtime > 0:
dbtype = "{:19s}, {:10d}, {:9,.1f}, {:8d} ".format(_num2datstr(countertime), count, count/addedtime * 60, addedtime)
wprint(dbtype)
rectime = _num2datstr(countertime)
_parseValueAdder (index, rectime, count, dbtype, count/addedtime * 60, addedtime)
countertime += addedtime
index += 1 + 4
# timestamp
elif raw == 0xEF:
wprint("{:20s}: 0x{:02X} ({:3d}): 5 bytes following: mmhhDDMMYY.".format(" Timestamp", raw, raw))
mm = _getDateByte(hisbytes[index + 1])
hh = _getDateByte(hisbytes[index + 2])
DD = _getDateByte(hisbytes[index + 3])
MM = _getDateByte(hisbytes[index + 4])
YY = _getDateByte(hisbytes[index + 5])
ss = "00" # added here, not defined in firmware
tbytes = "hexbytes:"
for i in range(1, 6): tbytes += " {:02x}".format( hisbytes[index + i])
tstamp = "20{}-{}-{} {}:{}:{}".format(YY, MM, DD, hh, mm, ss)
countertime = datestr2num(tstamp)
dbtype = "{:19s}, 0x{:02X} : Timestamp: {} ({})".format(_num2datstr(countertime), raw, tstamp, tbytes)
wprint(dbtype)
rectime = _num2datstr(countertime)
_parseCommentAdder(index, rectime, dbtype)
index += 1 + 5
# counts
else:
raw1 = hisbytes[index ]
raw2 = hisbytes[index + 1]
count = _getValue(raw1 << 8 | raw2)
parsecounter += 1
dbtype = "{:19s}, {:10d}, {:9,.1f}, {:8d}, # raw bytes: 0x{:02X}{:02X}".format(_num2datstr(countertime), count, count/interval * 60, interval, raw1, raw2)
if parsecounter < parsecountmax:
wprint(parsecounter, " : ", dbtype)
parsecomment = "# raw bytes: 0x{:02X}{:02X}".format(raw1, raw2)
rectime = _num2datstr(countertime)
_parseValueAdder (index, rectime, count, parsecomment, count/interval * 60, interval)
countertime += interval
index += 1 + 1
if index >= maxbytes:
dbtype = "# maxbytes reached or exceeded: index:{}, maxbytes:{} (0x{:04X})".format(index, maxbytes, maxbytes)
wprint(dbtype)
rectime = _num2datstr(countertime)
_parseCommentAdder(index, rectime, dbtype)
break
if index >= len(hisbytes) - 1:
break
setDebugIndent(0)
def _readDataFromFile(dat_path):
"""read an ASCII file as readlines and return as list of byte values"""
fncname = "_readDataFromFile: "
wprint(fncname)
setDebugIndent(1)
try:
with open(dat_path) as f:
filedata = f.readlines() # reads data as list of STRINGS
# last char in each line is LF (value: 0x10)
except Exception as e:
dprint(fncname + "Exception at f.readlines(): ", e)
return []
#print("_readDataFromFile: filedata: type:{}, lines:{}".format(type(filedata), len(filedata)))
# write *.dat data to database
gsql.DB_insertBin (gglobs.hisConn, "".join(filedata))
dumpdata = []
for a in filedata: # remove last char LF
#dumpdata.append(a.replace('\n', ''))
dumpdata.append(a[:-1])
wprint(fncname + "dumpdata: type:{}, lines:{}".format(type(dumpdata), len(dumpdata)))
wprint(fncname + "-"*20)
for a in dumpdata[:15]: wprint(a)
wprint(fncname + "-"*20)
setDebugIndent(0)
return dumpdata
def _clearPipeline(device):
"""Clearing pipeline"""
fncname = "_clearPipeline: "
wprint(fncname + "Clearing pipeline of device: '{}'".format(device))
setDebugIndent(1)
bytedata = b""
if gglobs.GSser.in_waiting > 0:
while gglobs.GSser.in_waiting > 0:
bytedata += gglobs.GSser.read(gglobs.GSser.in_waiting) # reads data as list of BYTES
time.sleep(0.1)
cleared_data = bytedata.decode("UTF-8")
wprint(fncname + "Cleared data: {} bytes:\n{}".format(len(cleared_data), cleared_data))
else:
wprint(fncname + "No data found waiting for clearing")
setDebugIndent(0)
def _convertDumpToBinList(dumpdata):
"""take ascii coded hex values from readlines str data and convert to list of bytes"""
# e,g,: dumpdata[12]: len=67: f5ef5923130819f507f5ee0300000af50c00020001000100050004000500030096
# ends with '96' as LF or CR+LF has been removed!
fncname = "_convertDumpToBinList: "
len_dumpdata = len(dumpdata)
vprint(fncname + "len(dumpdata): {}".format(len_dumpdata))
if len_dumpdata == 0:
vprint(fncname + "No data to convert")
return []
setDebugIndent(1)
index = 0
# search for text "GAMMA-SCOUT Protokoll"
for i in range(0, len_dumpdata):
if dumpdata[i].startswith("GAMMA-SCOUT Protokoll"):
index = i
break
# search for text "f5" beginning at "GAMMA-SCOUT Protokoll"
for i in range(index, len_dumpdata):
if dumpdata[i].startswith("f5"):
index = i
break
# convert double-chr to byte and add to list
listdumpdata = []
for i in range(index, len_dumpdata):
lendata = len(dumpdata[i])
# limited printout
#if i < index + 10 or i > (len_dumpdata - 4):
if 0 and (i < index + 10 or i > (len_dumpdata - 4)):
#wprint("i={:5d} dumpdata[i]: {} lendata:{}".format(i, dumpdata[i], lendata))
strwprint = "i={:5d} dumpdata[i]: {} lendata: {}".format(i, dumpdata[i], lendata)
try:
checksum = 0
for j in range(0, lendata - 2): checksum += int(dumpdata[i][j], 16)
strwprint += " Checksum: 0x{:02X}".format(checksum)
strwprint += " Delta to 0x{}: {:3d} decimal".format(dumpdata[i][-2:], checksum- int(dumpdata[i][-2:], 16))
except Exception as e:
edprint("Checksum calculation gave Exception: ", e)
exceptPrint(e, sys.exc_info(), "re checksum")
wprint(strwprint)
# get byte data from ASCII list
for j in range(0, lendata - 2, 2):
#print("j={}".format(j), end=" ")
try:
lbyte = int(dumpdata[i][j : j + 2], 16)
except Exception as e:
edprint("_convertDumpToBinList: Exception in 'lbyte = int(dumpdata[i][j : j + 2], 16)':", e, ", j=",j)
lbyte = 0xFA # overflow single byte value
listdumpdata.append(lbyte)
wprint("listdumpdata : len: {} (0x{:04X}) ".format(len(listdumpdata), len(listdumpdata)))
wprint("first 50 : ", listdumpdata[:50])
wprint("last 50 : ", listdumpdata[-50:])
setDebugIndent(0)
return listdumpdata
def _extractExtendedInfo(infoline):
"""to extract info from infoline
= e.g. 'Version 6.10 d93683 4217 18.08.19 17:43:57'
return: tuple: fw_version, serial_no, maxbytes, device_time
"""
fncname = "_extractExtendedInfo: "
#defaults = (infoline, "Failure", "Failure", 2**16 - 1, "Failure")
defaults = ("Failure", "Failure", 2**16 - 1, "Failure")
wprint(fncname + "infoline: '{}'".format(infoline))
setDebugIndent(1)
if len(infoline) == 0:
wprint(fncname + "No data in infoline: '{}'".format(infoline))
details = defaults
else:
version_result = infoline.split(" ") # e.g.: ['Version', '6.10', 'd93683', '4217', '18.08.19', '17:43:57']
if len(version_result) < 6:
wprint(fncname + "Not enough details in data in infoline: '{}'".format(infoline))
details = defaults
else:
fw_version = version_result[1] # 6.10
serial_no = version_result[2] # d93683
dtcombo = version_result[4] + " " + version_result[5] # 14.08.2019 18:37:00
try:
maxbytes = int(version_result[3], 16) # 16919 (0x4217)
except Exception as e:
edprint(fncname + "Exception maxbytes: ", e)
maxbytes = 2**16 - 1
# convert device's date+time format to GL format:
# 14.08.2019 18:37:00 --> 2019-08-14 18:37:00
try:
timestamp = time.mktime(datetime.datetime.strptime(dtcombo, "%d.%m.%y %H:%M:%S").timetuple())
device_time = datetime.datetime.fromtimestamp(timestamp).strftime('%Y-%m-%d %H:%M:%S')
except:
edprint(fncname + "Exception timestamp: ", e)
device_time = "Failure"
#details = version_result, fw_version, serial_no, maxbytes, device_time
details = fw_version, serial_no, maxbytes, device_time
wprint(fncname + "Details: ", details)
setDebugIndent(0)
return details
def _getModeStatus(device):
"""writing 'v' to the counter makes the counter send back: (Valid up to fw 6.1x)
- when in PC Mode: e.g.: 'Version 6.10 d93683 4213 18.08.19 17:42:05'
- when inNormal Mode: 'Standard'
Online models will return: current mode ("Online x")
returns: "PC" when in PC Mode
"Normal" when in Normal Mode (both for old and new 'online' models)
"Failure" else
"""
fncname ="_getModeStatus: "
vprint(fncname + " of device: '{}'".format(device))
setDebugIndent(1)
ok = _writeToDevice(b'v', device)
if not ok:
setDebugIndent(0)
mode = "Failure"
else:
# bytecount=10 gets either "Standard" or "Online" or "Version"
mode_response = _readCommandFromDevice(device, bytecount=10)
vprint(fncname + "Response: ", mode_response)
if mode_response.startswith("Standard"):
mode = "Normal"
gglobs.GSinPCmode = False
gglobs.GSDeviceDetected = "Gamma-Scout Classic"
elif mode_response.startswith("Online"):
mode = "Normal"
gglobs.GSinPCmode = False
gglobs.GSDeviceDetected = "Gamma-Scout Online"
elif mode_response.startswith("Version"):
mode = "PC"
gglobs.GSinPCmode = True
gglobs.GSDeviceDetected = None
else:
mode = "Failure"
gglobs.GSinPCmode = False
gglobs.GSDeviceDetected = None
setDebugIndent(0)
return mode
def _get_v2_response(device):
"""writing 'v' to the counter, while in PC Mode, to return Details:
like: ['', 'Version 6.10 d93683 4213 18.08.19 17:42:05'],
then extract:
- fw version
- SN
- number of used bytes in the protocol memory
- date and time
Valid in PC mode up to fw 6.1x
"""
wprint("_get_v2_response of device: '{}'".format(device))
setDebugIndent(1)
if not gglobs.GSinPCmode:
setDebugIndent(0)
return None, -1, "Device is NOT in PC Mode"
ok = _writeToDevice(b'v', device)
if not ok:
setDebugIndent(0)
return None, -1, "Failure writing 'v2' request"
v2_result = _readCommandFromDevice(device, bytecount=44)
wprint("_get_v2_response: response: '{}'".format(v2_result))
fw_version, serial_no, maxbytes, device_time = _extractExtendedInfo(v2_result)
gglobs.GSFirmware = fw_version
gglobs.GSMemory = maxbytes
gglobs.GSSerialNumber = serial_no
gglobs.GSDateTime = device_time
setDebugIndent(0)
return v2_result, 0, ""
def _get_v1_response(device):
"""writes 'v' to the counter while in NormalMode,
expected return: ['', 'Standard']
"""
# not used anymore
# original: # b'\r\nStandard\r\n'
fncname = "_get_v1_response: "
wprint(fncname + "of device: '{}'".format(device))
setDebugIndent(1)
ok = _writeToDevice(b'v', device)
if not ok:
edprint(fncname + "Failure writing 'v' for v1 request to device ", device)
return None, -1, "Could not write 'v' to the device"
v1_result = _readCommandFromDevice(device, bytecount=10)
wprint(fncname + "Data received: ", v1_result)
setDebugIndent(0)
#if len(v1_result) > 0: return v1_result[1], 0, ""
if len(v1_result) > 0: return v1_result, 0, ""
else:
edprint(fncname + "No response on 'v1' request")
return None, -1, "Device did not responsd to 'v' request"
def _readDataFromDevice(device, saveToBin=False, bytecount=None):
"""read data from the Gamma Scout device 'device';
argument 'device' currently not used
(GSDeviceName = 'Gamma-Scout' is set in init"""
fncname = "_readDataFromDevice: "
vprint(fncname + "Want to read {} bytes; Bytes waiting at start: {}".format(bytecount, gglobs.GSser.in_waiting))
setDebugIndent(1)
start = time.time()
devicedata = gglobs.GSser.read(bytecount) # reads data as list of BYTES
vprint(fncname + "Read {} bytes in {:0.4f} sec".format(len(devicedata), time.time() - start))
while True:
time.sleep(0.1)
bw = gglobs.GSser.in_waiting
wprint(fncname + "{} bytes waiting".format(bw))
if bw > 0: devicedata += gglobs.GSser.read(bw)
else: break
vprint(fncname + "Read {} bytes in {:0.4f} sec".format(len(devicedata), time.time() - start))
if time.time() - start > gglobs.GStimeout:
efprint("<br>ALERT: History may be incomplete as a reading timeout seems to have occured !<br>")
data = devicedata.strip().decode("UTF-8")
dumpdata = data.split("\r\n")
wprint(fncname + "devicedata: type: {:16s}, total length: {}".format(str(type(devicedata)), len(devicedata)))
wprint(fncname + "dumpdata: type: {:16s}, total length: {}".format(str(type(dumpdata)), len(dumpdata)))
wprint(fncname + "Data read (after stripping and decoding):\n{}".format(data))
# write device data to database
if saveToBin: gsql.DB_insertBin(gglobs.hisConn, devicedata)
setDebugIndent(0)
return dumpdata
def _readCommandFromDevice(device, bytecount):
"""read bytcount command data (=single line) from the Gamma Scout device 'device';
argument 'device' currently not used
(GSDeviceName = 'Gamma-Scout' is set in init"""
fncname = "_readCommandFromDevice: "
vprint(fncname + "Want to read {} bytes; Bytes waiting at start: {}".format(bytecount, gglobs.GSser.in_waiting))
setDebugIndent(1)
start = time.time()
devicedata = gglobs.GSser.read(bytecount) # reads data as sequence of BYTES
vprint(fncname + "Read {} bytes in {:0.4f}sec".format(len(devicedata), time.time() - start))
time.sleep(0.1)
_clearPipeline(device)
answer = devicedata.strip().decode("UTF-8")
vprint(fncname + "Read response '{}'".format(answer))
setDebugIndent(0)
return answer
def _writeToDevice(wdata, device):
"""writing wdata to device; type(wdata)=bytes, type(device)=str
returns True if ok, otherwise False"""
fncname = "_writeToDevice: "
if gglobs.GSser is None:
edprint(fncname + "Serial Port is closed; cannot write to it!")
return False
waiting_time = 4 # up to sec to wait for seeing data in serial port
start = time.time()
wprint(fncname + "Writing {} bytes data '{}' to device '{}'".format(len(wdata), wdata, device))
setDebugIndent(1)
try:
bytesWritten = gglobs.GSser.write(wdata) # this line writes the data to the port
except Exception as e:
edprint(fncname + "Exception writing to device '{}': ".format(device), e)
setDebugIndent(0)
return False
stop = time.time()
deltat = (stop - start)
if bytesWritten == len(wdata):
ok = True
wprint(fncname + "Writing OK, {} bytes written in {:0.4f} sec".format(bytesWritten, deltat))
while time.time() < (stop + waiting_time):
bw = gglobs.GSser.in_waiting
if bw > 0: break
time.sleep(0.001)
stop2 = time.time()
wprint(fncname + "Found {} bytes waiting after: {:0.4f}sec".format(bw, (stop2 - stop)))
else:
ok = False
wprint(fncname + "FAILURE writing to device in {:0.4f}sec : {} bytes written, but write data {} has length {}".format(deltams, bytesWritten, wdata, len(wdata)))
setDebugIndent(0)
return ok
#
# Public Functions
#
def GSmakeHistory(source, device): # source == "GSDevice" oder "GSDatFile"
"""read the history from a Gamma-Scout device or dump-file"""
fncname = "GSmakeHistory: "
if not gglobs.GSActivation:
dprint(fncname + "Gamma-Scout device not activated")
return -1, "Gamma-Scout device not activated"
dprint(fncname + "make Gamma-Scout history from: ", source)
setDebugIndent(1)
error = 0 # default: no error
message = "" # default: no message
# get data from Gamma-Scout device
if source == "GSDevice":
if not gglobs.GSConnection:
dprint(fncname + "Gamma-Scout device not connected")
setDebugIndent(0)
return -1, "Gamma-Scout device not connected"
if not gglobs.GSinPCmode:
dprint(fncname + "Cannot read from Gamma-Scout device - it is not in PC Mode")
setDebugIndent(0)
return -1, "Cannot read from Gamma-Scout device - it is not in PC Mode"
data_originDB = (stime(), gglobs.GSDeviceName)
# find the maxbytes value
res, error, message = _get_v2_response(device)
if error < 0:
edprint(fncname + "Failure getting v2 response")
setDebugIndent(0)
return error, message
# when in PC mode: 'b' dumps protocol memory (up to fw 6.1x; but apparently in all other modes too!)
ok = _writeToDevice(b'b', device) # correct
dumpdata = _readDataFromDevice(device, saveToBin=True, bytecount=gglobs.GSMemory * 2)
dbheader = "File created by dumping history from device"
dborigin = "Download from device"
dbdevice = "{}".format(data_originDB[1])
# get data from a GammaScout *.dat file
elif source == "GSDatFile":
data_originDB = (stime(), gglobs.datFilePath)
vprint("Make History as Database *.hisdb file from GS *.dat file: ", gglobs.datFilePath)
dumpdata = _readDataFromFile(gglobs.datFilePath)
for a in dumpdata[:15]: wprint(a)
# get version and more
fw_version, serial_no, maxbytes, device_time = _extractExtendedInfo(dumpdata[0])
gglobs.GSFirmware = fw_version
gglobs.GSSerialNumber = serial_no
gglobs.GSMemory = maxbytes
gglobs.GSDateTime = device_time
dbheader = "This file created from History Dump *.dat Data from file"
dborigin = gglobs.datFilePath
dbdevice = "<from file>"
# Programming error
else:
printProgError(fncname + "Programming Error - wrong source: ", source)
wprint("CounterFirmware: {}".format(gglobs.GSFirmware))
wprint("SerialNumber: {}".format(gglobs.GSSerialNumber))
wprint("maxbytes: {} (0x{:04X})".format(gglobs.GSMemory, gglobs.GSMemory))
wprint("DateTime: {}".format(gglobs.GSDateTime))
hisbytes = _convertDumpToBinList(dumpdata)
if len(hisbytes) == 0:
error = -1
message = "No valid data found!"
dprint(message)
else:
# parse HIST data
gglobs.HistoryDataList = []
gglobs.HistoryParseList = []
gglobs.HistoryCommentList = []
_getParsedHistory(hisbytes, maxbytes=gglobs.GSMemory)
# add headers
dbhisClines = [None] * 3
# ctype jday, jday modifier to use time unmodified
dbhisClines[0] = ["HEADER", None, "0 hours", dbheader]
dbhisClines[1] = ["ORIGIN", None, "0 hours", dborigin]
dbhisClines[2] = ["DEVICE", None, "0 hours", dbdevice]
# write to database
gsql.DB_insertDevice (gglobs.hisConn, *data_originDB)
gsql.DB_insertComments (gglobs.hisConn, dbhisClines)
gsql.DB_insertComments (gglobs.hisConn, gglobs.HistoryCommentList)
gsql.DB_insertData (gglobs.hisConn, gglobs.HistoryDataList)
gsql.DB_insertParse (gglobs.hisConn, gglobs.HistoryParseList)
fprint("Got memory dump of {} words ({:0.0%} of total); found {} records".format(gglobs.GSMemory, gglobs.GSMemory / (2**15-1),len(gglobs.HistoryDataList) ))
fprint("Database is created", debug=gglobs.debug)
setDebugIndent(0)
return error, message
def GSsaveHistDatData():
"""get the Gamma-Scout data from the database and save to *.dat file"""
if gglobs.hisConn == None:
gglobs.exgg.showStatusMessage("No data available")
return
fncname = "GSsaveHistDatData: "
fprint(header("Save History Dump Data to File"))
fprint("from: {}\n".format(gglobs.hisDBPath))
hist = gsql.DB_readBinblob(gglobs.hisConn)
if hist == None:
fprint("No dump data found in this database", error=True)
return
vprint(fncname + "hist: type: ", type(hist))
newpath = gglobs.hisDBPath + ".dat"
if isinstance(hist, str):
gglobs.exgg.setBusyCursor()
#fprint(hist)
writeBinaryFile(newpath, bytes(hist, 'utf-8'))
gglobs.exgg.setNormalCursor()
elif isinstance(hist, bytes):
gglobs.exgg.setBusyCursor()
#fprint(hist.decode("UTF-8"))
writeBinaryFile(newpath, hist)
gglobs.exgg.setNormalCursor()
vprint(fncname + "saved as file: " + newpath)
fprint("saved as file: " + newpath)
def GSshowDatData(*args):
"""Show Gamma-Scout type Dat Data from the database table bin"""
if gglobs.hisConn == None:
gglobs.exgg.showStatusMessage("No data available")
return
fprint(header("Show History Dat Data"))
hist = gsql.DB_readBinblob(gglobs.hisConn)
#print("createLstFromDB: hist:", hist)
if hist == None:
fprint("No Dat data found in this database", error=True)
return
if isinstance(hist, str):
gglobs.exgg.setBusyCursor()
fprint(hist)
gglobs.exgg.setNormalCursor()
elif isinstance(hist, bytes):
gglobs.exgg.setBusyCursor()
fprint(hist.decode("UTF-8"))
gglobs.exgg.setNormalCursor()
else:
fprint("Cannot read the Dat data; possibly not of Gamma-Scout origin")
def GSsetDeviceToNormalMode(device):
"""writes 'X' to the counter to switch from PC Mode back to Normal Mode;
response: when counter was in PC Mode : 'PC-Mode beendet'
when counter was in Normal Mode : ''
return: on success: True (and gglobs.GSinPCmode = False)
else : False (and gglobs.GSinPCmode = Unchanged)
valid up to fw 6.1x"""
fncname = "GSsetDeviceToNormalMode: "
success = False
wprint(fncname + "of device: '{}'".format(device))
setDebugIndent(1)
ok = _writeToDevice(b'X', device)
if ok:
X_result = _readCommandFromDevice(device, bytecount=17) # with final CR+LF there are 19 bytes
if X_result == 'PC-Mode beendet':
success = True
gglobs.GSinPCmode = False
wprint(fncname + "Response: '{}'".format(X_result))
elif X_result == '':
success = True
gglobs.GSinPCmode = False
wprint(fncname + "Response: '{}' (Counter had already been in Normal Mode)".format(X_result))
else:
success = False
wprint(fncname + "Failure ending PC Mode; response: '{}'".format(X_result))
if len(X_result) > 0: retval = X_result, 0, ""
else: retval = None, -1, "No response on 'X' request"
else:
success = False
wprint(fncname + "Failure when writing 'X' to device '{}'".format(device))
setDebugIndent(0)
return success
def GSsetDeviceToPCMode(device):
"""Set Gamma-Scout into PC Mode by sending P to device (same as pressing
PC button at device) expecting answer: 'PC-Mode gestartet'
return: on success: True (and gglobs.GSinPCmode = True)
else : False (and gglobs.GSinPCmode = unchanged)
"""
fncname = "GSsetDeviceToPCMode: "
success = False
vprint(fncname + "of device: '{}'".format(device))
setDebugIndent(1)
ok = _writeToDevice(b'P', device)
if ok:
P_result = _readCommandFromDevice(device, bytecount=19)
if P_result == 'PC-Mode gestartet':
success = True
gglobs.GSinPCmode = True
wprint(fncname + "Response: '{}'".format(P_result))
else:
success = False
wprint(fncname + "Failure starting PC Mode; response: '{}'".format(P_result))
else:
success = False
wprint(fncname + "Failure when writing 'P' to device '{}'".format(device))
setDebugIndent(0)
return success
def getGammaScoutValues(varlist):
"""get the values for all vars - but for the non-live counters there aren't any"""
alldata = {}
fncname = "getGammaScoutValues: "
return alldata # this device cannot log
if not gglobs.GSConnection:
dprint(fncname + "NO Gamma-Scout connection")
return alldata
dprint(fncname + "Gamma-Scout has no loggable variables")
return alldata
def autoBaudrateGammaScout(usbport):
"""Tries to find a proper baudrate by testing for successful serial
communication at up to all possible baudrates, beginning with the
highest"""
"""
NOTE: the device port can be opened without error at any baudrate,
even when no communication can be done, e.g. due to wrong baudrate.