Пример #1
0
    def test_basic_sync_serial_client(self):
        """ Test the basic methods for the serial sync client"""

        # receive/send
        client = ModbusSerialClient()
        client.socket = MockSocket()
        self.assertEqual(0, client.send(None))
        self.assertEqual(1, client.send('\x00'))
        self.assertEqual('\x00', client.receive(1))

        # connect/disconnect
        self.assertTrue(client.connect())
        client.close()

        # already closed socket
        client.socket = False
        client.close()

        self.assertEqual('ascii baud[19200]', str(client))
Пример #2
0
    def test_basic_sync_serial_client(self):
        """ Test the basic methods for the serial sync client"""

        # receive/send
        client = ModbusSerialClient()
        client.socket = MockSocket()
        self.assertEqual(0, client.send(None))
        self.assertEqual(1, client.send('\x00'))
        self.assertEqual('\x00', client.receive(1))

        # connect/disconnect
        self.assertTrue(client.connect())
        client.close()

        # already closed socket
        client.socket = False
        client.close()

        self.assertEqual('ascii baud[19200]', str(client))
Пример #3
0
    exit(1)
start = time.time()
rr = client.read_holding_registers(0xE000, 1, unit=0)
assert (rr.function_code < 0x80)
print(rr)
#data = client.read_coils(READ_COILS_PDU_Addr_Test_1, 1)

stop = time.time()
if data:
    succ = "was successful"
else:
    succ = "failed"
print("read %s, time spent reading: %fs" % (succ, stop - start))
print("Data read:")
print(data)
client.close()
try:
    data = client.read_coils(READ_COILS_PDU_Addr_Test_1, 1)
    stop = time.time()
    if data:
        succ = "was successful"
    else:
        succ = "failed"
    print("read %s, time spent reading: %fs" % (succ, stop - start))
    print("Data read:")
    print(data)
    client.close()
except pymodbus3.exceptions.ModbusIOException:
    print("SHIT!!!")
    print(pymodbus3.exceptions.ModbusIOException.__cause__)
    print(pymodbus3.exceptions.ModbusIOException.__context__)
Пример #4
0
class Modbus():

	# Init modbus device
	def __init__(self):

		self._client = ModbusClient(method='rtu',
			port = PORT,
			bytesize = BITS,
			parity = PARITY,
			baudrate = BAUDRATE,
			timeout = 1)

		if self._client.connect() == False:
			logger_modbus.error(bcolors.FAIL + "*******************************" + bcolors.ENDC)
			logger_modbus.error(bcolors.FAIL + "*** ERROR, DEVICE NOT FOUND ***" + bcolors.ENDC)
			logger_modbus.error(bcolors.FAIL + "*******************************" + bcolors.ENDC)
			time.sleep(1)

		return

	# Delete modbus device
	def __del__(self):

		logger_modbus.info("Delete modbus device")

		self.close_connection()

		return

	# Toggle relay
	def toggle_relay(self, channel, address):

		try:
			ret = self._client.write_register(channel, 0x0300, unit=address)
		except:
			logger_modbus.error(bcolors.FAIL + "Error, slave %s not responde!"  + bcolors.ENDC, address)
			return -1

		logger_modbus.info("Toggle relay %s" %ret)

		return ret

	# Read status relay
	def read_status_relay(self, channel, address):

		try:
			ret = self._client.read_holding_registers(channel, unit=address)
		except:
			logger_modbus.error(bcolors.FAIL + "Error, slave %s not responde!" + bcolors.ENDC, address)
			return -1

		logger_modbus.info("Status relay channel %d: %s" % (channel, int(ret.registers[0])))

		return int(ret.registers[0])

	# Close device
	def close_connection(self):

	    logger_modbus.info("Close connection!")

	    self._client.close()

	    return 0