1 Star 0 Fork 1

小何/cunqugui

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
lock.py 7.76 KB
一键复制 编辑 原始数据 按行查看 历史
慕远 提交于 2023-12-07 03:35 . 无线锁查询上报
import time
import serial
from serial import SerialException
ser3 = serial.Serial('/dev/ttyS4', 9600, timeout=0.5)
ser4 = serial.Serial('/dev/ttyS8', 9600, timeout=0.5)
threadFlag = None
def CRC_XOR(buff,len):
temp = 0
for i in range(len):
temp ^= buff[i]
return temp
def get_lock_status():
try:
res = ser3.read(100)
res = bytearray(res)
return res
except SerialException as e:
return get_lock_status()
def get_wireless_lock_status():
try:
res = ser4.read(100)
res = bytearray(res)
return res
except SerialException as e:
print(f"异常{e}")
return get_wireless_lock_status()
# 查询无线锁的状态
def close_lock_wireless(hexStr,slave_address=None,type=0):
res = get_wireless_lock_status()
print(f"开锁结果{res}")
try:
if len(res) >= 9:
if hex(res[-2]) == "0x0":
time.sleep(1)
close_light(hexArr[1],slave_address)
return True
else:
return False
if len(res) == 0:
return close_lock_wireless(hexStr, slave_address, type)
except Exception as e:
print(e)
print("无线锁结果解析异常")
return close_lock(hexStr, slave_address, type)
def open_lock(hexStr,slave_address=None,type=0):
if type == 0:
cmd = bytes.fromhex(hexStr)
ser3.write(cmd)
if close_lock(hexStr) == False:
time.sleep(1)
open_light(hexStr)
return True
return False
elif type ==1:
str = "AB " + slave_address + " AA BB " + hexStr
print(str)
cmd = bytes.fromhex(str)
ser4.write(cmd)
if close_lock(hexStr,slave_address,type) == False:
time.sleep(1)
open_light(hexStr,slave_address)
return True
else:
return False
else:
str = "AB " + slave_address +" FF AA"
print(str)
cmd = bytes.fromhex(str)
ser4.write(cmd)
if close_lock_wireless(hexStr, slave_address,type) == False:
time.sleep(1)
open_light(hexStr,slave_address)
return True
else:
return False
def close_lock(hexStr,slave_address=None,type=0):
if type==0:
print(f"{hexStr}监听关锁")
cmd = bytes.fromhex(hexStr)
if len(cmd) == 5:
hexArr = bytearray(cmd)
hexArr[0] = 0x80
hexArr[3] = 0x33
hexArr[4] = CRC_XOR(hexArr, 4)
byte_str = bytes(hexArr)
ser3.write(byte_str)
res = get_lock_status()
if len(res) >= 4:
if hex(res[3]) == "0x11":
# print("状态为开锁")
return False
elif hex(res[3]) == "0x0":
time.sleep(1)
close_light(hexArr[1])
# print("状态为关锁")
return True
else:
# print("没有获取到状态")
return False
elif type ==1:
hexArr = bytearray(bytes.fromhex(hexStr))
hexArr[0] = 0x80
hexArr[3] = 0x33
hexArr[4] = CRC_XOR(hexArr, 4)
hex_str = ' '.join('{:02X}'.format(x) for x in hexArr)
str = "AB " + slave_address + " AA BB " + hex_str
print(str)
print("调用1")
cmd = bytes.fromhex(str)
ser4.write(cmd)
res = get_wireless_lock_status()
print(f"开锁结果{res}")
try:
if len(res) >= 9:
if hex(res[-2]) == "0x0":
time.sleep(1)
close_light(hexArr[1],slave_address)
return True
else:
return False
if len(res) == 0:
return close_lock(hexStr, slave_address,type)
except Exception as e:
print(e)
print("检测异常")
print("调用2")
return close_lock(hexStr, slave_address,type)
else:
str = "AB " + slave_address + " FE 8A"
cmd = bytes.fromhex(str)
ser4.write(cmd)
res = get_wireless_lock_status()
print(f"开锁结果{res}")
try:
if len(res) >= 6:
if hex(res[-1]) == "0x0":
time.sleep(1)
# close_light(hexArr[1],slave_address)
return True
else:
return False
if len(res) == 0:
return close_lock(hexStr, slave_address,type)
except Exception as e:
print(e)
print("检测异常")
print("调用2")
return close_lock(slave_address,type)
def open_light(hostStr,slave_address=None):
cmd = bytes.fromhex(hostStr)
hexArr = bytearray(cmd)
if slave_address != None:
try:
# 开灯
# 定义串口参数
# 设置上电显示颜色 发送读取重量命令并获取返回值DD 55 EE 00 00 00 01 00 99 08 00 00 00 03 00 C8 80 00 40 AA BB
read_weight_command = [0xDD, 0x55, 0xEE, 0x00, 0x00, 0x00, hexArr[1], 0x00, 0x99, 0x08, 0x00, 0x00, 0x00, 0x03,
0x00,
0xC8, 0xFF, 0xFF, 0x00, 0xAA, 0xBB]
hex_str = ' '.join('{:02X}'.format(x) for x in read_weight_command)
str = "AB " + slave_address + " AA BB " + hex_str
print(f"开黄灯{str}")
cmd = bytes.fromhex(str)
ser4.write(cmd)
try:
ser4.read(100)
except SerialException:
print("获取开灯结果失败")
return True
except Exception as e:
return False
else:
try:
# 开灯
# 定义串口参数
print("开黄灯")
# 设置上电显示颜色 发送读取重量命令并获取返回值DD 55 EE 00 00 00 01 00 99 08 00 00 00 03 00 C8 80 00 40 AA BB
read_weight_command = [0xDD, 0x55, 0xEE, 0x00, 0x00, 0x00, hexArr[1], 0x00, 0x99, 0x08, 0x00, 0x00, 0x00, 0x03, 0x00,
0xC8, 0xFF, 0xFF, 0x00, 0xAA, 0xBB]
ser3.write(read_weight_command)
try:
ser3.read(100)
except SerialException:
print("获取开灯结果失败")
return True
except Exception as e:
return False
def close_light(hexStr,slave_address=None):
if slave_address != None:
# 无线
# 关电功能 DD 55 EE 00 00 00 01 00 99 08 00 00 00 03 00 C8 00 00 00 AA BB 颜色设置成黑色
read_weight_command = [0xDD, 0x55, 0xEE, 0x00, 0x00, 0x00, hexStr, 0x00, 0x99, 0x08, 0x00, 0x00, 0x00, 0x03,
0x00,
0xC8, 0xFF, 0x00, 0x00, 0xAA, 0xBB]
hex_str = ' '.join('{:02X}'.format(x) for x in read_weight_command)
str = "AB " + slave_address + " AA BB " + hex_str
print(f"开绿灯{str}")
cmd = bytes.fromhex(str)
ser4.write(cmd)
try:
ser4.read(100)
except SerialException:
print("获取关灯结果失败")
return True
else:
# 有线
print("开绿灯")
# 关电功能 DD 55 EE 00 00 00 01 00 99 08 00 00 00 03 00 C8 00 00 00 AA BB 颜色设置成黑色
read_weight_command = [0xDD, 0x55, 0xEE, 0x00, 0x00, 0x00, hexStr, 0x00, 0x99, 0x08, 0x00, 0x00, 0x00, 0x03,
0x00,
0xC8, 0xFF, 0x00, 0x00, 0xAA, 0xBB]
ser3.write(read_weight_command)
try:
ser3.read(100)
except SerialException:
print("获取关灯结果失败")
return True
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ice-lx/cunqugui.git
git@gitee.com:ice-lx/cunqugui.git
ice-lx
cunqugui
cunqugui
xiugai

搜索帮助