def __init__(self, params=None, ts=None): self.ts = ts self.group_name = 'genset' self.data_ipaddr = params.get('data_ipaddr') self.data_ipport = params.get('data_ipport') self.data_slave_id = params.get('data_slave_id') self.ctrl_ipaddr = params.get('cntl_ipaddr') self.ctrl_ipport = params.get('cntl_ipport') self.ctrl_slave_id = params.get('cntl_slave_id') self.data_reg_start = 50042 self.data_modbus_read_length = 79 self.data_device = client.ModbusClientDeviceTCP( slave_id=self.data_slave_id, ipaddr=self.data_ipaddr, ipport=self.data_ipport, timeout=2) self.ctrl_reg_start = 50105 self.ctrl_modbus_read_length = 15 self.ctrl_device = client.ModbusClientDeviceTCP( slave_id=self.ctrl_slave_id, ipaddr=self.ctrl_ipaddr, ipport=self.ctrl_ipport, timeout=2)
def __init__(self, device_type, slave_id=None, name=None, pathlist = None, baudrate=None, parity=None, ipaddr=None, ipport=None, timeout=None, trace=False): device.Device.__init__(self, addr=None) self.type = device_type self.name = name self.pathlist = pathlist self.slave_id = slave_id self.modbus_device = None self.retry_count = 2 self.base_addr_list = [40000, 0, 50000] try: if device_type == RTU: self.modbus_device = modbus.ModbusClientDeviceRTU(slave_id, name, baudrate, parity, timeout, self, trace) elif device_type == TCP: self.modbus_device = modbus.ModbusClientDeviceTCP(slave_id, ipaddr, ipport, timeout, self, trace) elif device_type == MAPPED: if name is not None: self.modbus_device = modbus.ModbusClientDeviceMapped(slave_id, name, pathlist, self) else: if self.modbus_device is not None: self.modbus_device.close() raise SunSpecClientError('Map file required for mapped device') except modbus.ModbusClientError, e: if self.modbus_device is not None: self.modbus_device.close() raise SunSpecClientError('Modbus error: %s' % str(e))
def test_modbus_client_device_tcp_read(pathlist=None, raw_traceback=False): """ -> 00 00 00 00 00 06 01 03 9C 40 00 02 <- 00 00 00 00 00 07 01 03 04 53 75 6E 53 """ try: d = modbus.ModbusClientDeviceTCP(1, ipaddr="127.0.0.1", trace_func=None, test=True) d.socket.in_buf = bytes( b'\x00\x00\x00\x00\x00\x07\x01\x03\x04\x53\x75\x6E\x53') d.socket.out_buf = bytes() data = d.read(40000, 2) if d.socket.out_buf != bytes( b'\x00\x00\x00\x00\x00\x06\x01\x03\x9C\x40\x00\x02'): raise Exception("Modbus request mismatch") if data != bytes(b'SunS'): raise Exception( "Read data mismatch - expected: 'SunS' received: %s") % (data) d.close() except Exception as e: if raw_traceback: traceback.print_exc(file=sys.stdout) print('*** Failure test_modbus_client_device_tcp_read: %s' % str(e)) return False return True
def test_modbus_client_device_tcp_write(pathlist=None, raw_traceback=False): """ -> 00 00 00 00 00 0B 01 10 9C 40 00 02 04 41 42 43 44 <- 00 00 00 00 00 06 01 10 9C 40 00 02 """ try: d = modbus.ModbusClientDeviceTCP(1, ipaddr="127.0.0.1", trace_func=None, test=True) d.socket.in_buf = bytes( b'\x00\x00\x00\x00\x00\x06\x01\x10\x9C\x40\x00\x02') d.socket.out_buf = bytes() d.write(40000, bytes(b'ABCD')) if d.socket.out_buf != bytes( b'\x00\x00\x00\x00\x00\x0B\x01\x10\x9C\x40\x00\x02\x04\x41\x42\x43\x44' ): raise Exception("Modbus request mismatch") d.close() except Exception as e: if raw_traceback: traceback.print_exc(file=sys.stdout) print('*** Failure test_modbus_client_device_tcp_write: %s' % str(e)) return False return True
def test_modbus_client_device_tcp_read(pathlist=None): """ -> 00 00 00 00 00 06 01 03 9C 40 00 02 <- 00 00 00 00 00 07 01 03 04 53 75 6E 53 """ try: d = modbus.ModbusClientDeviceTCP(1, ipaddr="127.0.0.1", trace_func=None, test=True) d.socket.in_buf = '\x00\x00\x00\x00\x00\x07\x01\x03\x04\x53\x75\x6E\x53' d.socket.out_buf = '' data = d.read(40000, 2) if d.socket.out_buf != '\x00\x00\x00\x00\x00\x06\x01\x03\x9C\x40\x00\x02': raise Exception("Modbus request mismatch") if data != 'SunS': raise Exception("Read data mismatch - expected: 'SunS' received: %s") % (data) d.close() except Exception, e: raise print '*** Failure test_modbus_client_device_tcp_read: %s' % str(e) return False
def open(self): ipaddr = self.param_value('ipaddr') ipport = self.param_value('ipport') slave_id = self.param_value('slave_id') self.genset = client.ModbusClientDeviceTCP(slave_id, ipaddr, ipport, timeout=5)
def open(self): """ Open the communications resources associated with the device. """ try: self.device = client.ModbusClientDeviceTCP(slave_id=self.slave_id, ipaddr=self.ip_addr, ipport=self.ip_port, timeout=self.ip_timeout) except Exception, e: raise DeviceError('Cannot connect to PM800: %s' % e)
def test_run(): result = script.RESULT_FAIL gsim = None ts.log_debug(' ') ts.log_debug( 'Note: Please remove the Grid Guard Code from the test configuration after ' 'it has been to run to avoid exposing your Grid Guard Code to others.') ts.log_debug(' ') try: ipaddr = ts.param_value('gg.ipaddr') new_gg = ts.param_value('gg.code') # register = ts.param_value('gg.register') # port = ts.param_value('gg.port') # comm_id = ts.param_value('gg.id') register = 43090 port = 502 comm_id = 3 if not new_gg or new_gg == 'None': raise script.ScriptFail('No Grid Guard Code specified.') device = client.ModbusClientDeviceTCP(comm_id, ipaddr, port) data = device.read(register, 2) gg = util.data_to_u32(data) ts.log('Current grid guard code: %s' % hex(gg)) if gg == 0: ts.log('Original grid guard was not enabled') else: ts.log('Original grid guard was enabled') device.write(register, util.u32_to_data(int(new_gg))) data = device.read(register, 2) gg = util.data_to_u32(data) ts.log('Updated grid guard code: = %s' % hex(gg)) if gg == 0: ts.log_warning('Current grid guard is not enabled') result = script.RESULT_FAIL else: ts.log('Current grid guard is enabled') result = script.RESULT_PASS except script.ScriptFail, e: reason = str(e) if reason: ts.log_error(reason)
def open(self): ipaddr = self.param_value('ipaddr') ipport = self.param_value('ipport') slave_id = self.param_value('slave_id') self.inv = client.ModbusClientDeviceTCP(slave_id, ipaddr, ipport, timeout=5) config_grid_guard = self.param_value('confgridguard') if config_grid_guard == 'True': gg = int(self.param_value('gridguard')) self.gridguard(gg)
def open(self): ipaddr = self.param_value('ipaddr') ipport = self.param_value('ipport') slave_id = self.param_value('slave_id') self.inv = client.ModbusClientDeviceTCP(slave_id, ipaddr, ipport, timeout=5) config_grid_guard = self.param_value('confgridguard') if config_grid_guard == 'True': gg = int(self.param_value('gridguard')) gg_success = self.gridguard(gg) if gg_success: self.ts.log('Grid Guard Code Accepted.') else: self.ts.log_warning('Grid Guard Code Not Accepted!')
def test_modbus_client_device_tcp_write(self): """ -> 00 00 00 00 00 0B 01 10 9C 40 00 02 04 41 42 43 44 <- 00 00 00 00 00 06 01 10 9C 40 00 02 """ d = modbus.ModbusClientDeviceTCP(1, ipaddr="127.0.0.1", trace_func=None, test=True) d.socket.in_buf = b'\x00\x00\x00\x00\x00\x06\x01\x10\x9C\x40\x00\x02' d.socket.out_buf = b'' d.write(40000, 'ABCD') if d.socket.out_buf != b'\x00\x00\x00\x00\x00\x0B\x01\x10\x9C\x40\x00\x02\x04\x41\x42\x43\x44': raise Exception("Modbus request mismatch") d.close()
def test_modbus_client_device_tcp_write(pathlist=None): """ -> 00 00 00 00 00 0B 01 10 9C 40 00 02 04 41 42 43 44 <- 00 00 00 00 00 06 01 10 9C 40 00 02 """ try: d = modbus.ModbusClientDeviceTCP(1, ipaddr="127.0.0.1", trace_func=None, test=True) d.socket.in_buf = '\x00\x00\x00\x00\x00\x06\x01\x10\x9C\x40\x00\x02' d.socket.out_buf = '' d.write(40000, 'ABCD') if d.socket.out_buf != '\x00\x00\x00\x00\x00\x0B\x01\x10\x9C\x40\x00\x02\x04\x41\x42\x43\x44': raise Exception("Modbus request mismatch") d.close() except Exception, e: print '*** Failure test_modbus_client_device_tcp_write: %s' % str(e) return False
def test_modbus_client_device_tcp_read(self): """ -> 00 00 00 00 00 06 01 03 9C 40 00 02 <- 00 00 00 00 00 07 01 03 04 53 75 6E 53 """ d = modbus.ModbusClientDeviceTCP(1, ipaddr="127.0.0.1", trace_func=None, test=True) d.socket.in_buf = b'\x00\x00\x00\x00\x00\x07\x01\x03\x04\x53\x75\x6E\x53' d.socket.out_buf = b'' data = d.read(40000, 2) if d.socket.out_buf != b'\x00\x00\x00\x00\x00\x06\x01\x03\x9C\x40\x00\x02': raise Exception("Modbus request mismatch") if data != b'SunS': raise Exception( "Read data mismatch - expected: 'SunS' received: %s") % (data) d.close()
def open(self): self.device = client.ModbusClientDeviceTCP(self.slave_id, self.ipaddr, self.ipport, timeout=5)
1119: I2 1121: I3 3475: PF1 3477: PF2 3479: PF3 ''' if __name__ == "__main__": ipaddr = '1.1.1.39' #ipaddr = str(raw_input('ip address: ')) device = None if ipaddr: device = client.ModbusClientDeviceTCP(slave_id=159, ipaddr=ipaddr, ipport=502, timeout=10) #, trace_func=trace) data = device.read(1025, 2, op=client.FUNC_READ_INPUT) print((util.data_to_float(data))) data = device.read(1027, 2, op=client.FUNC_READ_INPUT) print((util.data_to_float(data))) data = device.read(1029, 2, op=client.FUNC_READ_INPUT) print((util.data_to_float(data))) print(('%s' % data_read())) print((data_read()['AC_P_1'])) print((data_read()['AC_P_2'])) print((data_read()['AC_P_3']))