#与汇川AM401plc进行通信
可使用
import modbus_tk
import modbus_tk.defines as cst
import modbus_tk.modbus_tcp as modbus_tcp
logger = modbus_tk.utils.create_logger("console")
import struct
AI = {}
if __name__ == "__main__":
def int2float(a, b):
f = 0
try:
z0 = hex(a)[2:].zfill(4) # 取0x后边的部分
z1 = hex(b)[2:].zfill(4) # 取0x后边的部分
z = z1 + z0 # 高字节在前 低字节在后
f = struct.unpack('!f', bytes.fromhex(z))[0] # 返回浮点数
# z=z0+z1 #低字节在前 高字节在后
# print (z)
except BaseException as e:
print(e)
return f
def float2int(a):
return struct.unpack(', struct.pack(', a)) # 两次操作都为小端形式
try:
# 连接MODBUS TCP从机
master = modbus_tcp.TcpMaster(host="192.168.7.254")
master.set_timeout(5.0)
logger.info("connected")
a_0 = master.execute(1, cst.READ_HOLDING_REGISTERS, 600, 2)#读浮点数据
AI[0] = float('%.2f' % int2float(a_0[0], a_0[1]))
print(AI[0])
a = float2int(11.25)
logger.info(master.execute(1, cst.WRITE_MULTIPLE_REGISTERS, 600, output_value=[a[0],a[1]]))#写浮点数据
# 读输入寄存器
# logger.info(master.execute(1, cst.WRITE_MULTIPLE_COILS, 0, output_value=[1,1,1,1]))
logger.info(master.execute(1, cst.WRITE_SINGLE_REGISTER, 4, output_value=0)) #写入mx8.0 ,除以2
logger.info(master.execute(1, cst.READ_INPUT_REGISTERS, 4, 1)) # #读取mx8.0 ,除以2
logger.info(master.execute(1, cst.WRITE_SINGLE_REGISTER, 100, output_value=-1)) # mw100 写入20
logger.info(master.execute(1, cst.READ_INPUT_REGISTERS, 100, 1)) # mw100 读取
# logger.info(master.execute(1, cst.WRITE_MULTIPLE_REGISTERS, 100, output_value=[3,0,0,0])) #mw100 写入2 批量写入
except modbus_tk.modbus.ModbusError as e:
logger.error("%s- Code=%d" % (e, e.get_exception_code()))
负数读写有问题
import modbus_tk
import modbus_tk.defines as cst
import modbus_tk.modbus_tcp as modbus_tcp
logger = modbus_tk.utils.create_logger("console")
import struct
AI = {}
if __name__ == "__main__":
def int2float(a1):
return struct.unpack(', struct.pack(', a1[0], a1[1]),a1[2])
def float2int(a):
return struct.unpack(', struct.pack(', a)) # 两次操作都为小端形式
try:
# 连接MODBUS TCP从机
master = modbus_tcp.TcpMaster(host="192.168.7.254")
master.set_timeout(5.0)
logger.info("connected")
a_0 = master.execute(1, cst.READ_HOLDING_REGISTERS, 600, 2)
b=struct.unpack(', struct.pack(', a_0[0], a_0[1]))
print(b)
a = float2int(-256.456)
logger.info(master.execute(1, cst.WRITE_MULTIPLE_REGISTERS, 600, output_value=[a[0],a[1]]))
# 读输入寄存器
# logger.info(master.execute(1, cst.WRITE_MULTIPLE_COILS, 0, output_value=[1,1,1,1]))
logger.info(master.execute(1, cst.WRITE_SINGLE_REGISTER, 4, output_value=0)) #写入mx8.0 ,除以2
logger.info(master.execute(1, cst.READ_INPUT_REGISTERS, 4, 1)) # #读取mx8.0 ,除以2
logger.info(master.execute(1, cst.WRITE_SINGLE_REGISTER, 100, output_value=-1)) # mw100 写入20
logger.info(master.execute(1, cst.READ_INPUT_REGISTERS, 100, 1)) # mw100 读取
# logger.info(master.execute(1, cst.WRITE_MULTIPLE_REGISTERS, 100, output_value=[3,0,0,0])) #mw100 写入2 批量写入
except modbus_tk.modbus.ModbusError as e:
logger.error("%s- Code=%d" % (e, e.get_exception_code()))
https://blog.csdn.net/lzl640/article/details/118722675?ops_request_misc=&request_id=&biz_id=102&utm_term=python%E5%86%99%E5%85%A5modbustcp%E6%B5%AE%E7%82%B9&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-2-118722675.142v62pc_search_tree,201v3control,213v1control&spm=1018.2226.3001.4187
https://blog.csdn.net/weixin_42631695/article/details/126870220?ops_request_misc=&request_id=&biz_id=102&utm_term=python%E5%86%99%E5%85%A5modbustcp%E6%B5%AE%E7%82%B9&utm_medium=distribute.pc_search_result.none-task-blog-2allsobaiduweb~default-8-126870220.142v62pc_search_tree,201v3control,213v1control&spm=1018.2226.3001.4187