1024AirConditioner


import serial
import crcmod
import threading
from playsound import playsound
import time

com = serial.Serial("com1", 38400, timeout=0.2)
lock = threading.Lock()

def getCrc16(veritydata):
if not veritydata:
return
crc16 = crcmod.mkCrcFun(0x18005, 0xFFFF, True, 0x00000)
return crc16(veritydata)

def getModbusBytes(addr, startRegAddr, regNum, regValue, funCode):
if addr < 0 or addr > 0xFF or startRegAddr < 0 or startRegAddr > 0xFFFF or regNum < 0 or regNum > 0x70:
return
sendBytes = addr.to_bytes(1, byteorder="big", signed=False) +\
funCode.to_bytes(1, byteorder="big", signed=False) +\
startRegAddr.to_bytes(2, byteorder="big", signed=False)
for num in range(regNum):
sendBytes += regValue[num].to_bytes(2, byteorder="big", signed=False)
crcCode = getCrc16(sendBytes)
sendBytes += crcCode.to_bytes(2, byteorder="little", signed=False)
return sendBytes

def getLightSwitchState():
sendBytes = getModbusBytes(1, 0, 1, (1,), 4)
lock.acquire()
com.write(sendBytes)
readBytes = com.read(1 * 2 + 5)
lock.release()
return readBytes[4]

def alarmOn():
time.sleep(5)
sendBytes = getModbusBytes(1, 0, 1, (1,), 6)
lock.acquire()
com.write(sendBytes)
com.read(len(sendBytes))
lock.release()
return

def alarmOff():
sendBytes = getModbusBytes(1, 0, 1, (0,), 6)
lock.acquire()
com.write(sendBytes)
com.read(len(sendBytes))
lock.release()
return

def lightOn():
sendBytes = getModbusBytes(1, 1, 1, (1,), 6)
lock.acquire()
com.write(sendBytes)
com.read(len(sendBytes))
lock.release()
return

def lightOff():
sendBytes = getModbusBytes(1, 1, 1, (0,), 6)
lock.acquire()
com.write(sendBytes)
com.read(len(sendBytes))
lock.release()
return

def playSound():
thread1 = threading.Thread(target=playsound, name='thread1', kwargs={"sound":r"D:\1024\1024AirConditioner\temp\yinpin.mp3"})
thread1.start()

if __name__ == '__main__':
last_state = 0
while True:
state = getLightSwitchState()
if state == 1:
if last_state == 0:
print("1")
file = open(r"D:\1024\1024AirConditioner\temp\1.txt", "w")
file.close()
lightOn()
playSound()
thread2 = threading.Thread(target=alarmOn, name='thread2')
thread2.start()
last_state = 1
else:
if last_state == 1:
print("2")
file = open(r"D:\1024\1024AirConditioner\temp\0.txt", "w")
file.close()
lightOff()
alarmOff()
last_state = 0




中盛综合测试系统:https://zhongshengkeji.oss-cn-shenzhen.aliyuncs.com/%E5%B8%B8%E7%94%A8%E8%BD%AF%E4%BB%B6/%E6%B5%8B%E8%AF%95%E8%BD%AF%E4%BB%B6/%E4%B8%AD%E7%9B%9B%E7%BB%BC%E5%90%88%E6%B5%8B%E8%AF%95%E7%B3%BB%E7%BB%9F.zip

https://zhongshengkeji.oss-cn-shenzhen.aliyuncs.com/%E5%B8%B8%E7%94%A8%E8%BD%AF%E4%BB%B6/%E9%80%9A%E7%94%A8%E5%B7%A5%E5%85%B7/Microsoft%20.NET%20Framework%204.5.2.zip

 
posted @ 2023-10-12 15:21  suphgcm  阅读(13)  评论(0编辑  收藏  举报