def receiveCommand(self): log.info("read 1") command_id = self.read(1) if len(command_id) < 1: log.info("Did not read one byte of command_id") return None klass = getCommandByID(unpack(b'B', command_id)[0]) if not klass: log.error("Command ID not recognized: %s", command_id) return None command = klass() self.consume(1) if len(command) > 1: buf = command_id + self.read(len(command) - 1) else: buf = command_id if len(buf) < len(command): log.info("Read only %s of %s bytes", len(buf), len(command)) self.pushback(command_id) return None try: log.info("unpack %s", buf) command.unpack(buf) except Exception as e: log.info("Cannot unpack buf=%s exception=%s", buf, str(e)) return None self.consume(len(command) - 1) log.info("return %s", command) return command
def __init__(self, serial_number=None, device=None, timeout=0, *args, **kwargs): log.info("serial_number=%s device=%s timeout=%s args=%s kwargs=%s", serial_number, device, timeout, args, kwargs) self.serial = Serial(*args, **kwargs) self.serial.baudrate = 115200 self.serial.timeout = timeout self.serial.port = None if serial_number: ports = list(list_ports.grep(serial_number)) if ports: self.serial.port = ports[0].device self.serial.serial_number = ports[0].serial_number log.info("search for serial_number=%s found port=%s", serial_number, self.serial.port) elif device: self.serial.port = device log.info("using device=%s", device) else: # NOTE: 2021-06-23 (sustainablelab) # Long serial numbers are no longer supported # Change "CHROMATION" to "CH" # ports = list(list_ports.grep("CHROMATION")) ports = list(list_ports.grep("CH")) if ports: self.serial.port = ports[0].device self.serial.serial_number = ports[0].serial_number log.info( "defaulting to searching for CHROMATION hardware, " "found port=%s, serial_number=", self.serial.port, self.serial.serial_number) if not self.serial.port: for port in list_ports.comports(True): if port.vid == 1027 and port.pid == 24597: self.serial.port = port.device break if not self.serial.port: log.error("Cannot find CHROMATION device") raise MicroSpecConnectionException("Cannot find CHROMATION device") try: self.serial.open() except Exception as e: log.error( "Cannot open device: check if port is already in use, such as another MicroSpec interface is using it: %s" % (e)) raise MicroSpecConnectionException(str(e)) super().__init__(self.serial) log.info("return")
def receiveReply(self, command_id): log.info("command_id=%s", command_id) bridge_klass = getBridgeReplyByID(command_id) sensor_klass = getSensorReplyByID(command_id) if not bridge_klass: log.error("Command ID not recognized: %s", command_id) return None if bridge_klass is BridgeNull: log.info("bridge reply is BridgeNull") return bridge_klass() bridgebuf = self.read(0) log.info("bridgebuf=%s", bridgebuf) try: bridge_reply = bridge_klass(bridgebuf) except Exception as e: log.info("bridgebuf=%s exception=%s", bridgebuf, str(e)) return None if not hasattr(bridge_reply, "status") or bridge_reply.status is None: log.info("bridge reply is empty") return None bridgelen = len(bytes(bridge_reply)) self.consume(bridgelen) bridgebuf = bridgebuf[0:bridgelen] if bridge_reply.status != 0 or not sensor_klass: log.info("return bridge_reply=%s", bridge_reply) return bridge_reply sensorbuf = self.read(0) log.info("bridgebuf=%s", sensorbuf) try: sensor_reply = sensor_klass(sensorbuf) except Exception as e: log.info("sensorbuf=%s exception=%s pushing back bridgebuf=%s", sensorbuf, str(e), bridgebuf) self.pushback(bridgebuf) return None if sensor_reply.status is None: log.info("sensor reply is empty") self.pushback(bridgebuf) return None self.consume(len(bytes(sensor_reply))) log.info("return sensor_reply=%s", sensor_reply) return sensor_reply