买了个负载仪,DL24数控可调恒流放电负载,想测试间隔放电,发现没有这个功能,但是其可以通过串口进行控制,就用串口监听找到协议(单向开环的也没什么验证),任何用python控制它定时进行开关,或者进行阶梯放电
import serial
import threading
import schedule
import time
s0 = serial.Serial("COM7", 9600) # PC端
command_power = {
'switch_on': [
('B1 B2 01 01 00 B6'), #开
('B1 B2 02 05 00 B6'), #5A
],
'switch_off': [
('B1 B2 01 00 00 B6'), #关
('B1 B2 02 00 06 B6'), # 60mA
],
}
# main thread
def TXData(): #
creatCTR = threading.Thread(target=SetThread)
creatCTR.start()
def SetThread():
threadCTR = threading.Thread(target=setDL24)
threadCTR.start()
while True:
if not threadCTR.is_alive():
threadCTR = threading.Thread(target=setDL24)
threadCTR.start()
time.sleep(60)
def sendCtrOrder(ctrSta,ctrNum):
hex = bytes.fromhex(command_power['switch_on'][ctrNum])
print(time.strftime("%H:%M:%S", time.localtime()))
print('开 下发数据:', hex)
s0.write(hex)
time.sleep(1)
hex = bytes.fromhex(command_power['switch_off'][ctrNum])
print(time.strftime("%H:%M:%S", time.localtime()))
print('关 下发数据:', hex)
s0.write(hex)
def ctrSwitch1(ctrSta):
sendCtrOrder(ctrSta, 0) #0 开关; 1 幅度
def setDL24():
print('thread creat')
ctrStaOn = 'switch_on'
ctrStaOff = 'switch_off'
schedule.every(4).seconds.do(ctrSwitch1, ctrStaOn)
# schedule.every(10).seconds.do(ctrSwitch1, ctrStaOff)
while True:
schedule.run_pending()
time.sleep(1)
# 主函数
if __name__ == '__main__':
TXData()
pass
|