-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOmstNET.py
4570 lines (3816 loc) · 171 KB
/
OmstNET.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/env python3.4 -i
##MIT License
##
##Copyright (c) 2018 Douglas E. Moore
##
##Permission is hereby granted, free of charge, to any person obtaining a copy
##of this software and associated documentation files (the "Software"), to deal
##in the Software without restriction, including without limitation the rights
##to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
##copies of the Software, and to permit persons to whom the Software is
##furnished to do so, subject to the following conditions:
##
##The above copyright notice and this permission notice shall be included in all
##copies or substantial portions of the Software.
##
##THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
##IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
##FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
##AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
##LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
##OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
##SOFTWARE.
import os # for isatty?
import re # needed for parsing optomux ASCII packets
from time import perf_counter # needed for computing turnaround timeouts
from time import sleep # delays (and perhaps a form of kernel yield?)
from itertools import chain # needed for combining ranges in arg verification
import OmstTTY as tty # communications via serial port
import OmstUTL as utl # logging and TBD
import datetime # for timedelta
from collections import namedtuple # used to return mistic data
from collections import OrderedDict # used to build mistic commands and parse responses
import inspect # used to parse mistic responses
##import binascii as ba # hexlify
# init the utils such as logging
utl._init()
MsgFmt = namedtuple('MsgFmt',['cmd','rsp'])
CmdFmt = namedtuple('CmdFmt',['cmd','args'])
RspMsg = namedtuple('RspMsg',['ack','data'])
PktTyp = namedtuple('PktTyp',['jumpers','options'])
class OmstNET:
binary_mode = False
crc_mode = False
"""
'MISTIC PROTOCOL USER’S GUIDE, Form 270-100823 — August, 2010'
p2-5
"""
errors = {
1:('UNDEFINED COMMAND','The command character was not a legal command character.'),
2:('CHECKSUM OR CRC ERROR',
'The checksum or CRC received by the Mistic I/O unit did not match \
the value calculated from the command message.'),
3:('BUFFER O VERRUN ERROR',
'The receive buffer limit of 256 characters has been exceeded. \
The command was ignored.'),
4:('POWER-UP CLEAR ERROR',
'After a power failure (4.8 VDC or lower) or a reset command, \
you must issue a Power-Up Clear Command before issuing any \
other command. If you do not, you will receive this error code. \
This alerts the host that a power failure has occurred.'),
5:('DATA FIELD ERROR','Not enough characters received.'),
6:('COMMUNICATIONS LINK WATCHDOG TIME-OUT ERROR',
'The communications link watchdog timer has timed out and the \
specified actions have been taken. The command that returns \
this error is not executed but it does clear the error.'),
7:('SPECIFIED DATA INVALID ERROR',
'One or more data fields contains an illegal value.'),
8:('BUSY ERROR',
'This error is used by the LC Communicator to indicate a setup \
mode condition.'),
9:('INVALID MODULE TYPE ERROR',
'This error code is returned when one or more specified modules \
is not of the type required by the command.'),
10:('INVALID EVENT ENTRY ERROR',
'This error code is returned when an attempt is made to enable \
an event interrupt on a null entry in the event reaction table \
or an illegal reaction command is specified.'),
11:('HIGH RESOLUTION TIME DELAY LIMIT REACHED - DIGITAL ONLY',
'This error code is returned when an attempt is made to start a \
square wave, generate N pulses or time-proportional output with a \
delay time value less than 10 milliseconds on more than eight output positions.'),
}
"""
analog event datatypes.
"""
analog_event_data_types = {
0x00:'Current Counts',
0x01:'Average Counts',
0x02:'Peak Counts',
0x03:'Lowest Counts',
0x04:'Totalized Counts',
0x10:'Current Engineering Units',
0x11:'Average Engineering Units',
0x12:'Peak Engineering Units',
0x13:'Lowest Engineering Units',
0x14:'Totalized Engineering Units',
}
"""
'MISTIC PROTOCOL USER’S GUIDE, Form 270-100823 — August, 2010'
This dictionary is built from information in Chapters 4 - 16.
Chapter 4 has tables of commands by category
Chapters 5 - 16 describe how each command is built, and what
the device response contains
Each of these commands has a corresponding function with a similar name.
As an example, for the dictionary entry:
'READ EVENT TABLE ENTRY':
MsgFmt('O EE','CB CC RC RCC ED RES NNNNNNNN TTTTTTTT'),
there is a corresponding function:
def read_event_entry_enable_disable_status(self,aa,EE)
"""
commands = {
# Chapter 5: Digital Setup / System Commands
# Command Name, Command Format, Version
'IDENTIFY TYPE':MsgFmt('F', 'DDDD'),
'POWER UP CLEAR':MsgFmt('A',None),
'REPEAT LAST RESPONSE':MsgFmt('^',None),
'RESET':MsgFmt('B',None),
'RESET ALL PARAMETERS TO DEFAULT':MsgFmt('x',None),
'SET RESPONSE DELAY':MsgFmt('~ DD',None),
'SET SYSTEM OPTIONS':MsgFmt('C SS CC','DDDD'),
'SET WATCHDOG MOMO AND DELAY':MsgFmt('D MMMM NNNN TTTT',None),
# Chapter 6 = MsgFmt Digital I/O Configuration Commands
# Command Name, Command Format, Version
'READ MODULE CONFIGURATION':MsgFmt('Y','TT'),
'SET CHANNEL CONFIGURATION':MsgFmt('a CC TT',None),
'SET I/O CONFIGURATION-GROUP':MsgFmt('G MMMM TT',None),
'STORE SYSTEM CONFIGURATION':MsgFmt('E',None),
# Chapter 7 = MsgFmt Digital Read/Write, Latch Commands
# Command Name, Command Format, Version
'CLEAR OUTPUT (DEACTIVATE OUTPUT)':MsgFmt('e CC',None),
'READ AND OPTIONALLY CLEAR LATCHES GROUP':MsgFmt('S FF','PPPP NNNN'),
'READ AND OPTIONALLY CLEAR LATCH':MsgFmt('w CC FF','DD'),
'READ MODULE STATUS':MsgFmt('R','DDDD'),
'SET OUTPUT MODULE STATE-GROUP':MsgFmt('J MMMM NNNN',None),
'SET OUTPUT (ACTIVATE OUTPUT)':MsgFmt('d CC',None),
# Chapter 8 = MsgFmt Digital Counter, Frequency Commands
# Command Name, Command Format, Version
'READ 32 BIT COUNTER':MsgFmt('l CC','DDDDDDDD'),
'READ AND CLEAR 16 BIT COUNTER':MsgFmt('o CC','DDDD'),
'CLEAR COUNTER':MsgFmt('c CC',None),
'ENABLE/DISABLE COUNTER GROUP':MsgFmt('H MMMM SS',None),
'ENABLE/DISABLE COUNTER':MsgFmt('b CC SS',None),
'READ 16 BIT COUNTER':MsgFmt('m CC','DDDD'),
'READ 32 BIT COUNTER GROUP':MsgFmt('T MMMM','DDDDDDDD'),
'READ AND CLEAR 32 BIT COUNTER GROUP':MsgFmt('U MMMM','DDDDDDDD'),
'READ AND CLEAR 32 BIT COUNTER':MsgFmt('n CC','DDDDDDDD'),
'READ COUNTER ENABLE/DISABLE STATUS':MsgFmt('u','DDDD'),
'READ FREQUENCY MEASUREMENT':MsgFmt('t CC','DDDD'),
'READ FREQUENCY MEASUREMENT GROUP':MsgFmt('Z MMMM','DDDD'),
# Chapter 9 = MsgFmt Digital Time Delay/Pulse Output Commands
# Command Name, Command Format, Version
'SET TIME PROPORTIONAL OUTPUT PERIOD':MsgFmt('] CC TTTTTTTT',None),
'SET TPO PERCENTAGE':MsgFmt('j CC PPPPPPPP',None),
'START OFF PULSE':MsgFmt('g CC TTTTTTTT',None),
'START ON PULSE':MsgFmt('f CC TTTTTTTT',None),
'GENERATE N PULSES':MsgFmt('i CC NNNNNNNN FFFFFFFF XXXXXXXX',None),
'READ OUTPUT TIMER COUNTER':MsgFmt('k CC','TTTTTTTT'),
'START CONTINUOUS SQUARE WAVE':MsgFmt('h CC NNNNNNNN FFFFFFFF',None),
# Chapter 10: Digital Pulse/Period Measurement Commands
# Command Name, Command Format, Version
'READ 32 BIT PULSE/PERIOD MEASUREMENT':MsgFmt('p CC','DDDDDDDD'),
'READ AND RESTART 16 BIT PULSE/PERIOD':MsgFmt('s CC','DDDD'),
'READ AND RESTART 32 BIT PULSE/PERIOD':MsgFmt('r CC','DDDDDDDD'),
'READ 16 BIT PULSE/PERIOD MEASUREMENT':MsgFmt('q CC','DDDD'),
'READ 32 BIT PULSE/PERIOD GROUP':MsgFmt('W MMMM','DDDDDDDD'),
'READ AND RESTART 32 BIT PULSE/PERIOD GROUP':MsgFmt('X MMMM','DDDDDDDD'),
'READ PULSE/PERIOD COMPLETE STATUS':MsgFmt('V','DDDD'),
# Chapter 11: Digital Event/Reaction Commands
# Command Name, Command Format, Version
'ENABLE/DISABLE EVENT ENTRY GROUP':MsgFmt('{ GG MMMM NNNN',None),
'ENABLE/DISABLE EVENT TABLE ENTRY':MsgFmt('N EE SS',None),
'READ AND CLEAR EVENT LATCHES':MsgFmt('Q EE','DDDD'),
'READ EVENT DATA HOLDING BUFFER':MsgFmt('I EE','DDDDDDDD'),
'READ EVENT ENTRY ENABLE/DISABLE STATUS':MsgFmt('v EE','DDDD'),
'READ EVENT LATCHES':MsgFmt('P EE','DDDD'),
'SET EVENT ON COUNTER/TIMER >=':MsgFmt('L EE CC NNNNNNNN',None),
'SET EVENT ON COUNTER/TIMER <=':MsgFmt('} EE CC NNNNNNNN',None),
'CLEAR EVENT/REACTION TABLE':MsgFmt('_',None),
'CLEAR EVENT TABLE ENTRY':MsgFmt('\ EE',None),
'CLEAR INTERRUPT':MsgFmt('zB',None),
'READ AND OPTIONALLY CLEAR EVENT LATCH':MsgFmt('zA EE FF','DD'),
'READ EVENT TABLE ENTRY':MsgFmt('O EE','CB CC RC RCC ED RES NNNNNNNN TTTTTTTT'),
'SET EVENT INTERRUPT STATUS':MsgFmt('I EE SS',None),
'SET EVENT ON COMM LINK WATCHDOG TIMEOUT':MsgFmt('y EE',None),
'SET EVENT ON MOMO MATCH':MsgFmt('K EE MMMM NNNN',None),
'SET EVENT REACTION COMMAND':MsgFmt('M EE',None),
# Chapter 12: Analog Setup/System Commands
# Command Name, Command Format, Version
'SET COMM LINK WATCHDOG AND DELAY':MsgFmt('D TTTT',None),
'SET COMM LINK WATCHDOG TIMEOUT DATA':MsgFmt('H MMMM DDDDDDDD',None),
# Chapter 13: Analog I/O Configuration Commands
# Command Name, Command Format, Version
'CALCULATE AND SET ADC MODULE OFFSET':MsgFmt('d CC','OOOO'),
'CALCULATE AND SET ADC MODULE GAIN':MsgFmt('e CC','GGGG'),
'SET ADC MODULE OFFSET':MsgFmt('b CC OOOO',None),
'SET ADC MODULE GAIN':MsgFmt('c CC GGGG',None),
'SET AVERAGING SAMPLE WEIGHT (DIG. FILTERING)':MsgFmt('h CC DDDD',None),
'SET TOTALIZATION SAMPLE RATE':MsgFmt('g CC DDDD',None),
'SET ENGINEERING UNIT SCALING PARAMETERS':MsgFmt('f CC HHHHHHHH LLLLLLLL',None),
'SET TPO RESOLUTION':MsgFmt('] CC SS',None),
# Chapter 14: Analog Read/Write/Output Commands
# Command Name, Command Format, Version
'RAMP DAC OUTPUT TO ENDPOINT':MsgFmt('Z CC EEEEEEEE SSSSSSSS',None),
'READ AND CLEAR I/O MODULE 16 BIT DATA':MsgFmt('s CC TT','DDDD'),
'READ AND CLEAR I/O MODULE 16 BIT DATA-GROUP':MsgFmt('S MMMM TT','DDDD'),
'READ AND CLEAR I/O MODULE 32 BIT DATA':MsgFmt('s CC TT','DDDDDDDD'),
'READ AND CLEAR I/O MODULE 32 BIT DATA-GROUP':MsgFmt('S MMMM TT','DDDDDDDD'),
'READ I/O MODULE 16 BIT MAGNITUDE':MsgFmt('r CC TT','DDDD'),
'READ I/O MODULE 16 BIT MAGNITUDE-GROUP':MsgFmt('R MMMM TT','DDDD'),
'READ I/O MODULE 32 BIT MAGNITUDE':MsgFmt('r CC TT','DDDDDDDD'),
'READ I/O MODULE 32 BIT MAGNITUDE-GROUP':MsgFmt('R MMMM TT','DDDDDDDD'),
'SET DAC MODULE MAGNITUDE, ENG. UNITS':MsgFmt('w CC DDDDDDDD',None),
'SET DAC MODULE MAGNITUDE, ENG. UNITS-GRP.':MsgFmt('W MMMM DDDDDDDD',None),
'SET DAC MODULE MAGNITUDE, COUNTS':MsgFmt('x CC DDDD',None),
'SET DAC MODULE MAGNITUDE, COUNTS-GROUP':MsgFmt('X MMMM DDDD',None),
# Chapter 15: Analog Event/Reaction Commands
# Command Name, Command Format, Version
# Note: ***************************************************************
# Setpoints may be 16 or 32 bits. The command format showed the data
# as DDDD[DDDD]. Since DDDD[DDDD] is not a valid Python variable
# name, and the length of the variable name is used as a field width
# indication, the commands 'SET EVENT ON I/O >= SETPOINT' and
# 'SET EVENT ON I/O <= SETPOINT are expanded to two versions for each,
# a 16 bit (COUNTS) and a 32 bit (ENG. UNITS) version
# *********************************************************************
'SET EVENT ON I/O >= SETPOINT (COUNTS)':MsgFmt('K EE CC TT DDDD',None),
'SET EVENT ON I/O >= SETPOINT (ENG. UNITS)':MsgFmt('K EE CC TT DDDDDDDD',None),
'SET EVENT ON I/O <= SETPOINT (COUNTS)':MsgFmt('L EE CC TT DDDD',None),
'SET EVENT ON I/O <= SETPOINT (ENG. UNITS)':MsgFmt('L EE CC TT DDDDDDDD',None),
# Note: ***************************************************************
# 'SET EVENT REACTION COMMAND':MsgFmt('M EE [REACTION COMMAND]',None)
#
# The 'SET EVENT REACTION COMMAND' takes a parameter [REACTION COMMAND]
# which is specific to each reaction type. It seemed like the best way
# handle this was to split the 'SET EVENT REACTION COMMAND' into
# separated functions, pasing the [REACTION COMMAND] parameters as if
# they were command arguments.
#
# A [REACTION COMMAND] is essentially a way for Mistic to call certain
# digital or analog functions when a specific event happens, thus
# minimizing the need for the host to poll serially before sending a
# command to take some action.
# *********************************************************************
# REACTION COMMANDS (Configuration M)
'ON EVENT NULL REACTION (DO NOTHING)':MsgFmt('M EE RC',None),
'ON EVENT ENABLE/DISABLE EVENT TABLE ENTRY':MsgFmt('M EE RC ee SS',None),
'ON EVENT ENABLE/DISABLE EVENT ENTRY GROUP':MsgFmt('M EE RC GG MMMM NNNN',None),
# REACTION COMMANDS (To be used with analog command M)
'ON EVENT SET OUTPUT MODULE STATE-GROUP':MsgFmt('M EE RC MMMM NNNN',None),
'ON EVENT START ON PULSE':MsgFmt('M EE RC CC TTTTTTTT',None),
'ON EVENT START OFF PULSE':MsgFmt('M EE RC CC TTTTTTTT',None),
'ON EVENT ENABLE/DISABLE COUNTER':MsgFmt('M EE RC CC SS',None),
'ON EVENT CLEAR COUNTER':MsgFmt('M EE RC CC',None),
'ON EVENT READ AND HOLD COUNTER VALUE':MsgFmt('M EE RC CC',None),
# REACTION COMMANDS (To be used with analog command M)
'ON EVENT SET DAC MODULE MAGNITUDE, COUNTS':MsgFmt('M EE RC CC DDDD',None),
'ON EVENT SET DAC MODULE MAGNITUDE, ENG. UNITS':MsgFmt('M EE RC CC DDDDDDDD',None),
'ON EVENT RAMP DAC OUTPUT TO ENDPOINT':MsgFmt('M EE RC CC EEEEEEEE SSSSSSSS',None),
'ON EVENT ENABLE/DISABLE PID LOOP':MsgFmt('M EE RC LL SS',None),
'ON EVENT SET PID LOOP SETPOINT':MsgFmt('M EE RC LL SSSSSSSS',None),
'ON EVENT READ AND HOLD I/O DATA':MsgFmt('M EE RC CC TT',None),
'ON EVENT SET PID LOOP MIN-MAX OUTPUT LIMITS':MsgFmt('M EE RC LL HHHHHHHH LLLLLLLL',None),
# Chapter 16: PID Loop Commands
# Command Name, Command Format, Version
'SET PID LOOP CONTROL OPTIONS':MsgFmt('j CC SSSS CCCC',None),
'SET PID LOOP DERIVATIVE RATE':MsgFmt('n LL DDDDDDDD',None),
'SET PID LOOP GAIN':MsgFmt('l LL GGGGGGGG',None),
'SET PID LOOP PROCESS VARIABLE':MsgFmt('q LL SSSSSSSS',None),
'SET PID LOOP SETPOINT':MsgFmt('k LL SSSSSSSS',None),
'INITIALIZE PID LOOP':MsgFmt('i LL II SS OO TTTT',None),
'READ ALL PID LOOP PARAMETERS':MsgFmt('T LL',\
'CCCC RRRR II SS ZZ OO PPPPPPPP SSSSSSSS GGGGGGGG IIIIIIII DDDDDDDD HHHHHHHH ' + \
'LLLLLLLL ZZZZ YYYY OOOOOOOO AAAAAAAA BBBBBBBB QQQQQQQQ FFFFFFFF KKKKKKKK'),
## 'READ PID LOOP PARAMETER':MsgFmt('t LL PP','DDDD'),
# break down into sub commands because the response data field varies
'READ PID LOOP CONTROL WORD':MsgFmt('t LL PP','DDDD'),
'READ PID LOOP RATE WORD':MsgFmt('t LL PP','DDDD'),
'READ PID LOOP OUTPUT COUNTS':MsgFmt('t LL PP','DDDDDDDD'),
'READ PID LOOP INPUT, SETPOINT, OUTPUT CHANNELS':MsgFmt('t LL PP','II SS ZZ OO'),
'READ PID LOOP INPUT VALUE':MsgFmt('t LL PP','DDDDDDDD'),
'READ PID LOOP SETPOINT VALUE':MsgFmt('t LL PP','DDDDDDDD'),
'READ PID LOOP OUTPUT VALUE':MsgFmt('t LL PP','DDDDDDDD'),
'READ PID LOOP GAIN TERM':MsgFmt('t LL PP','DDDDDDDD'),
'READ PID LOOP INTEGRAL TERM':MsgFmt('t LL PP','DDDDDDDD'),
'READ PID LOOP DERIVATIVE TERM':MsgFmt('t LL PP','DDDDDDDD'),
'READ PID LOOP MAXIMUM SETPOINT LIMIT':MsgFmt('t LL PP','DDDDDDDD'),
'READ PID LOOP MINIMUM SETPOINT LIMIT':MsgFmt('t LL PP','DDDDDDDD'),
'READ PID LOOP OUTPUT MAXIMUM LIMIT':MsgFmt('t LL PP','DDDDDDDD'),
'READ PID LOOP OUTPUT MINIMUM LIMIT':MsgFmt('t LL PP','DDDDDDDD'),
'READ PID LOOP OUTPUT MAXIMUM CHANGE PER SCAN':MsgFmt('t LL PP','DDDDDDDD'),
'READ PID LOOP OUTPUT COUNTS':MsgFmt('t LL PP','DDDDDDDD'),
'READ PID LOOP FULL SCALE IN ENG. UNITS':MsgFmt('t LL PP','DDDDDDDD'),
'READ PID LOOP ZERO SCALE IN ENG. UNITS':MsgFmt('t LL PP','DDDDDDDD'),
'SET PID LOOP INTEGRAL RESET RATE':MsgFmt('m LL IIIIIIII',None),
'SET PID LOOP MAXIMUM RATE OF CHANGE':MsgFmt('u LL RRRRRRRR',None),
'SET PID LOOP MIN-MAX OUTPUT LIMITS':MsgFmt('p LL HHHHHHHH LLLLLLLL',None),
'SET PID LOOP MIN-MAX SETPOINT LIMITS':MsgFmt('o LL HHHHHHHH LLLLLLLL',None)
}
## Bit 0 = Frequency resolution setting: 0 = 1 Hz, 1 = 10 Hz.
## Bit 1 = Not used.
## Bit 2 = Not used.
## Bit 3 = Not used.
## Bit 4 = CRC initialization value: 0 = 0000, 1 = FFFF.
## Bit 5 = CRC method select: 0 = reverse, 1 = classical.
## Bit 6 = CRC polynomial select: 0 = CRC16, 1 = CCITT.
## Bit 7 = Global event interrupt enable: 0 = disabled, 1 = enabled.
def __init__(self):
"""
"""
self.tty = tty.OmstTTY()
# array of bytes, resolution is 10ms
self.response_delay = {}
# save a copy of the 'SET SYSTEM OPTIONS" byte
self.brain_syst_options = {}
# not sure if this is needed but it might be since
# there is a repeat last response and we don't want
# to lose sight of what command was sent
self.last_command = {}
if self.crc16r == None:
self.crc16r = self.generate_crc_table(0xa001)
@utl.logger
def get_response_timeout(self,aa):
"""
Compute a response timeout so read won't hang forever.
It is based on the time at the current baudrate of
sending the longest command and receiving the longest
response, plus the 'Set Turnaround Delay' time plus
a 10ms buffer starting at the current perf_counter
reading.
if the tty is invalid, return -1
"""
if self.tty.fd and os.isatty(self.tty.fd):
if aa not in self.response_delay:
self.response_delay[aa] = 0
# max ASCII response packet would likely be:
# 'A'+ (16 32 bit analog values) + (16 bit crc) + \r
max_opto_msg = 1 + 16 * 8 + 4 + 1
# time required to send two max sized messages
# 2 * secs/char * len(maxmsg)
# + turnaround delay setting
# + current perf_counter
# + 10 ms fudge factor
to = 2 * 10/self.tty.baud * max_opto_msg \
+ 0.010 * self.response_delay[aa] \
+ perf_counter() \
+ 0.010
# some extra time for twiddling termios in binary mode
if self.binary_mode:
to += 0.005 * max_opto_msg//2
return to
return -1
@utl.logger
def verify_dvf(self,aa,data):
"""
"""
if self.binary_mode:
if self.crc_mode:
exp = int.from_bytes(data[-2:],byteorder='big')
act = self.crc(aa,data[0:-2])
return(act == exp)
else:
exp = int.from_bytes(data[-1:],byteorder='big')
act = self.chksum(aa,data[0:-1])
return(act == exp)
else:
if self.crc_mode:
act = int(data[-5:-1],16)
exp = self.crc(aa,data[:-5])
return(act == exp)
else:
act = int(data[-3:-1],16)
exp = self.chksum(aa,data[:-3])
return(act == exp)
return False
@utl.logger
def mistic_data_to_int16(self,v):
"""
Mistic returns module data in 2's complement
"""
if v & 0x8000:
return 0x10000 - v
return v
@utl.logger
def mistic_data_to_int32(self,v):
"""
Mistic returns module data in 2's complement
"""
if v & 0x80000000:
return (0x100000000 - v)
return v
@utl.logger
def mistic_data_to_engineering_units(self,v):
if v & 0x80000000:
v = (0x100000000 - v)
return v / 65536
@utl.logger
def parse_mistic_response_data(self,names,rsp):
"""
Analog data tends to come over the wire as twos complement
"""
ntflds = namedtuple('ntflds',list(rsp.data._fields))
ntdats = []
for field in rsp.data._fields:
v = getattr(rsp.data,field)
if isinstance(v,int):
if field in names:
if len(field) == 4:
ntdats += [self.mistic_data_to_int16(v)]
elif len(field) == 8:
ntdats += [self.mistic_data_to_engineering_units(v)]
else:
ntdats += [v]
elif isinstance(v,tuple):
ll = []
for e in v:
if field in names:
if len(field) == 4:
ll += [self.mistic_data_to_int16(e)]
elif len(field) == 8:
ll += [self.mistic_data_to_engineering_units(e)]
else:
ll += [v]
ntdats += [tuple(ll)]
return RspMsg(rsp.ack,ntflds(*ntdats))
@utl.logger
def int16_to_mistic_data(self,v):
"""
Mistic wants module data in 2's complement
"""
if v < 0:
v += 0x10000
return v
@utl.logger
def int32_to_mistic_data(self,v):
if v < 0:
v += 0x100000000
return v
@utl.logger
def engineering_units_to_mistic_data(self,v):
"""
Mistic wants module data in 2's complement
"""
v = int(v*65536)
if v < 0:
v += 0x100000000
return v
"""
responses to the identify type command
Note: this one is a bit confusing.
The B3000 returns A003004\r which I presume
is hex, but the manual lists the expected
value as 0048 which is heximal or deximal?
"""
digital_brainboards = {
0x0a:'Remote 16-Point Digital Multifunction I/O Unit',
0x0b:'G4D32RS Digital Remote Simple I/O Unit',
0x14:'Local 16-Point Digital Multifunction I/O Unit',
0x15:'Local 16-Point Digital Non-multifunction I/O Unit',
0x30:'B3000 (digital address) Multifunction I/O Unit',
}
analog_brainboards = {
0x0c:'Remote 16-Point Analog I/O Unit',
0x0d:'Remote 8-Point Analog I/O Unit',
0x16:'Local 16-Point Analog I/O Unit',
0x17:'Local 8-Point Analog I/O Unit',
0x32:'B3000 (analog address) Multifunction I/O Unit',
}
@utl.logger
def identify_type(self,aa):
"""
Sends an 'IDENTIFY TYPE'
MsgFmt(cmd='F', rsp='DDDD')
Data out:
None
Data in:
DDDD is a 16 bit value from the following table:
10 = Remote 16-Point Digital Multifunction I/O Unit
11 = G4D32RS Digital Remote Simple I/O Unit
12 = Remote 16-Point Analog I/O Unit
13 = Remote 8-Point Analog I/O Unit
20 = Local 16-Point Digital Multifunction I/O Unit
21 = Local 16-Point Digital Non-multifunction I/O Unit
22 = Local 16-Point Analog I/O Unit
23 = Local 8-Point Analog I/O Unit
48 = B3000 (digital address) Multifunction I/O Unit
50 = B3000 (analog address) Multifunction I/O Unit
"""
return self.send_receive_2(aa,'IDENTIFY TYPE',inspect.currentframe())
@utl.logger
def what_am_i(self,aa):
"""
what type of brain or controller am I in text?
"""
rsp = self.identify_type(aa)
if rsp.ack == 'A':
ntflds = namedtuple('ntflds',['DDDD','BrainBoardType'])
devices = {}
devices.update(self.digital_brainboards)
devices.update(self.analog_brainboards)
bbt =(rsp.data.DDDD,devices[rsp.data.DDDD[0]])
return RspMsg(rsp.ack,ntflds(*bbt))
return rsp
@utl.logger
def power_up_clear(self,aa):
"""
Sends a 'POWER UP CLEAR'
MsgFmt(cmd='A', rsp=None)
Data out:
None
Data In:
None
Prevents the I/O unit from returning a Power-Up Clear Expected
error message in response to instructions following application
of power or the Reset command.
"""
return self.send_receive_2(aa,'POWER UP CLEAR',inspect.currentframe())
@utl.logger
def repeat_last_response(self,aa):
"""
"""
return self.send_receive_2(aa,'REPEAT LAST RESPONSE',inspect.currentframe())
@utl.logger
def reset(self,aa):
"""
Send a 'REPEAT LAST RESPONSE'
MsgFmt(cmd='^', rsp=None)
Data out:
None
Data In:
None
This command causes the addressed unit to repeat the response to the
previous command.
"""
return self.send_receive_2(aa,'RESET',inspect.currentframe())
@utl.logger
def reset_all_parameters_to_default(self,aa):
"""
Send a 'RESET ALL PARAMETERS TO DEFAULT'
MsgFmt(cmd='x', rsp=None)
Data out:
None
Data In:
None
This command will cause all modules and event parameters to be reset to
factory default conditions.
"""
return self.send_receive_2(aa,'RESET ALL PARAMETERS TO DEFAULT',inspect.currentframe())
@utl.logger
def set_response_delay(self,aa,DD=0):
"""
Send a 'SET RESPONSE DELAY'
MsgFmt(cmd='~ DD', rsp=None)
Data out:
DD - delay in 10 ms increments
Data In:
None
REMARKS :
After acknowledging the “Set Response Delay” command, for all
subsequent commands, the I/O unit will wait DD x 10 milliseconds
before responding. Note that any particular command is executed
immediately and that only the acknowledge or data response is delayed.
The default response delay setting is zero unless a different value
has been set and saved in EEPROM with a “Store System Configuration”
command.
"""
rsp = self.send_receive_2(aa,'SET RESPONSE DELAY',inspect.currentframe())
if rsp.ack == 'A':
self.response_delay[aa] = DD
return rsp
"""
bits in the system options byte
"""
system_options = {
'Frequency resolution setting':(0,0), # 0 = 1 Hz, 1 = 10 Hz.
'CRC initialization value':(4,0), # 0 = 0000, 1 = FFFF.
'CRC method select':(5,0), # 0 = reverse, 1 = classical.
'CRC polynomial select':(6,0), # 0 = CRC16, 1 = CCITT.
'Global event interrupt enable':(7,0) # 0 = disabled, 1 = enabled.
}
@utl.logger
def set_frequency_resolution_to_10_hz(self,aa,SS=1,CC=0):
rsp = self.send_receive_2(aa,'SET SYSTEM OPTIONS',inspect.currentframe())
if rsp.ack == 'A':
self.brain_syst_options[aa] = rsp.data[0]
return rsp
@utl.logger
def set_frequency_resolution_to_1_hz(self,aa,SS=0,CC=1):
if rsp.ack == 'A':
self.brain_syst_options[aa] = rsp.data[0]
return rsp
@utl.logger
def get_frequency_resolution(self,aa,SS=0,CC=0):
rsp = self.send_receive_2(aa,'SET SYSTEM OPTIONS',inspect.currentframe())
if rsp.ack == 'A':
self.brain_syst_options[aa] = rsp.data[0]
return rsp
@utl.logger
def enable_global_interrupt(self,aa,SS=128,CC=0):
rsp = self.send_receive_2(aa,'SET SYSTEM OPTIONS',inspect.currentframe())
if rsp.ack == 'A':
self.brain_syst_options[aa] = rsp.data[0]
return rsp
@utl.logger
def disable_global_interrupt(self,aa,SS=0,CC=128):
rsp = self.send_receive_2(aa,'SET SYSTEM OPTIONS',inspect.currentframe())
if rsp.ack == 'A':
self.brain_syst_options[aa] = rsp.data[0]
return rsp
@utl.logger
def get_global_interrupt_enable(self,aa,SS=0,CC=0):
rsp = self.send_receive_2(aa,'SET SYSTEM OPTIONS',inspect.currentframe())
if rsp.ack == 'A':
self.brain_syst_options[aa] = rsp.data[0]
return rsp
@utl.logger
def set_system_options(self,aa,SS=0,CC=0):
"""
Sends a 'SET SYSTEM OPTIONS'
MsgFmt(cmd='C SS CC', rsp='DDDD')
Data out:
SS - bits to be set
CC - bits to be cleared
Data In:
DDDD -
This command is used to set (SS) or clear (CC) the bits in the Option
Control Byte. The Option Control Byte is used to select certain system
options. The system options and the controlling bits are as follows:
Bit 0 = Frequency resolution setting: 0 = 1 Hz, 1 = 10 Hz.
Bit 1 = Not used.
Bit 2 = Not used.
Bit 3 = Not used.
Bit 4 = CRC initialization value: 0 = 0000, 1 = FFFF.
Bit 5 = CRC method select: 0 = reverse, 1 = classical.
Bit 6 = CRC polynomial select: 0 = CRC16, 1 = CCITT.
Bit 7 = Global event interrupt enable: 0 = disabled, 1 = enabled.
The factory default is 00. This byte is stored in EEPROM when command E
is executed and is restored upon power-up or when the RESET command (B)
is executed.
You can use this command to set (SS), clear (CC) or do nothing to the
bits in the Option Control Byte.
You can use the Store System Configuration (Command E) to save the Option
Control Byte in EEPROM. The Option Control Byte is always restored from
EEPROM upon power-up or when the RESET (Command B) is executed.
The response from this command is a 16-bit word. The current value of the
Option Control Byte is returned as the least significant byte of the 16-bit
word. The most significant byte can be ignored. It is for future expansion.
"""
return self.send_receive_2(aa,'SET SYSTEM OPTIONS',inspect.currentframe())
@utl.logger
def set_comm_link_watchdog_momo_and_delay(self,aa,MMMM,NNNN,TTTT):
"""
Send a 'SET WATCHDOG MOMO AND DELAY'
MsgFmt(cmd='D MMMM NNNN TTTT', rsp=None)
Data out:
MMMM - digital outputs to be turned on
NNNN - digital outputs to be turned off
TTTT - delay in 10ms ticks
Data In:
None
This command sets the communications line watchdog parameters for the
addressed I/O unit. When enabled, if a command (or > character for ASCII
protocol) is not received after the specified delay, output modules may
be instructed to turn on, turn off or do nothing. Anytime delays on
specified output channels are canceled upon watchdog timeout. After
the specified modules are rturned on or turned off, a complete scan of the
event/reaction table occurs, starting from 0. A delay of zero (0) disables
the watchdog function.
The minimum delay for the watchdog timer is 200 milliseconds
(TTTT = 0014 Hex). If the watchdog timer for a particular I/O unit times
out, the next instruction sent to the I/O unit will not be executed.
Instead an error code will be sent back to the host computer. This error
code is sent as a warning to let the host know that a timeout occurred.
Subsequent commands will be executed in a normal manner, provided the
time interval between commands is shorter than the watchdog timer delay
time.
On power up this parameter is restored from EEPROM memory.
"""
return self.send_receive_2(aa,'SET WATCHDOG MOMO AND DELAY',inspect.currentframe())
@utl.logger
def cancel_comm_link_watchdog_momo_and_delay(self,aa):
"""
Almost impossible to recover from a short WD timer while typing at terminal,
so this should do it
"""
rsp = self.power_up_clear(aa)
if rsp.ack == 'A':
return self.set_comm_link_watchdog_momo_and_delay(aa,0,0,0)
return rsp
"""
Commands that set or get the digital module type at
one or more locations need these values
"""
digital_module_types = {
0x00:'Counter Input',
0x01:'Positive Pulse Measurement Input',
0x02:'Negative Pulse Measurement Input',
0x03:'Period Measurement Input',
0x04:'Frequency Measurement Input',
0x05:'Quadrature Counter Input',
0x06:'On Time Totalizer Input',
0x07:'Off Time Totalizer Input',
0x80:'Standard Output'
}
"""
Commands that set or get the analog module type at
one or more locations need these values
"""
analog_module_types = {
0x00:'Generic Input Module',
0x01:'Reserved',
0x02:'Reserved',
0x03:'G4AD3 4 to 20 mA',
0x04:'G4AD4 ICTD',
0x05:'G4AD5 Type J Thermocouple',
0x06:'G4AD6 0 to 5 VDC',
0x07:'G4AD7 0 to 10 VDC',
0x08:'G4AD8 Type K Thermocouple',
0x09:'G4AD9 0 to 50 mV',
0x0A:'G4AD10 100 Ohm RTD',
0x0B:'G4AD11 -5 to +5 VDC',
0x0C:'G4AD12 -10 to +10 VDC',
0x0D:'G4AD13 0 to 100 mV',
0x10:'G4AD16 0 to 5 Amperes',
0x11:'G4AD17 Type R Thermocouple',
0x12:'G4AD18 Type T Thermocouple',
0x13:'G4AD19 Type E Thermocouple',
0x14:'G4AD20 0 to 4095 Hz.',
0x16:'G4AD22 0 to 1 VDC',
0x17:'G4AD17 Type S Thermocouple',
0x18:'G4AD24 Type B Thermocouple',
0x19:'G4AD25 0 to 100 VAC/VDC',
0x80:'Generic Output Module',
0x81:'Reserved',
0x82:'Reserved',
0x83:'G4DA3 4 to 20 mA',
0x84:'G4DA4 0 to 5 VDC',
0x85:'G4DA5 0 to 10 VDC',
0x86:'G4DA6 -5 to +5 VDC',
0x87:'G4DA7 -10 to +10 VDC',
0x88:'G4DA8 0 to 20 mA',
0x89:'G4DA9 Time Proportional Output',
}
@utl.logger
def read_module_configuration(self,aa):
"""
Send a 'READ MODULE CONFIGURATION'
MsgFmt(cmd='Y', rsp='DD')
Data out:
None
Data In:
DD - a tuple of module types
This command causes the addressed unit to send back a response to the
host that identifies the module configuration type for each of the 16
channels. Data is returned as 2 ASCII Hex digits for each of the 16
channels for ASCII protocol. For binary protocol, data is returned as
1 data byte for each channel.
For digital modules, the data is interpreted as follows:
00 Hex = Counter Input
01 Hex = Positive Pulse Measurement Input
02 Hex = Negative Pulse Measurement Input
03 Hex = Period Measurement Input
04 Hex = Frequency Measurement Input
05 Hex = Quadrature Counter Input
06 Hex = On Time Totalizer Input
07 Hex = Off Time Totalizer Input
80 Hex = Standard Output
For analog modules, the data is interpreted as follows:
00 = Generic Input Module
01 = Reserved
02 = Reserved
03 = G4AD3 4 to 20 mA
04 = G4AD4 ICTD
05 = G4AD5 Type J Thermocouple
06 = G4AD6 0 to 5 VDC
07 = G4AD7 0 to 10 VDC
08 = G4AD8 Type K Thermocouple
09 = G4AD9 0 to 50 mV
0A = G4AD10 100 Ohm RTD
0B = G4AD11 -5 to +5 VDC
0C = G4AD12 -10 to +10 VDC
0D = G4AD13 0 to 100 mV
10 = G4AD16 0 to 5 Amperes
11 = G4AD17 Type R Thermocouple
12 = G4AD18 Type T Thermocouple
13 = G4AD19 Type E Thermocouple
14 = G4AD20 0 to 4095 Hz.
16 = G4AD22 0 to 1 VDC
17 = G4AD17 Type S Thermocouple
18 = G4AD24 Type B Thermocouple
19 = G4AD25 0 to 100 VAC/VDC
80 = Generic Output Module
81 = Reserved
82 = Reserved
83 = G4DA3 4 to 20 mA
84 = G4DA4 0 to 5 VDC
85 = G4DA5 0 to 10 VDC
86 = G4DA6 -5 to +5 VDC
87 = G4DA7 -10 to +10 VDC
88 = G4DA8 0 to 20 mA
89 = G4DA9 Time Proportional Output
"""
return self.send_receive_2(aa,'READ MODULE CONFIGURATION',inspect.currentframe())
@utl.logger
def set_channel_configuration(self,aa,CC,TT):
"""
Send a 'SET CHANNEL CONFIGURATION'
MsgFmt(cmd='a CC TT', rsp=None)
Data out:
CC - channel index
TT - module type
Data In:
None
This command configures the module type on a singel channel.
For digital modules, the data is interpreted as follows:
00 Hex = Counter Input
01 Hex = Positive Pulse Measurement Input
02 Hex = Negative Pulse Measurement Input
03 Hex = Period Measurement Input
04 Hex = Frequency Measurement Input
05 Hex = Quadrature Counter Input
06 Hex = On Time Totalizer Input
07 Hex = Off Time Totalizer Input
80 Hex = Standard Output
For analog modules, the data is interpreted as follows:
00 = Generic Input Module
01 = Reserved
02 = Reserved
03 = G4AD3 4 to 20 mA
04 = G4AD4 ICTD
05 = G4AD5 Type J Thermocouple
06 = G4AD6 0 to 5 VDC
07 = G4AD7 0 to 10 VDC
08 = G4AD8 Type K Thermocouple
09 = G4AD9 0 to 50 mV
0A = G4AD10 100 Ohm RTD
0B = G4AD11 -5 to +5 VDC
0C = G4AD12 -10 to +10 VDC
0D = G4AD13 0 to 100 mV
10 = G4AD16 0 to 5 Amperes
11 = G4AD17 Type R Thermocouple
12 = G4AD18 Type T Thermocouple
13 = G4AD19 Type E Thermocouple
14 = G4AD20 0 to 4095 Hz.
16 = G4AD22 0 to 1 VDC
17 = G4AD17 Type S Thermocouple
18 = G4AD24 Type B Thermocouple
19 = G4AD25 0 to 100 VAC/VDC
80 = Generic Output Module
81 = Reserved
82 = Reserved
83 = G4DA3 4 to 20 mA
84 = G4DA4 0 to 5 VDC
85 = G4DA5 0 to 10 VDC
86 = G4DA6 -5 to +5 VDC
87 = G4DA7 -10 to +10 VDC
88 = G4DA8 0 to 20 mA
89 = G4DA9 Time Proportional Output
"""
return self.send_receive_2(aa,'SET CHANNEL CONFIGURATION',inspect.currentframe())
@utl.logger
def set_io_configuration_group(self,aa,MMMM,TT):
"""
Send a 'SET I/O CONFIGURATION-GROUP'
MsgFmt(cmd='G MMMM TT', rsp=None)
Data Out:
MMMM - bit mask of channels to be configured
TT - tuple of module types starting with lowest channel
Data In:
None
This command configures the modules specified by data field MMMM to
the Configuration Type specified by data field TT. For each bit in
data field MMMM that is set to a 1, there must be a corresponding
data field TT.
"""
return self.send_receive_2(aa,'SET I/O CONFIGURATION-GROUP',inspect.currentframe())
@utl.logger
def store_system_configuration(self,aa):
"""
Send a 'STORE SYSTEM CONFIGURATION'
MsgFmt(cmd='E', rsp=None)
Data Out:
None
Data In:
None
This command saves the current system parameters to EEPROM. The
parameters saved to EEPROM are:
(1) Module configuration.
(2) Counter enable/disable status.
(3) Communications link watchdog parameters.
(4) Option Control Byte.
(5) Response Delay Setting
(6) Event/Reaction table entries 00 thru 20 Hex.
This command requires one (1) second to execute. The command response
is sent after the command has finished executing.
"""
return self.send_receive_2(aa,'STORE SYSTEM CONFIGURATION',inspect.currentframe())