実際のところ
import socket
IP_ADDRESS = "192.168.1.168"
PORT = 502
COMMAND = bytes([0x00, 0x01, 0x00, 0x02, 0x00, 0x01, 0x10])
def send_modbus_command(ip_address, port, command):
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.settimeout(10)
s.connect((ip_address, port))
s.sendall(command)
response = s.recv(1024)
print("Received (bytes):", response)
parsed_response = ",".join(["0x{:02x}".format(b) for b in response])
print("Received (parsed):", parsed_response)
if __name__ == "__main__":
send_modbus_command(IP_ADDRESS, PORT, COMMAND)
Received (bytes): b'\x00\x01\x00\x02\x00\x02\x10\x00'
Received (parsed): 0x00,0x01,0x00,0x02,0x00,0x02,0x10,0x00