from mbUtils import writeMultRegs, sockSend, writeSingleReg from time import sleep import socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect(("192.168.0.2", 502)) data = 0 addr = 7386 msg = writeSingleReg(1, addr, data) sockSend(msg, s) finally: print("end") s.close()
from mbUtils import writeMultRegs, sockSend, readRegs, readCoils import socket import math from struct import * s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) mbReglist = ["0"] mbCoilList = ["0"] numScanRegs = 40 numScanCoils = 700 try: s.connect(("192.168.0.2", 502)) for x in range(1, numScanRegs): msg = readRegs(x, x, 1) respMsg = sockSend(msg, s) if respMsg[7] != 0x83: mbReglist.append(x) for x in range(1, numScanCoils): msg = readCoils(x, x, 1) respMsg = sockSend(msg, s) if respMsg[7] != 0x81: mbCoilList.append(x) print("Registers=") print(*mbReglist, sep=", ") print("Coils=") print(*mbCoilList, sep=", ") finally: