-
Notifications
You must be signed in to change notification settings - Fork 1
/
chamber_communication.py
executable file
·769 lines (652 loc) · 25.4 KB
/
chamber_communication.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Module for talking to the ESZ Cincinnati Sub-Zero (EZT-570i) chamber over Modbus
The chamber speaks ModBus protocol over RS323 and RS485
Over RS232 can control 1 chamber,
Over RS485 can control 31 chambers
Over ethernet, use ethernet-to-serial adapter (by gridconnect.com GC-BF-430)
The chamber rs232 requires specific serial port settings:
baud:9600
data bits: 8
stop bits: 1
flow control: none
parity: Even
The Touch Screen on the front of the chamber is connected to a Windows CE computer.
The Windows CE computer saves the Profiles to compact flash.
The Windows CE computer does not offer a network share to access the files.
One must manually eject the CF and read the files.
By John Stile At Meyer Sound Laboratories Inc.
this is distributed under a MIT license, see LICENSE
"""
import binascii # for human readable access to bit packed data
import ctypes # to access binary packed data in calibration file
import struct # for crc and message packing
import socket # for tcp communication
import itertools
import serial # for rs232 communication
import logging # for log facility
import time
import retrying
import modbus_packets
import chamber_commands
def int_or_float(s):
"""
Modbus can't handle decimals, so we multiply any float by 10.
:param s: string representation of a numeric value.
:return: int
"""
try:
return int(s)
except ValueError:
# if we have a float,
# protocol requires multiply by 10 and cast to int
return int(float(s) * 10)
def retry_if_crc_error(exception):
"""Return True if we should retry (in this case when it's a CRC error), False otherwise"""
return isinstance(exception, CRCError)
class ChamberCommunication(object):
"""Communication with EZT570i"""
def __init__(self, comm_params=None, log=None, chamber_number=1):
self.log = log
self.comm = None
self.crc = None
self.comm_params = comm_params
if 'comm_type' in self.comm_params:
self.comm_type = self.comm_params['comm_type']
else:
self.comm_type = 'dummy'
if not comm_params:
self.comm_params = {}
else:
self.comm_params = comm_params
self.chamber_number = chamber_number
# Factory to abstract how we talk to the chamber
self.comm_func = {
'serial': { # over rs232
'connect': self.create_com_serial,
'disconnect': self.disconnect_com_serial,
'write': self.write_com_serial,
'read': self.read_com_serial,
},
'network': { # over network-to-rs232 adapter
'connect': self.create_com_network,
'disconnect': self.disconnect_com_network,
'write': self.write_com_network,
'read': self.read_com_network
},
'dummy': { # when not connected, for development
'connect': self.create_com_dummy,
'disconnect': self.disconnect_com_dummy,
'write': self.write_com_dummy,
'read': self.read_com_dummy
}
}
self.command = {
"read_regs": 0x03,
"write_reg": 0x06,
"write_profile_reg": 0x10
}
# Get crc class
self.crc = CRC16()
# Messages are sent in packets that must be delimited by a pause at least as long as the time it
# takes to send 28 bits (3.5 characters). To determine this time in seconds, divide 28 by the baud
# rate. In the case of EZT-570i communications at 9600 baud, this calculates to a minimum period
# of 3ms.
#
# In addition, the EZT’s timeout period must be added to that, in order to properly time the send and
# receive messages between the host computer and multiple EZT’s on the serial link. With a
# default timeout period in the EZT-570i of 200ms, it makes a total pause of 203ms minimum.
self.comm_wait_time = 0.203
def connect(self):
if self.comm_type == 'network':
self.log.info("Network communicaiton with chamber")
assert self.comm_params['net_port'] and self.comm_params['net_addr'] and self.comm_params['net_timeout']
elif self.comm_type == 'serial':
self.log.info("Serial communicaiton with chamber")
assert self.comm_params['serial_port']
else:
self.log.info("Dummy communicaiton with chamber")
# Get communication
self.comm_func[self.comm_type]['connect']()
def disconnect(self):
# Get communication
self.comm_func[self.comm_type]['disconnect']()
def write_register(self, register, value):
"""
Set the value of a register
:param register: Address of register
:param value: Value of to write.
:return:
"""
self.log.debug(
"\n"
"# =========================================\n"
"# Write Register\n"
"# ========================================="
)
# --------------------------------------
# PACKET HEADER FOR WRITE REGISTER
# --------------------------------------
# e.g. 0x01 0x06 0x0015 0x0001
fmt = '!2B2H'
packed_header = struct.pack(
fmt,
self.chamber_number,
self.command['write_reg'],
register,
value
)
self.log.debug(
"{:<80}:packed header".format(
[hex(a) for a in struct.unpack(fmt, packed_header)]
)
)
# Add CRC
modbus_msg_as_bytes = self.crc.add_crc(packed_header)
data_hexstring = binascii.hexlify(modbus_msg_as_bytes)
self.log.debug("{:<20}:message + crc".format(data_hexstring))
# load byte array into structure
modbus_write_request = modbus_packets.WriteRegister()
ctypes.memmove(
ctypes.addressof(modbus_write_request),
modbus_msg_as_bytes,
len(modbus_msg_as_bytes)
)
# Print structure
self.log.debug(
(
"Modbus Request: {}"
).format(
modbus_write_request
)
)
# --------------------------------------
# Send request
# --------------------------------------
self.comm_func[self.comm_type]['write'](modbus_write_request)
# --------------------------------------
# Read response
# --------------------------------------
# Create response Structure sized for expected data
modbus_write_response = modbus_packets.WriteRegister()
modbus_response = self.read_response(modbus_write_response)
self.log.debug(
(
"Modbus Request: {}"
).format(
modbus_response
)
)
def read_registers(self, register, quantity):
"""
Read the value of a series of registers
:param register: Starting register
:param quantity: How many registers to read
"""
self.log.debug(
(
"\n# =========================================\n"
"# Read Registers: reg:{}, quantity:{}\n"
"# ========================================="
).format(
register,
quantity
)
)
# --------------------------------------
# PACKET HEADER READ REGISTERS
# --------------------------------------
# e.g. 0x01 0x03 0x003D 0x0001
fmt = '!2B2H'
packed_header = struct.pack(
fmt,
self.chamber_number,
self.command['read_regs'],
register,
quantity
)
self.log.debug(
(
"{:<80}:packed header"
).format(
[hex(a) for a in struct.unpack(fmt, packed_header)]
)
)
# Add CRC
modbus_msg_as_bytes = self.crc.add_crc(packed_header)
data_hex_string = binascii.hexlify(modbus_msg_as_bytes)
self.log.debug("{:<20}:message + crc".format(data_hex_string))
# load structure
modbus_read_request = modbus_packets.ReadRegistersSend()
ctypes.memmove(
ctypes.addressof(modbus_read_request),
modbus_msg_as_bytes,
len(modbus_msg_as_bytes)
)
self.log.debug(
(
"Modbus Request:{}"
).format(
modbus_read_request
)
)
# Send modbus request
self.comm_func[self.comm_type]['write'](modbus_read_request)
# Create response Structure sized for expected data
modbus_read_response = modbus_packets.read_response_factory(quantity)
# Read modbus response
modbus_response = self.read_response(modbus_read_response)
return modbus_response
def read_response(self, modbus_response):
size_read_response = ctypes.sizeof(modbus_response)
# Read response to read request
modbus_response_msg_as_bytes = \
self.comm_func[self.comm_type]['read'](size_read_response)
if self.comm_type is not 'dummy':
# Validate response
self.crc.validate_crc(modbus_response_msg_as_bytes)
# Convert string to bytes to string of hex
modbus_response_msg_as_hex = binascii.hexlify(modbus_response_msg_as_bytes)
self.log.debug("{:<20} : response msg".format(modbus_response_msg_as_hex))
# Populate the structure
ctypes.memmove(
ctypes.addressof(modbus_response),
modbus_response_msg_as_bytes,
len(modbus_response_msg_as_bytes)
)
# Print structure
self.log.debug(
(
"Modbus Response:{}"
).format(
modbus_response
)
)
return modbus_response
def print_profile(self, project_file):
self.log.info(
(
"\n"
"# =========================================\n"
"# PRINT PROFILE: {}\n"
"# ========================================="
).format(
project_file
)
)
# Read file into list of profile steps
with open(project_file) as fh:
# Harvest the first line
profile_header = self.read_profile_lines(fh, 1)
self.log.info("Step:{}".format(profile_header))
# Load Header into cyptes.Structure
profile_header_bytes = struct.pack('!15H', *profile_header[0])
profile_header_struct = chamber_commands.ProfileHeader()
ctypes.memmove(
ctypes.addressof(profile_header_struct),
profile_header_bytes,
len(profile_header_bytes)
)
self.log.info("Profile Header:\n{}".format(profile_header_struct))
# Determine how many steps to read from file
steps_tot = profile_header[0][9]
self.log.info("Steps in Profile:{}".format(steps_tot))
# Read steps from file
profile_steps = self.read_profile_lines(fh, steps_tot)
self.log.debug("Steps loaded:{}".format(len(profile_steps)))
register_start = 200 # First Register Address 0x00c8
registers_to_write = 15 # data registers per packet
for i, step in enumerate(profile_steps):
register_start += registers_to_write
# Load Steps into cyptes.Structure
profile_step_bytes = struct.pack('!15h', *step)
profile_step_struct = chamber_commands.ProfileSteps(register_start)
ctypes.memmove(
ctypes.addressof(profile_step_struct),
profile_step_bytes,
len(profile_step_bytes)
)
self.log.info(
(
"\n=========================================\n"
"Step:{}\n"
"=========================================\n"
"{}"
).format(
i+1,
profile_step_struct
)
)
# Convert profile header+steps into list of modbus packets
modbus_packed_profile = self.profile_to_modbus_packets(
profile_header, profile_steps
)
def load_profile(self, project_file):
"""
Load a CSZ Profile file into the chamber.
:param project_file: path to file
:return:
"""
self.log.debug(
(
"\n"
"# =========================================\n"
"# WRITE PROFILE: {}\n"
"# ========================================="
).format(
project_file
)
)
# Read file into list of profile steps
with open(project_file) as fh:
# Harvest the first line
profile_header = self.read_profile_lines(fh, 1)
self.log.debug("Profile Header:{}".format(profile_header))
# Determine how many steps to read from file
steps_tot = profile_header[0][9]
self.log.info("Steps in Profile:{}".format(steps_tot))
# Read steps from file
profile_steps = self.read_profile_lines(fh, steps_tot)
self.log.debug("Steps loaded:{}".format(len(profile_steps)))
# Convert profile header+steps into list of modbus packets
modbus_packed_profile = self.profile_to_modbus_packets(
profile_header, profile_steps
)
self.log.debug("Profile Packets")
for i in modbus_packed_profile:
self.log.debug("Packet:{}".format(i))
# Send over the wire
self.write_profile_to_modbus(modbus_packed_profile)
def write_profile_to_modbus(self, modbus_packed_profile):
"""
Write profile packets to modbus
:param modbus_packed_profile:
:return:
"""
self.log.info("Load Profile")
for i, packet in enumerate(modbus_packed_profile):
self.log.info("--------------Line:{} --------------".format(i))
#---------------------------------------
# Write lines, and retry if a problem
#---------------------------------------
self.write_profile_lines(packet)
self.log.info("Profile upload complete")
@retrying.retry(
stop_max_delay=40000,
wait_fixed=200,
stop_max_attempt_number=200,
retry_on_exception=retry_if_crc_error
)
def write_profile_lines(self, packet):
self.log.debug(
'WriteProfileSend:{}'.format(packet)
)
# --------------------------------------
# Send request
# --------------------------------------
self.comm_func[self.comm_type]['write'](packet)
# --------------------------------------
# Mandatory wait between each write
# --------------------------------------
time.sleep(self.comm_wait_time)
# --------------------------------------
# Read response
# --------------------------------------
# Create response Structure sized for expected data
modbus_write_profile_response = modbus_packets.WriteProfileResponse()
modbus_response = self.read_response(modbus_write_profile_response)
# Print structure
self.log.debug(
(
"WriteProfileResponse:{}"
).format(
modbus_response
)
)
def read_profile_lines(self, fh, lines):
"""
Read the profile file, converting to list of int
:param fh:
:param lines:
:return:
"""
self.log.debug("Read the profile file, converting to list of int")
data_all_lines = []
for i in xrange(lines):
# Convert comma separated text to array of int, with 15 elements
data_int_array = []
data_list = fh.readline().strip().split(',')[:15]
self.log.debug("data_str:{}".format(data_list))
for val in data_list:
# Convert string to int, floats are multiplied by 10
val = int_or_float(val)
# Finally add to the array
data_int_array.append(val)
self.log.debug("data_int_array:{}".format(data_int_array))
data_all_lines.append(data_int_array)
return data_all_lines
def profile_to_modbus_packets(self, profile_header, profile_steps):
"""
Convert list of int to list of modbus packets
:param profile_header:
:param profile_steps:
:return: list of WriteProfileSend
"""
self.log.debug("Convert list of int to list of modbus packets")
modbus_packed_profile = []
register_start = 200 # First Register Address 0x00c8
registers_to_write = 15 # data registers per packet
bytes_written = 30 # data bytes in each packet
for line, data_int_array in enumerate(itertools.chain(profile_header, profile_steps)):
# Increment the starting register address by 15 for consecutive elements
# First is 200, second is 215, third is 230, ....
if line:
register_start += registers_to_write
# --------------------------------------
# Convert array of int to packed data
# --------------------------------------
data_bytearray = bytearray()
for i in data_int_array:
s = struct.pack('!h', i)
data_bytearray += s
data_hexstring = binascii.hexlify(data_bytearray)
self.log.debug("{:<80}:data_bytearray".format(data_hexstring))
# --------------------------------------
# PACKET HEADER FOR PROFILE WRITE
# --------------------------------------
fmt = '!2B2HB'
packed_header = struct.pack(
fmt,
self.chamber_number,
self.command['write_profile_reg'],
register_start,
registers_to_write,
bytes_written
)
self.log.debug(
"{:<80}:packed header".format(
[hex(a) for a in struct.unpack(fmt, packed_header)]
)
)
# --------------------------------------
# JOIN HEADER + DATA
# --------------------------------------
msg_as_bytes = bytes(packed_header) + bytes(data_bytearray)
data_hexstring = binascii.hexlify(msg_as_bytes)
self.log.debug("{:<80}:msg_as_bytes".format(data_hexstring))
# --------------------------------------
# Add CRC
# --------------------------------------
modbus_msg_as_bytes = self.crc.add_crc(msg_as_bytes)
data_hexstring = binascii.hexlify(modbus_msg_as_bytes)
self.log.debug("{:<80}:msg_as_bytes + crc".format(data_hexstring))
# --------------------------------------
# Load ctypes.Structure
# --------------------------------------
modbus_profile_request = modbus_packets.write_profile_factory(registers_to_write)
ctypes.memmove(
ctypes.addressof(modbus_profile_request),
modbus_msg_as_bytes,
len(modbus_msg_as_bytes)
)
# --------------------------------------
# Append to list of packed data
# --------------------------------------
modbus_packed_profile.append(modbus_profile_request)
return modbus_packed_profile
def create_com_network(self):
"""Network Setup"""
self.log.debug("Create tcp scoket communication object")
self.log.debug("Opening socket")
net_port = self.comm_params['net_port']
net_addr = self.comm_params['net_addr']
net_timeout = self.comm_params['net_timeout']
self.comm = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.comm.connect((net_addr, net_port))
self.comm.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
self.comm.settimeout(net_timeout)
def create_com_serial(self):
"""Serial Port Setup"""
self.log.debug("Create serial communication object")
self.comm = serial.Serial(
port=self.comm_params['serial_port'],
baudrate=9600,
timeout=1,
parity=serial.PARITY_EVEN,
bytesize=serial.EIGHTBITS,
stopbits=serial.STOPBITS_ONE,
xonxoff=False,
rtscts=False,
dsrdtr=False
)
def create_com_dummy(self):
"""Dummy Setup"""
self.log.debug("Creating dummy communication object")
self.comm = ""
def disconnect_com_network(self):
self.comm.close()
def disconnect_com_serial(self):
self.comm.close()
def disconnect_com_dummy(self):
self.comm = None
def write_com_serial(self, buffer):
"""Send request over Serial"""
self.log.debug("Send request")
if not (self.comm and self.comm.is_open):
self.comm.open()
self.comm.write(buffer)
time.sleep(self.comm_wait_time)
def write_com_network(self, buffer):
"""Send request over Ethernet"""
self.log.debug("Send request")
self.comm.sendall(buffer)
time.sleep(self.comm_wait_time)
def write_com_dummy(self, buffer):
"""Send request over Ethernet"""
self.log.info("Send request")
time.sleep(self.comm_wait_time)
def read_com_serial(self, size_read_response):
"""Read serial port, return stuff"""
self.log.debug("Read response")
return self.comm.read(size_read_response)
def read_com_network(self, size_read_response):
"""Read network socket, return stuff"""
self.log.debug("Read response")
chunks = []
bytes_recd = 0
while bytes_recd < size_read_response:
self.log.debug("bytes_recd:{}".format(bytes_recd))
chunk = self.comm.recv(1024)
self.log.debug("chunk:{}".format(chunk))
if chunk == '':
raise RuntimeError("socket connection broken")
chunks.append(chunk)
bytes_recd = bytes_recd + len(chunk)
return ''.join(chunks)
def read_com_dummy(self, size_read_response):
"""Read nothing, return empty"""
return ''
class CRC16(object):
"""
Working with CRC16
Most code is taken from:
https://github.com/pyhys/minimalmodbus/blob/e99f4d74c83258c6039073082955ac9bed3f2155/minimalmodbus.py # NOQA
"""
def __init__(self):
self.look_up_table = self.generate_look_up_table()
def generate_look_up_table(self):
""" Generate look up table.
:return: List
"""
poly = 0xA001
table = []
for index in range(256):
data = index << 1
crc = 0
for _ in range(8, 0, -1):
data >>= 1
if (data ^ crc) & 0x0001:
crc = (crc >> 1) ^ poly
else:
crc >>= 1
table.append(crc)
return table
def get_crc(self, msg):
""" Return CRC of 2 byte for message.
>>> assert self.get_crc(b'\x02\x07') == struct.unpack('<H', b'\x41\x12')
:param msg: bytes.
:return: 2 bytes.
"""
register = 0xFFFF
for byte_ in msg:
try:
val = struct.unpack('<B', byte_)[0]
# Iterating over a bit-like objects in Python 3 gets you ints.
except TypeError:
val = byte_
register = \
(register >> 8) ^ self.look_up_table[(register ^ val) & 0xFF]
# CRC is little-endian!
return struct.pack('<H', register)
def add_crc(self, msg):
""" Append CRC to message.
:param msg: bytes.
:return: bytes.
"""
return msg + self.get_crc(msg)
def validate_crc(self, msg):
""" Validate CRC of message.
:param msg: bytes, message with CRC.
:raise: CRCError.
"""
if not struct.unpack('<H', self.get_crc(msg[:-2])) ==\
struct.unpack('<H', msg[-2:]):
raise CRCError('CRC validation failed.')
class CRCError(Exception):
""" Valid error to raise when CRC isn't correct. """
pass
if __name__ == '__main__':
# --------------------
# Logging setup
# --------------------
ch = logging.StreamHandler()
ch_fmt = logging.Formatter("%(levelname)s\t: %(message)s")
ch.setFormatter(ch_fmt)
log = logging.getLogger()
log.addHandler(ch)
log.setLevel(logging.INFO) # DEBUG,INFO,WARNING,ERROR,CRITICAL
log.debug("Initialized Logger")
import chamber_commands
light_control = chamber_commands.ctrl_registers['CHAMBER_LIGHT_CONTROL']
comm_params = {'comm_type': 'dummy'}
chamber = ChamberCommunication(comm_params, log=log)
chamber.connect()
# Chamber "Profile" file
project_file = 'GALILEO.txt'
chamber.load_profile(project_file)
state = {
'Off': 0,
'On': 1
}
chamber.toggle_light(2)