def test(self, cmd=None): if cmd == 'self': mb = modbus.get_port(self.modbus_port) if not mb: return 'FAILED' mb.release() return 'OK' if cmd == 'info': mb = modbus.get_port(self.modbus_port) if not mb: return 'FAILED' if mb.client_type in ['tcp', 'udp']: reg = 22 else: reg = 21 rr = mb.read_holding_registers(reg, 1, unit=self.addr) mb.release() if rr.isError(): return 'FAILED' try: return '{:.2f}'.format(float(rr.registers[0]) / 100.0) except: log_traceback() return 'FAILED' if cmd == 'get': return self.get(timeout=get_timeout() * 10) return { 'info': 'returns relay firmware version', 'get': 'get current relay state' }
def exec(self, cmd=None, args=None): if cmd == 'id': try: new_id = int(args) if new_id < 1 or new_id > 247: raise Exception('id not in range 1..247') mb = modbus.get_port(self.modbus_port) if not mb: raise Exception('unable to get modbus port {}'.format( self.modbus_port)) try: result = mb.write_register(18, new_id, unit=self.addr) except: log_traceback() result = None mb.release() if not result or result.isError(): return 'FAILED' self.addr = new_id self.phi_cfg['addr'] = new_id return 'OK' except: log_traceback() return 'FAILED' else: return { 'id': 'change relay slave ID (1..247), relay reboot required' }
def test(self, cmd=None): if cmd == 'self': mb = modbus.get_port(self.modbus_port) if not mb: return 'FAILED' mb.release() return 'OK' if cmd == 'get': return self.get(timeout=get_timeout() * 10) return {'get': 'get current relay state'}
def set(self, port=None, data=None, cfg=None, timeout=0): try: p = int(port) val = int(data) except: return False if p < 1 or p > self.port_max or val < 0 or val > 1: return False mb = modbus.get_port(self.modbus_port, timeout) if not mb: return None result = mb.write_coil(p - 1, True if val else False, unit=self.addr) mb.release() return not result.isError()
def set(self, port=None, data=None, cfg=None, timeout=0): time_start = time() try: if isinstance(port, list): _port = port _data = data else: _port = [port] _data = [data] mb = modbus.get_port(self.modbus_port, timeout) if not mb: return None rr = mb.read_holding_registers(1, 1, unit=self.unit_id) mb.release() if rr.isError(): self.log_debug(rr) return False r = rr.registers[0] for i in range(0, len(_port)): _p = int(_port[i]) _d = int(_data[i]) if _p < 1 or _p > 8: raise Exception('port is not in range 1..8') if _d < 0 or _d > 1: raise Exception('data is not in range 0..1') if _d: r = 1 << (_p - 1) ^ 0xFF & r else: r = r | 1 << (_p - 1) if time_start + timeout <= time(): raise Exception('Modbus timeout') mb = modbus.get_port(self.modbus_port, timeout) if not mb: return None rr = mb.write_register(1, r, unit=self.unit_id) mb.release() if rr.isError(): self.log_debug(rr) return False else: return True except: log_traceback() return False
def get(self, port=None, cfg=None, timeout=0): mb = modbus.get_port(self.modbus_port, timeout) if not mb: return None rr = mb.read_coils(0, 16, unit=self.addr) mb.release() if rr.isError(): return None result = {} try: for i in range(16): result[str(i + 1)] = 1 if rr.bits[i] else 0 except: result = None return result
def get(self, port=None, cfg=None, timeout=0): mb = modbus.get_port(self.modbus_port, timeout) if not mb: return None try: rr = mb.read_input_registers(30014, 2, unit=self.addr) mb.release() if rr.isError(): self.log_debug(rr) return None return {'l': rr.registers[0] * 65536 + rr.registers[1]} except: log_traceback() return None
def test(self, cmd=None): if cmd == 'self': mb = modbus.get_port(self.modbus_port, get_timeout()) rr = mb.read_input_registers(30000, 1, unit=self.addr) mb.release() try: if rr.isError(): raise Exception(str(rr)) if rr.registers[0] != 1: raise Exception('status: {}'.format(rr.registers[0])) return 'OK' except Exception as e: self.log_error(e) return 'FAILED' elif cmd == 'fwver': mb = modbus.get_port(self.modbus_port, get_timeout()) rr = mb.read_input_registers(30001, 1, unit=self.addr) mb.release() if rr.isError(): self.log_error(rr) return None return rr.registers[0] else: return {'fwver': 'Get firmware version'}
def get(self, port=None, cfg=None, timeout=0): mb = modbus.get_port(self.modbus_port, timeout) if not mb: return None try: rr = mb.read_input_registers(30002, 2, unit=self.addr) mb.release() if rr.isError(): self.log_debug(rr) return None temp = rr.registers[0] if temp > 32767: temp = temp - 65536 return {'t': temp / 100, 'h': rr.registers[1] / 100} except: log_traceback() return None
def get(self, port=None, cfg=None, timeout=0): mb = modbus.get_port(self.modbus_port, timeout) if not mb: return None try: rr = mb.read_holding_registers(3000, 2, unit=self.unit_id) mb.release() if rr.isError(): self.log_debug(rr) return None val = struct.unpack( 'f', struct.pack('H', rr.registers[0]) + struct.pack('H', rr.registers[1]))[0] return (1, '{:.2f}'.format(val)) if val > 0 else 0 except: log_traceback() return None
def get(self, port=None, cfg=None, timeout=0): mb = modbus.get_port(self.modbus_port, timeout) if not mb: return None try: rr = mb.read_holding_registers(1, 1, unit=self.unit_id) mb.release() if rr.isError(): self.log_debug(rr) return None result = {} r = rr.registers[0] for i in range(0, 7): result[str(i + 1)] = 1 if r & 1 << i else 0 return result except: log_traceback() return None
def set(self, port=None, data=None, cfg=None, timeout=0): try: status, value = data if not value: status = 0 mb = modbus.get_port(self.modbus_port, timeout) if not mb: return None if status == 0: value = 0 dts = [] v = struct.pack('f', float(value)) dts.append(struct.unpack('H', v[:2])[0]) dts.append(struct.unpack('H', v[2:])[0]) rr = mb.write_registers(3000, dts, unit=self.unit_id) mb.release() if rr.isError(): self.log_debug(rr) return False else: return True except: log_traceback() return False