大哥能帮我编写下吗? 家里的志高中央空调也是modbus的,主要的命令已经弄出来了,具体能再帮忙完善下吗?
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from socket import *
import struct
from functools import reduce
import re
def check_sum(data1):
b1 = bytearray.fromhex(data1)
sum = reduce(lambda x,y : x+y, b1[2:],0)
checksum = (sum ^ 0xFF) + 1 & 0xFF
b1+=struct.pack('B',(checksum))
comend = re.findall(r'.{2}', b1.hex())
print ('生成控制指令'+b1.hex())
print ('生成控制指令'+'0x'+',0x'.join(comend))
#print (b1.hex())
return b1.hex()
def main():
# 1.创建tcp_client_socket 套接字对象
tcp_client_socket = socket(AF_INET,SOCK_STREAM)
# 作为客户端,主动连接服务器较多,一般不需要绑定端口
# 2.连接服务器
tcp_client_socket.connect(("192.168.4.12",50000))
#tcp_client_socket.connect(("www.baidu.com",80))
air_v = '001301d3'#版本 #001301da锁定模式 #解除锁定模式001301db,调试模式001301d0 001301d3(书房可用)
air_id = '80' #设备1#'80' 设备2#'40' 设备3#'20' 设备4#'10'
air_x = '00000000000000'#补码
air_pow = '81'# 开机( 88制冷 81送风 #82 除湿 #83 制冷 #84制热 #90自动 )#80关机(错误 实际是制冷(但机器不工作)) #08关机
air_Mod = '03'#04低 #02中 #01高 #03自动
air_hot = '14'#温度 13表示19度.;14表示20度
air_x2 = '0000'
air_swing = '00'#开启摆动'04',#关闭摆动'00'
air_end = '002c'#控制模式 #自由模式'002c' #遥控器锁定模式'0023' #解除遥控器模式'0022'
#前20位参数组合
aircomend =[
air_v,
air_id,
air_x,
air_pow,
air_Mod,
air_hot,
air_x2,
air_swing,
air_end
]
#合并前20位参数
str1 = ''.join(aircomend)
#生成效验码
#查询码
#str1 = '001301d00800000000000000000000000000002f'
air_cmd = check_sum(str1)
#生成控制指令
tcp_client_socket.send(bytes.fromhex(air_cmd))
#tcp_client_socket.send(bytes.fromhex('001301d08000000000000000000000000000002f80'))
recv_data = tcp_client_socket.recv(1024) # 此处与udp不同,客户端已经知道消息来自哪台服务器,不需要用recvfrom了
# 设备 开关状态 开关模式 风度 温度 摆风 遥控器84开启04关闭
#002a01d0 f0fe6b4b715e 80 00 00 00 00 00 00 00 01 00 14 00 00 13 54 50 50 49 00 00 00 00 00 00 00 00 00 00 00 00 0a 00 00 cd#设备1 #送风模式#19度
#002a01d0 f0fe6b4b715e 80 00 00 00 00 00 00 00 01 00 14 00 00 12 54 50 50 49 00 00 00 00 00 00 00 00 00 00 00 00 0a 00 00 cd#设备1 #送风模式#18度
#002a01d0 f0fe6b4b715e 80 00 00 00 00 00 00 00 00 00 14 00 00 12 54 50 50 49 00 00 00 00 00 00 00 00 00 00 00 00 0a 00 00 cf#设备1 #制冷模式#18度
#002a01d0 f0fe6b4b715e 80 00 00 00 00 00 00 00 00 00 14 88 00 12 54 50 50 49 00 00 00 00 00 00 00 00000000000a000047#开启制冷模式
#002a01d0 f0fe6b4b715e 80 00 00 00 00 00 00 00 01 00 14 00 00 12 54 50 50 49 00 00 00 00 00 00 00 00000000000a0000ce#关机
#002a01d0 f0fe6b4b715e 80 00 00 00 00 00 00 00 01 00 14 88 04 12 54 50 50 49 00 00 00 00 00 04 00 00 00 00 00 00 0a 00 00 3e
#002a01d0 f0fe6b4b715e 80 00 00 00 00 00 00 00 01 00 14 88 02 12 52 50 50 49 00 00 00 00 00 00 00 00000000000a000046
#002a01d0 f0fe6b4b715e 80 00 00 00 00 00 00 00 01 00 14 88 04 12 52 50 50 49 00 00 00 00 00 00 00 00000000000a000044
#002a01d0 f0fe6b4b715e 80 00000000000000 01 00 14 88 00 12 52 50 50 49 00 00 00 00 00 00 00 00000000000a000048
#002a01d0 f0fe6b4b715e 80 00000000000000 01 00 14 a8 00 12 52 50 50 49 00 00 00 00 00 00 14 00000000000a000014#锁定
#002a01d0 f0fe6b4b715e 80 00000000000000 01 00 14 00 00 15 52 50 50 49 00 00 00 00 00 00 00 00000000000a0000cd
#002a01d0 f0fe6b4b715e 80 00000000000000 01 00 14 20 00 15 52 50 50 49 00 00 00 00 00 00 10 00000000000a00009d
#002a01d0f0fe6b4b715e8000000000000000010014000015525050490000000000008000000000000a00004d#遥控器锁定模式
#002a01d0f0fe6b4b715e8000000000000000000014000015525050490000000000000000000000000a0000ce
#002a01d0f0fe6b4b715e8000000000000000010014000015525050490000000000000000000000000a0000cd#关闭遥控器锁定模式
if recv_data:
print("返回的消息为:",recv_data.hex())
else:
print("对方已离线。。")
tcp_client_socket.close()
if __name__ == '__main__':
main()
|