-
Notifications
You must be signed in to change notification settings - Fork 1
/
gcommands.py
2334 lines (1866 loc) · 91.2 KB
/
gcommands.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 -*-
"""
gcommands.py - GeigerLog commands to handle the Geiger counter
include in programs with:
import gcommands
"""
###############################################################################
# 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__ = ["Phil Gillaspy", "GQ"]
__license__ = "GPL3"
# credits:
# device command coding taken from:
# Phil Gillaspy, https://sourceforge.net/projects/gqgmc/
# and GQ document 'GQ-RFC1201.txt'
# (GQ-RFC1201,GQ Geiger Counter Communication Protocol, Ver 1.40 Jan-2015)
# and GQ's disclosure at:
# http://www.gqelectronicsllc.com/forum/topic.asp?TOPIC_ID=4948
from gutils import *
#
# Commands and functions implemented in device
#
def getVER():
# Get hardware model and version
# send <GETVER>> and read 14 bytes
# returns total of 14 bytes ASCII chars from GQ GMC unit.
# includes 7 bytes hardware model and 7 bytes firmware version.
# e.g.: 'GMC-300Re 4.20'
# ATTENTION: new counters may deliver 15 bytes. e.g. "GMC-500+Re 1.18",
# the 500 version with firmware 1.18
dprint("getVER:")
setDebugIndent(1)
recd = None
rec, error, errmessage = serialCOMM(b'<GETVER>>', 14, orig(__file__))
#for i in range(0,14):
# print("i={:2d} rec={:08b} rec={:02X}".format(i, rec[i], rec[i]))
if error >= 0:
try:
recd = rec.decode('UTF-8') # convert from bytes to str
except Exception as e:
error = -1
errmessage = "ERROR getting Version - Bytes are not ASCII: " + str(rec)
exceptPrint(e, sys.exc_info(), errmessage)
recd = str(rec)
# FOR TESTING ONLY - start GL with command: 'testing'
if gglobs.testing:
pass
#recd = "GMC-300Re 3.20"
#recd = "GMC-300Re 4.20"
#recd = "GMC-300Re 4.22"
recd = "GMC-320Re 3.22" # device used by user katze
#recd = "GMC-320Re 4.19"
#recd = "GMC-320Re 5.xx"
#recd = "GMC-500Re 1.00"
#recd = "GMC-500Re 1.08"
#recd = "GMC-500+Re 1.0x"
#recd = "GMC-500+Re 1.18"
#recd = "GMC-600Re 1.xx"
#recd = "GMC-600+Re 2.xx" # fictitious device; not (yet) existing
# simulates the bug in the 'GMC-500+Re 1.18'
#recd = ""
try:
lenrec = len(rec)
except:
lenrec = None
dprint("getVER: len:{}, rec:\"{}\", recd='{}', err={}, errmessage='{}'".format(lenrec, rec, recd, error, errmessage))
setDebugIndent(0)
return (recd, error, errmessage)
def getValuefromRec(rec, maskHighBit=False):
"""calclate the CPM, CPS value from a record"""
if gglobs.nbytes == 2 and maskHighBit==True:
value = (rec[0] & 0x3f) << 8 | rec[1]
elif gglobs.nbytes == 2 and maskHighBit==False:
value = rec[0]<< 8 | rec[1]
elif gglobs.nbytes == 4 :
value = ((rec[0]<< 8 | rec[1]) << 8 | rec[2]) << 8 | rec[3]
return value
def getGMCValues(varlist):
"""return all values in the varlist
NOTE: the getC* functions all return (value, error, errmessage)
now only value will be forwarded
"""
alldata = {}
if not gglobs.GMCConnection: # GMCcounter is NOT connected!
dprint("getGMCValues: GMC Counter is not connected")
return alldata
if varlist == None:
return alldata
for vname in varlist:
if vname == "CPM": alldata[vname] = getCPM() [0] # counts per MINUTE
elif vname == "CPS": alldata[vname] = getCPS() [0] # counts per SECOND
elif vname == "CPM1st": alldata[vname] = getCPML()[0] # CPM from 1st tube, normal tube
elif vname == "CPS1st": alldata[vname] = getCPSL()[0] # CPS from 1st tube, normal tube
elif vname == "CPM2nd": alldata[vname] = getCPMH()[0] # CPM from 2nd tube, extra tube
elif vname == "CPS2nd": alldata[vname] = getCPSH()[0] # CPS from 2nd tube, extra tube
elif vname == "X": alldata[vname] = getDateTimeAsNum() # time as read from device
vprint("{:20s}: Variables:{} Data:{}".format("getGMCValues", varlist, alldata))
return alldata
def getCPM():
# Get current CPM value
# send <GETCPM>> and read 2 bytes
# In total 2 bytes data are returned from GQ GMC unit
# as a 16 bit unsigned integer.
# The first byte is MSB byte data and second byte is LSB byte data.
# e.g.: 00 1C -> the returned CPM is 28
# e.g.: 0B EA -> the returned CPM is 3050
#
# return CPM, error, errmessage
wprint("getCPM: standard command")
setDebugIndent(1)
value = gglobs.NAN
rec, error, errmessage = serialCOMM(b'<GETCPM>>', gglobs.nbytes, orig(__file__))
if error >= 0:
value = getValuefromRec(rec, maskHighBit=False)
value = scaleVarValues("CPM", value, gglobs.ValueScale["CPM"])
wprint("getCPM: rec= {}, value= {}, err= {}, errmsg= {}".format(rec, value, error, errmessage ))
setDebugIndent(0)
return (value, error, errmessage)
def getCPS():
# Get current CPS value
# send <GETCPS>> and read 2 bytes
# In total 2 bytes data are returned from GQ GMC unit
#
# Comment from Phil Gallespy:
# 1st byte is MSB, but note that upper two bits are reserved bits.
# cps_int |= ((uint16_t(cps_char[0]) << 8) & 0x3f00);
# cps_int |= (uint16_t(cps_char[1]) & 0x00ff);
# my observation: highest bit in MSB is always set!
# e.g.: 80 1C -> the returned CPS is 28
# e.g.: FF FF -> = 3F FF -> the returned maximum CPS is 16383
# or 16383 * 60 = 982980 CPM
#
# return CPS, error, errmessage
wprint("getCPS: standard command")
setDebugIndent(1)
value = gglobs.NAN
rec, error, errmessage = serialCOMM(b'<GETCPS>>', gglobs.nbytes, orig(__file__))
if error >= 0:
value = getValuefromRec(rec, maskHighBit=True)
value = scaleVarValues("CPS", value, gglobs.ValueScale["CPS"])
wprint("getCPS: rec=", rec, ", value=", value)
setDebugIndent(0)
return (value, error, errmessage)
def getCPML():
"""get CPM from High Sensitivity tube that should be the 'normal' tube"""
wprint("getCPML: 1st tube, HIGH sensitivity")
setDebugIndent(1)
value = gglobs.NAN
rec, error, errmessage = serialCOMM(b'<GETCPML>>', gglobs.nbytes, orig(__file__))
if error >= 0:
value = getValuefromRec(rec, maskHighBit=False)
value = scaleVarValues("CPM1st", value, gglobs.ValueScale["CPM1st"])
wprint("getCPML: rec=", rec, ", value=", value)
setDebugIndent(0)
return (value, error, errmessage)
def getCPMH():
"""get CPM from Low Sensitivity tube that should be the 2nd tube in the 500+"""
wprint("getCPMH: 2nd tube, LOW sensitivity")
setDebugIndent(1)
value = gglobs.NAN
rec, error, errmessage = serialCOMM(b'<GETCPMH>>', gglobs.nbytes, orig(__file__))
if error >= 0:
value = getValuefromRec(rec, maskHighBit=False)
value = scaleVarValues("CPM2nd", value, gglobs.ValueScale["CPM2nd"])
wprint("getCPMH: rec=", rec, ", value=", value)
setDebugIndent(0)
return (value, error, errmessage)
def getCPSL():
"""get CPS from High Sensitivity tube that should be the 'normal' tube"""
wprint("getCPSL: 1st tube, HIGH sensitivity")
setDebugIndent(1)
value = gglobs.NAN
rec, error, errmessage = serialCOMM(b'<GETCPSL>>', gglobs.nbytes, orig(__file__))
if error >= 0:
value = getValuefromRec(rec, maskHighBit=True)
value = scaleVarValues("CPS1st", value, gglobs.ValueScale["CPS1st"])
wprint("getCPSL: rec=", rec, ", value=", value)
setDebugIndent(0)
return (value, error, errmessage)
def getCPSH():
"""get CPS from Low Sensitivity tube that should be the 2nd tube in the 500+"""
wprint("getCPSH: 2nd tube, LOW sensitivity")
setDebugIndent(1)
value = gglobs.NAN
rec, error, errmessage = serialCOMM(b'<GETCPSH>>', gglobs.nbytes, orig(__file__))
if error >= 0:
value = getValuefromRec(rec, maskHighBit=True)
value = scaleVarValues("CPS2nd", value, gglobs.ValueScale["CPS2nd"])
wprint("getCPSH: rec=", rec, ", value=", value)
setDebugIndent(0)
return (value, error, errmessage)
def turnHeartbeatOn():
# 3. Turn on the GQ GMC heartbeat
# Note: This command enable the GQ GMC unit to send count per second data to host every second automatically.
# Command: <HEARTBEAT1>>
# Return: A 16 bit unsigned integer is returned every second automatically. Each data package consist of 2 bytes data from GQ GMC unit.
# The first byte is MSB byte data and second byte is LSB byte data.
# e.g.: 10 1C the returned 1 second count is 28. Only lowest 14 bits are used for the valid data bit.
# The highest bit 15 and bit 14 are reserved data bits.
# Firmware supported: GMC-280, GMC-300 Re.2.10 or later
dprint("turnHeartbeatOn:")
setDebugIndent(1)
if gglobs.GMCser == None:
rec = ""
error = 1
errmessage = "No serial connection"
else:
rec, error, errmessage = serialCOMM(b'<HEARTBEAT1>>', 0, orig(__file__))
dprint("turnHeartbeatOn: rec='{}', err={}, errmessage='{}'".format(rec, error, errmessage))
setDebugIndent(0)
return (rec, error, errmessage)
def turnHeartbeatOFF():
# 4. Turn off the GQ GMC heartbeat
# Command: <HEARTBEAT0>>
# Return: None
# Firmware supported: Re.2.10 or later
dprint("turnHeartbeatOFF:")
setDebugIndent(1)
if gglobs.GMCser == None:
rec = ""
error = 1
errmessage = "No serial connection"
else:
rec, error, errmessage = serialCOMM(b'<HEARTBEAT0>>', 0, orig(__file__))
dprint("turnHeartbeatOFF: rec='{}', err={}, errmessage='{}'".format(rec, error, errmessage))
setDebugIndent(0)
return (rec, error, errmessage)
def getVOLT():
# Get battery voltage status
# send <GETVOLT>> and read 1 byte
# returns one byte voltage value of battery (X 10V)
# e.g.: return 62(hex) is 9.8V
# Example: Geiger counter GMC-300E+
# with Li-Battery 3.7V, 800mAh (2.96Wh)
# -> getVOLT reading is: 4.2V
# -> Digital Volt Meter reading is: 4.18V
#
# GMC 500/600 is different. Delivers 5 ASCII bytes
# z.B.[52, 46, 49, 49, 118] = "4.11v"
dprint("getVOLT:")
setDebugIndent(1)
if gglobs.GMCser == None:
rec = ""
error = 1
errmessage = "No serial connection"
else:
rec, error, errmessage = serialCOMM(b'<GETVOLT>>', gglobs.voltagebytes, orig(__file__))
dprint("getVOLT: VOLT: raw:", rec)
######## TESTING (uncomment all 4) #############
#rec = b'3.76v' # 3.76 Volt
#error = 1
#errmessage = "testing"
#dprint("getVOLT: TESTING with rec=", rec, debug=True)
################################################
if error == 0 or error == 1:
if gglobs.voltagebytes == 1:
rec = str(rec[0]/10.0)
elif gglobs.voltagebytes == 5:
rec = rec.decode('UTF-8')
else:
rec = str(rec) + " @config: voltagebytes={}".format(gglobs.voltagebytes)
else:
rec = "ERROR"
error = 1
errmessage = "getVOLT: ERROR getting voltage"
dprint("getVOLT: Using config setting voltagebytes={}: Voltage='{}', err={}, errmessage='{}'".format(gglobs.voltagebytes, rec, error, errmessage))
setDebugIndent(0)
return (rec, error, errmessage)
def getSPIR(address = 0, datalength = 4096):
# Request history data from internal flash memory
# Command: <SPIR[A2][A1][A0][L1][L0]>>
# A2,A1,A0 are three bytes address data, from MSB to LSB.
# The L1,L0 are the data length requested.
# L1 is high byte of 16 bit integer and L0 is low byte.
# The length normally not exceed 4096 bytes in each request.
# Return: The history data in raw byte array.
# Comment: The minimum address is 0, and maximum address value is
# the size of the flash memory of the GQ GMC Geiger count. Check the
# user manual for particular model flash size.
# address must not exceed 2^(3*8) = 16 777 215 because high byte
# is clipped off and only lower 3 bytes are used here
# (but device address is limited to 2^20 - 1 = 1 048 575 = "1M"
# anyway, or even only 2^16 - 1 = 65 535 = "64K" !)
# datalength must not exceed 2^16 = 65536 or python conversion
# fails with error; should be less than 4096 anyway
# device delivers [(datalength modulo 4096) + 1] bytes,
# e.g. with datalength = 4128 (= 4096 + 32 = 0x0fff + 0x0020)
# it returns: (4128 modulo 4096) + 1 = 32 + 1 = 33 bytes
# This contains a WORKAROUND activated with 'SPIRbugfix=True':
# it asks for only (datalength - 1) bytes,
# but as it then reads one more byte, the original datalength is obtained
# BUG ALERT - WORKAROUND ##################################################
#
# GMC-300 : no workaround, but use 2k SPIRpage only!
# GMC-300E+ : use workaround 'datalength - 1' with 4k SPIRpage
# GMC-320 : same as GMC-300E+
# GMC-320+ : same as GMC-300E+
# GMC-500 : ist der Bug behoben oder nur auf altem Stand von GMC-300 zurück?
# workaround ist nur 2k SPIRpage anzufordern
# GMC-500+ : treated as a GM-500
# GMC-600 : treated as a GM-500
# GMC-600+ : treated as a GM-500
# End BUG ALERT - WORKAROUND ##############################################
# address: pack into 4 bytes, big endian; then clip 1st byte = high byte!
ad = struct.pack(">I", address)[1:]
# datalength: pack into 2 bytes, big endian; use all bytes
# but adjust datalength to fix bug!
if gglobs.SPIRbugfix:
dl = struct.pack(">H", datalength - 1)
else:
dl = struct.pack(">H", datalength)
dprint("getSPIR: SPIR requested: address: {:5d}, datalength:{:5d} (hex: address: {:02x} {:02x} {:02x}, datalength: {:02x} {:02x})".format(address, datalength, ad[0], ad[1], ad[2], dl[0], dl[1]))
setDebugIndent(1)
rec, error, errmessage = serialCOMM(b'<SPIR' + ad + dl + b'>>', datalength, orig(__file__)) # returns bytes
if rec != None :
msg = "datalength={:4d}".format(len(rec))
else:
msg = "ERROR: No data received!"
dprint("getSPIR: received: {}, err={}, errmessage='{}'".format(msg, error, errmessage))
setDebugIndent(0)
return (rec, error, errmessage)
def getHeartbeatCPS():
"""read bytes until no further bytes coming"""
# Caution: untested; might not be working
if not gglobs.debug: return # execute only in debug mode
eb= 0
while True: # read more until nothing is returned
# (actually, never more than 1 more is returned)
eb += 1
rec = ""
rec = gglobs.GMCser.read(2)
#rec = list(map (ord, rec)) # fails with py3????????????????
rec = rec # not tested with Py3 !!!
cps = ((rec[0] & 0x3f) << 8 | rec[1])
#print( "eb=", eb, "cps:", cps)
break
return cps
def getExtraByte():
"""read single bytes until no further bytes coming"""
xrec = b""
try: # failed when called from 2nd instance of GeigerLog; just to avoid error
bytesWaiting = gglobs.GMCser.in_waiting
except:
#print("---------------------getExtraByte: failed try")
bytesWaiting = 0
#bytesWaiting += 5 # TESTING ONLY
if bytesWaiting == 0:
wprint("getExtraByte: No extra bytes waiting for reading")
else:
vprint("getExtraByte: Bytes waiting: {}".format(bytesWaiting), verbose=True)
while True: # read single byte until nothing is returned
try:
x = gglobs.GMCser.read(1)
except Exception as e:
edprint("getExtraByte: Exception: {}".format(e))
exceptPrint(e, sys.exc_info(), "getCFG: Exception: ")
x = 0
if len(x) == 0: break
xrec += x
vprint("getExtraByte: Got {} extra bytes from reading-pipeline:".format(len(xrec)), list(xrec), verbose=True)
vprint("getExtraByte: Extra bytes as ASCII: '{}'".format(BytesAsASCII(xrec)), verbose=True)
return xrec
def getCFG():
# Get configuration data
# send <GETCFG>> and read 256 bytes
# returns
# - the cfg as a Python bytes string, should be 256 or 512 bytes long
# - the error value (0, -1, +1)
# - the error message as string
# has set gglobs.cfg with read cfg
# NOTE: 300series uses 256 bytes; 500 and 600 series 512 bytes
# config size is determined by deviceproperties
"""
The meaning is: (from: http://www.gqelectronicsllc.com/forum/topic.asp?TOPIC_ID=4447)
CFG data Offset table. Start from 0
'*' in left column = read and/or set by GeigerLog
==========================================
* PowerOnOff, //to check if the power is turned on/off intended
* AlarmOnOff, //1
* SpeakerOnOff,
GraphicModeOnOff,
BackLightTimeoutSeconds,
IdleTitleDisplayMode,
AlarmCPMValueHiByte, //6
AlarmCPMValueLoByte,
* CalibrationCPMHiByte_0,
* CalibrationCPMLoByte_0,
* CalibrationuSvUcByte3_0,
* CalibrationuSvUcByte2_0, //11
* CalibrationuSvUcByte1_0,
* CalibrationuSvUcByte0_0,
* CalibrationCPMHiByte_1,
* CalibrationCPMLoByte_1, //15
* CalibrationuSvUcByte3_1,
* CalibrationuSvUcByte2_1,
* CalibrationuSvUcByte1_1,
* CalibrationuSvUcByte0_1,
* CalibrationCPMHiByte_2, //20
* CalibrationCPMLoByte_2,
* CalibrationuSvUcByte3_2,
* CalibrationuSvUcByte2_2,
* CalibrationuSvUcByte1_2,
* CalibrationuSvUcByte0_2, //25
IdleDisplayMode,
AlarmValueuSvByte3,
AlarmValueuSvByte2,
AlarmValueuSvByte1,
AlarmValueuSvByte0, //30
AlarmType,
* SaveDataType,
SwivelDisplay,
ZoomByte3,
ZoomByte2, //35
ZoomByte1,
ZoomByte0,
SPI_DataSaveAddress2,
SPI_DataSaveAddress1,
SPI_DataSaveAddress0, //40
SPI_DataReadAddress2,
SPI_DataReadAddress1,
SPI_DataReadAddress0,
* PowerSavingMode,
Reserved, //45
Reserved,
Reserved,
DisplayContrast,
* MAX_CPM_HIBYTE,
* MAX_CPM_LOBYTE, //50
Reserved,
LargeFontMode,
LCDBackLightLevel,
ReverseDisplayMode,
MotionDetect, //55
# http://www.gqelectronicsllc.com/forum/topic.asp?TOPIC_ID=5278, Reply #47
# bBatteryType: 1 is non rechargeable. 0 is chargeable.
bBatteryType,
* BaudRate,
Reserved,
GraphicDrawingMode,
LEDOnOff,
Reserved,
SaveThresholdValueuSv_m_nCPM_HIBYTE,
SaveThresholdValueuSv_m_nCPM_LOBYTE,
SaveThresholdMode,
SaveThresholdValue3, # 65 by my count
SaveThresholdValue2,
SaveThresholdValue1,
SaveThresholdValue0,
Save_DateTimeStamp, //this one uses 6 byte space # 69 by my count
!!! but in the GMC-300E this is at #62 !!!
# from: http://www.gqelectronicsllc.com/forum/topic.asp?TOPIC_ID=4948 Reply #17
CFG_nSaveThresholdValue3, //65
CFG_nSaveThresholdValue2,
CFG_nSaveThresholdValue1,
CFG_nSaveThresholdValue0,
CFG_SSID_0, // 69
//...
CFG_SSID_31 = CFG_SSID_0 + 31, //69 + 31
CFG_Password_0, //101
//...
CFG_Password_31 = CFG_Password_0 + 31, //101 + 31
CFG_Website_0, //133
//....
CFG_Website_31 = CFG_Website_0 + 31, //133 + 31
CFG_URL_0, //165
//....
CFG_URL_31 = CFG_URL_0 + 31, //165 + 31
CFG_UserID_0, //197
//...........
CFG_UserID_31 = CFG_UserID_0 + 31, //197+31
CFG_CounterID_0, //229
//....
CFG_CounterID_31 = CFG_CounterID_0 + 31, //229 + 31
CFG_Period, //261
CFG_WIFIONOFF, //262
CFG_TEXT_STATUS_MODE,
Meaning of the values:
PowerOnOff byte:
300 series : 0 for ON and 255 for OFF
500 and 600 series: 0 for ON and 1 for OFF # strange, but EmfDev in :
http://www.gqelectronicsllc.com/forum/topic.asp?TOPIC_ID=4948 Reply #14
CFG_TEXT_STATUS_MODE is for displaying the "normal/medium/high" text in the large font mode.
0 - "Off" - not displayed
1 - "On" - black text and white background
2 - "Inverted" - White text and black background
calibration data:
300 series: little-endian
500/600 series: big-endian
from: http://www.gmcmap.com/tutorial.asp
*The Wifi feature is only available on models GMC 320+ V5, GMC 500, GMC 500+, GMC 600, GMC 600+ or later.
**If you are using a GMC-600 or GMC-600+, after factory reset, please go to Wifi, Reset Wifi Module, press YES.
-->??? is 320+ using a 256 or 512 bytes config???
"""
dprint("getCFG:")
setDebugIndent(1)
getExtraByte() # cleaning buffer before getting cfg (some problem in the 500)
cfg = b""
error = 0
errmessage = ""
cfg, error, errmessage = serialCOMM(b'<GETCFG>>', gglobs.configsize, orig(__file__))
#print("getCFG: cfg:", cfg, type(cfg))
####### BEGIN TESTDATA ####################################################
# replaces rec with data from other runs
# requires that a testdevice was activated in getVER; the only relevant
# prperty is the length of config
if gglobs.testing: # only do this when command line command testing was used
if gglobs.configsize == 512:
# using a 512 bytes sequence; data from a GMC-500(non-plus) readout
#
# power is OFF:
cfg500 = "01 00 00 02 1F 00 00 64 00 3C 3E C7 AE 14 27 10 42 82 00 00 00 19 41 1C 00 00 00 3F 00 00 00 00 02 02 00 00 00 00 FF FF FF FF FF FF 00 01 00 78 0A FF FF 3C 00 05 FF 01 00 00 0A 00 01 0A 00 64 00 3F 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 77 77 77 2E 67 6D 63 6D 61 70 2E 63 6F 6D 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 6C 6F 67 32 2E 61 73 70 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 00 02 11 05 1E 10 34 05 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF".split(' ')
# power is ON:
cfg500 = ["0"] + cfg500[1:]
#print("cfg500:\n", len(cfg500), cfg500)
cfg = b''
for a in cfg500: cfg += bytes([int(a, 16)])
elif "GMC-320Re 5." in gglobs.GMCdeviceDetected : # 320v5 device (with WiFi)
cfg = (cfg[:69] + b'abcdefghijklmnopqrstuvwxyz0123456789' * 10)[:256] # to simulate WiFi settings
elif gglobs.configsize == 256: # 320v4 device
# next line from 320+ taken on: 2017-05-26 15:29:38
# power is off:
cfg320plus = [255, 0, 0, 2, 31, 0, 0, 100, 0, 60, 20, 174, 199, 62, 0, 240, 20, 174, 199, 63, 3, 232, 0, 0, 208, 64, 0, 0, 0, 0, 63, 0, 2, 2, 0, 0, 0, 0, 255, 255, 255, 255, 255, 255, 0, 1, 0, 120, 25, 255, 255, 60, 1, 8, 255, 1, 0, 254, 10, 0, 1, 10, 0, 100, 0, 0, 0, 0, 63, 17, 5, 23, 16, 46, 58, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255]
# power is on:
cfg320plus = [ 0] + cfg320plus[1:]
cfg = b''
for a in cfg320plus: cfg += bytes([a])
else:
#cfg = rec
pass
print("TESTDATA: cfg:", len(cfg), type(cfg), "\n", BytesAsHex(cfg))
#rec = cfg # simulates a return from GETCFG
error = 1
errmessage = "SIMULATION"
####### END TESTDATA ######################################################
if cfg != None:
dprint("getCFG: Got {} bytes as {}" .format(len(cfg), type(cfg)))
dprint("getCFG: CFG as HEX : {}" .format(BytesAsHex(cfg)))
dprint("getCFG: CFG as DEC : {}" .format(BytesAsDec(cfg)))
dprint("getCFG: CFG as ASCII: {}" .format(BytesAsASCII(cfg)))
if gglobs.endianness == 'big':
#print "using 500er, 600er: use big-endian"
fString = ">f"
else:
#print "using other than 500er and 600er: use little-endian"
fString = "<f"
try:
for key in gglobs.cfgLowKeys:
gglobs.cfgLow[key] = cfg[gglobs.cfgLowndx[key][0] : gglobs.cfgLowndx[key][1]]
except Exception as e:
edprint("getCFG: Exception: cfgLow[key]: {}".format(e))
exceptPrint(e, sys.exc_info(), "getCFG: Exception: ")
return cfg, -1, "getCFG: Exception: {}".format(e)
#print("cfgLow: len: ", len(gglobs.cfgLow))
#for key in gglobs.cfgLowKeys: print("{:15s} {}".format(key, gglobs.cfgLow[key]))
#print("cfgLow['Speaker']: len: ", len(gglobs.cfgLow["Speaker"]), gglobs.cfgLow["Speaker"])
#print("gglobs.cfgLow['Speaker'][0]", gglobs.cfgLow["Speaker"][0])
try:
# ("Power", "Alarm", "Speaker", "CalibCPM_0", "CalibuSv_0", "CalibCPM_1", "CalibuSv_0", "CalibCPM_2", "CalibuSv_0", "SaveDataType", "MaxCPM", "Baudrate", "Battery")
gglobs.cfgLow["Power"] = gglobs.cfgLow["Power"] [0]
gglobs.cfgLow["Alarm"] = gglobs.cfgLow["Alarm"] [0]
gglobs.cfgLow["Speaker"] = gglobs.cfgLow["Speaker"] [0]
gglobs.cfgLow["SaveDataType"] = gglobs.cfgLow["SaveDataType"] [0]
gglobs.cfgLow["Baudrate"] = gglobs.cfgLow["Baudrate"] [0]
gglobs.cfgLow["Battery"] = gglobs.cfgLow["Battery"] [0]
gglobs.cfgLow["CalibCPM_0"] = struct.unpack(">H", gglobs.cfgLow["CalibCPM_0"] )[0]
gglobs.cfgLow["CalibCPM_1"] = struct.unpack(">H", gglobs.cfgLow["CalibCPM_1"] )[0]
gglobs.cfgLow["CalibCPM_2"] = struct.unpack(">H", gglobs.cfgLow["CalibCPM_2"] )[0]
gglobs.cfgLow["CalibuSv_0"] = struct.unpack(fString, gglobs.cfgLow["CalibuSv_0"] )[0]
gglobs.cfgLow["CalibuSv_1"] = struct.unpack(fString, gglobs.cfgLow["CalibuSv_1"] )[0]
gglobs.cfgLow["CalibuSv_2"] = struct.unpack(fString, gglobs.cfgLow["CalibuSv_2"] )[0]
gglobs.cfgLow["MaxCPM"] = struct.unpack(">H", gglobs.cfgLow["MaxCPM"] )[0]
gglobs.cfgLow["ThresholdMode"] = gglobs.cfgLow["ThresholdMode"] [0]
gglobs.cfgLow["ThresholdCPM"] = struct.unpack(">H", gglobs.cfgLow["ThresholdCPM"] )[0]
gglobs.cfgLow["ThresholduSv"] = struct.unpack(fString, gglobs.cfgLow["ThresholduSv"] )[0]
except Exception as e:
edprint("getCFG: Exception: set cfgLow[key]: {}".format(e))
exceptPrint(e, sys.exc_info(), "getCFG: Exception: ")
return cfg, -1, "getCFG: Exception: set cfgLow[key]: {}".format(e)
#print("cfgLow:")
#for key in gglobs.cfgLowKeys: print("{:15s} {}".format(key, gglobs.cfgLow[key]))
#for i in range(0, 3): print("Calib#{:1d} {}".format(i, gglobs.cfgLow["CalibuSv_{}".format(i)] / gglobs.cfgLow["CalibCPM_{}".format(i)]))
# now get the WiFi part of the config
#wifinames = ("SSID", "Password", "Website", "URL", "UserID", "CounterID", "Period")
if gglobs.configsize == 512:
for key in gglobs.cfgMapKeys:
#print("key: ", key)
try:
gglobs.cfgMap[key] = cfg[gglobs.cfg512ndx[key][0] : gglobs.cfg512ndx[key][1]] .decode("UTF-8").replace("\x00", "")
#gglobs.GMCmap[key] = gglobs.cfgMap[key]
except Exception as e:
dprint("Failure getting gglobs.cfgMap[key] with key==", key, debug=True)
gglobs.cfgMap[key] = "ERROR INVALID"
#gglobs.GMCmap[key] = gglobs.cfgMap[key]
#print("gglobs.cfgMap:", gglobs.cfgMap)
elif "GMC-320Re 5." in gglobs.GMCdeviceDetected: # 320v5 device
for key in gglobs.cfgMapKeys:
try:
gglobs.cfgMap[key] = cfg[gglobs.cfg256ndx[key][0] : gglobs.cfg256ndx[key][1]] .decode("UTF-8").replace("\x00", "")
#gglobs.GMCmap[key] = gglobs.cfgMap[key]
except:
gglobs.cfgMap[key] = "ERROR INVALID"
#gglobs.GMCmap[key] = gglobs.cfgMap[key]
else: # all other non-Wifi counters
for key in gglobs.cfgMapKeys:
gglobs.cfgMap[key] = "None"
#gglobs.GMCmap[key] = gglobs.cfgMap[key]
else:
dprint("getCFG: ERROR: Failed to get any configuration data", debug=True)
gglobs.cfg = cfg # set the global value with whatever you got !!!
setDebugIndent(0)
return cfg, error, errmessage
def writeConfigData(cfgaddress, value):
# 9. Write configuration data
# Command: <WCFG[A0][D0]>>
# A0 is the address and the D0 is the data byte(hex).
# A0 is offset of byte in configuration data.
# D0 is the assigned value of the byte.
# Return: 0xAA
# Firmware supported: GMC-280, GMC-300 Re.2.10 or later
"""
from: http://www.gqelectronicsllc.com/forum/topic.asp?TOPIC_ID=4948 Reply #20
EmfDev:
The <ECFG>> and <CFGUPDATE>> still work.
But the <WCFG[A0][D0]>> is now <WCFG[Hi][Lo][Ch]>>
So instead of 2, there are 3 params.
Hi - High byte
Lo - Low byte
Ch - Byte to write
3c 57 43 46 47 00 02 01 3e 3e example for writing 1 to the speaker.
I think you already know that you can't change a byte from 0x00 to 0x01 but you can change it from 0x01
to 0x00
So one way users can change config is to:
1. <GETCFG>> copy each byte of the config
2. <ECFG>> erase config from the device
3. Edit the config you got from 1 like you can change speaker from 1 to 0 or 0 to 1
4. <WCFG[0x00 - 0x01][0x00 - 0xFF][valid 8bits]>> starting from position 0 to the end of config
5. <CFGUPDATE>>
"""
dprint("writeConfigData: cfgaddress: {}, value: {}".format(cfgaddress, value))
setDebugIndent(1)
# get config
getCFG() # get a fresh config to the global variable
# could there be a problem by using old config? time stamps?
cfg = copy.copy(gglobs.cfg)
cfglen = len(cfg)
if cfglen <= 256 and gglobs.configsize == 256:
doUpdate = 256
elif cfglen <= 512 and gglobs.configsize == 512:
doUpdate = 512
else: # failure:
msg = "Number of configuration data inconsistent with detected device.<br>Updating config will NOT be done, as Device might be damaged by it!"
fprint(msg, error=True)
dprint("writeConfigData: " + msg.replace('<br>', ' '), debug=True)
setDebugIndent(0)
return
# remove right side FFs; will be default after erasing
cfgstrip = cfg.rstrip(b'\xFF')
dprint("writeConfigData: Config right-stripped from FF: len:{}: ".format(len(cfgstrip)), BytesAsHex(cfgstrip))
# modify config at cfgaddress
cfgmod = cfgstrip[:cfgaddress] + bytes([value]) + cfgstrip[cfgaddress + 1:]
dprint("writeConfigData: Config mod @address: {}; new len:{}: ".format(cfgaddress, len(cfgmod)), BytesAsHex(cfgmod))
# erase config
dprint("WriteConfig: Erase Config")
setDebugIndent(1)
rec = serialCOMM(b'<ECFG>>', 1, orig(__file__))
setDebugIndent(0)
dprint("WriteConfig: Write new Config Data for Config Size:{}".format(doUpdate))
if doUpdate == 256:
pfs = "i=A0={:>3d}(0x{:02X}), cfgval=D0={:>3d}(0x{:02X})" # formatted print string
# write config of up to 256 bytes
for i, c in enumerate(cfgmod):
A0 = bytes([i])
D0 = bytes([c])
vprint("WriteConfig: " + pfs.format(i, int.from_bytes(A0, byteorder='big'), c, int.from_bytes(D0, byteorder='big')))
setDebugIndent(1)
rec = serialCOMM(b'<WCFG' + A0 + D0 + b'>>', 1, orig(__file__))
setDebugIndent(0)
else: # doUpdate == 512
pfs = "i==A0={:>3d}(0x{:03X}), cfgval==D0={:>3d}(0x{:02X})" # formatted print string
# write config of up to 512 bytes
for i, c in enumerate(cfgmod):
A0 = struct.pack(">H", i) # pack address into 2 bytes, big endian
D0 = bytes([c])
vprint("WriteConfig: " + pfs.format(i, int.from_bytes(A0, byteorder='big'), c, int.from_bytes(D0, byteorder='big')))
setDebugIndent(1)
rec = serialCOMM(b'<WCFG' + A0 + D0 + b'>>', 1, orig(__file__))
setDebugIndent(0)
# GMC-500 always times out at byte #11.
# solved: it is a bug in the firmware; data resulted in ">>>"
# update config
dprint("WriteConfig: Update Config")
setDebugIndent(1)
rec = updateConfig()
setDebugIndent(0)
setDebugIndent(0)
def updateConfig():
# 13. Reload/Update/Refresh Configuration
# Command: <CFGUPDATE>>
# Return: 0xAA
# Firmware supported: GMC-280, GMC-300 Re.2.20 or later
# in GMC-500Re 1.08 this command returns: b'0071\r\n\xaa'
# see http://www.gqelectronicsllc.com/forum/topic.asp?TOPIC_ID=4948 #40
dprint("updateConfig:")
setDebugIndent(1)
rec, error, errmessage = serialCOMM(b'<CFGUPDATE>>', 1, orig(__file__))
dprint("updateConfig: rec: ", rec)
setDebugIndent(0)
return rec, error, errmessage
def getSERIAL():
# Get serial number
# send <GETSERIAL>> and read 7 bytes
# returns the serial number in 7 bytes
# each nibble of 4 bit is a single hex digit of a 14 character serial number
# e.g.: F488007E051234
#
# This routine returns the serial number as a 14 character ASCII string
dprint("getSERIAL:")
setDebugIndent(1)
rec, error, errmessage = serialCOMM(b'<GETSERIAL>>', 7, orig(__file__))
dprint("getSERIAL: raw: rec: ", rec)
if error == 0 or error == 1: # Ok or Warning
hexlookup = "0123456789ABCDEF"
sn =""
for i in range(0,7):
n1 = ((rec[i] & 0xF0) >>4)
n2 = ((rec[i] & 0x0F))
sn += hexlookup[n1] + hexlookup[n2]
rec = sn
dprint("getSERIAL: decoded: ", rec)
setDebugIndent(0)
return (rec, error, errmessage)
def setDATETIME():
# from GQ-RFC1201.txt:
# NOT CORRECT !!!!
# 22. Set year date and time
# command: <SETDATETIME[YYMMDDHHMMSS]>>
# Return: 0xAA
# Firmware supported: GMC-280, GMC-300 Re.3.00 or later
# from: GQ-GMC-ICD.odt
# CORRECT! 6 bytes, no square brackets
# <SETDATETIME[YY][MM][DD][hh][mm][ss]>>
dprint("setDATETIME:")
setDebugIndent(1)
tl = list(time.localtime()) # tl: [2018, 4, 19, 13, 2, 50, 3, 109, 1]
# for: 2018-04-19 13:02:50
tl0 = tl.copy()
tl[0] -= 2000
tlstr = b''
for i in range(0,6): tlstr += bytes([tl[i]])
dprint("setDATETIME: now:", tl0[:6], ", coded:", tlstr)
rec, error, errmessage = serialCOMM(b'<SETDATETIME'+ tlstr + b'>>', 1, orig(__file__))
setDebugIndent(0)
return (rec, error, errmessage)
def getDATETIME():
# Get year date and time
# send <GETDATETIME>> and read 7 bytes
# returns 7 bytes data: YY MM DD HH MM SS 0xAA
#
# This routine returns date and time in the format:
# YYYY-MM-DD HH:MM:SS
# e.g.: 2017-12-31 14:03:19
dprint("getDATETIME:")
setDebugIndent(1)
# yields: rec: b'\x12\x04\x13\x0c9;\xaa' , len:7