Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import socket
- import struct
- from time import sleep
- import sys,os
- ip = "192.168.1.7"
- port = 502
- unit_id = 1
- start_reg = 4607
- count = 16
- transaction_id = 1
- protocol_id = 0
- length = 6
- function_code = 4
- pkt = struct.pack(
- ">HHHBBHH",
- transaction_id,
- protocol_id,
- length,
- unit_id,
- function_code,
- start_reg,
- count
- )
- with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
- s.connect((ip, port))
- s.sendall(pkt)
- resp = s.recv(1024)
- print("Raw response:", resp.hex())
- if len(resp) < 9:
- raise SystemExit("Short response")
- if resp[7] == 0x84:
- print(f"Modbus exception code: 0x{resp[8]:02X}")
- raise SystemExit
- byte_count = resp[8]
- data = resp[9:9 + byte_count]
- registers = [struct.unpack(">H", data[i:i+2])[0] for i in range(0, len(data), 2)]
- while True:
- for i, value in enumerate(registers):
- print(f"304{608 + i:03d}: {value} (0x{value:04X})")
- sleep(0.5)
- os.system('clear')
- """"
- is D516,518 bogus? ??
- """
Advertisement
Add Comment
Please, Sign In to add comment