添加现有代码V1.6
This commit is contained in:
@@ -0,0 +1,308 @@
|
||||
# cpydaq模块使用示例
|
||||
import gc
|
||||
import pyb
|
||||
import cpydaq
|
||||
import time
|
||||
import urandom
|
||||
|
||||
CPY_PROT_CMD_HEARTBEAT = 0x01
|
||||
CPY_PROT_CMD_VERSION = 0x02
|
||||
CPY_PROT_CMD_PARAM_READ = 0x03
|
||||
CPY_PROT_CMD_PARAM_WRITE = 0x04
|
||||
CPY_PROT_CMD_SAMPLE_LEN = 0x05
|
||||
CPY_PROT_CMD_SAMPLE_DATA = 0x06
|
||||
CPY_PROT_CMD_IAP = 0x07
|
||||
CPY_PROT_CMD_BOOT_BLOCKING = 0x08
|
||||
CPY_PROT_CMD_JUMP_APP = 0x09
|
||||
CPY_PROT_CMD_RESET = 0x0A
|
||||
CPY_PROT_CMD_OTP_READ = 0x0B
|
||||
CPY_PROT_CMD_OTP_WRITE = 0x0C
|
||||
CPY_PROT_CMD_RESET_FACTORY = 0x0D
|
||||
|
||||
CPY_PROT_CMD_ACK = 0x80
|
||||
CPY_PROT_CMD_ERROR = 0xC0
|
||||
|
||||
max_packet_len = 16 * 1024
|
||||
|
||||
relay_pin = pyb.Pin(pyb.Pin.cpu.E1, pyb.Pin.OUT)
|
||||
sample_pin = pyb.Pin(pyb.Pin.cpu.E0, pyb.Pin.OUT)
|
||||
pwr_pin = pyb.Pin(pyb.Pin.cpu.C11, pyb.Pin.OUT)
|
||||
|
||||
relay_pin.value(1)
|
||||
sample_pin.value(1)
|
||||
pwr_pin.value(0)
|
||||
|
||||
def daq_set_relay(state):
|
||||
if state:
|
||||
relay_pin.value(0)
|
||||
else:
|
||||
relay_pin.value(1)
|
||||
|
||||
def daq_set_sample(state):
|
||||
if state:
|
||||
sample_pin.value(0)
|
||||
else:
|
||||
sample_pin.value(1)
|
||||
|
||||
def daq_sample_by_time(timeout=1000):
|
||||
daq_set_sample(True)
|
||||
time.sleep_ms(timeout)
|
||||
daq_set_sample(False)
|
||||
|
||||
def daq_init():
|
||||
cpydaq.init(max_packet_len)
|
||||
|
||||
def daq_check_recv(timeout):
|
||||
start_time = time.ticks_ms()
|
||||
while True:
|
||||
time.sleep_ms(2)
|
||||
response = cpydaq.check_recv()
|
||||
if response is not None:
|
||||
return response
|
||||
if time.ticks_ms() > timeout + start_time:
|
||||
return None
|
||||
|
||||
def daq_exec_cmd(cmd, data=None, timeout=1000):
|
||||
# 完全重写函数,确保不使用任何可能导致关键字参数混淆的语法
|
||||
if data is None:
|
||||
send_data = b"\x00\x00\x00\x00"
|
||||
else:
|
||||
send_data = data
|
||||
# 调用cpydaq.start_recv() - 不传递任何参数
|
||||
cpydaq.start_recv()
|
||||
# 调用cpydaq.send_packet() - 只传递位置参数
|
||||
cpydaq.send_packet(cmd, send_data)
|
||||
# 调用daq_check_recv() - 只传递位置参数
|
||||
response = daq_check_recv(timeout)
|
||||
|
||||
return response
|
||||
|
||||
def daq_read_version(timeout=100):
|
||||
response = daq_exec_cmd(CPY_PROT_CMD_VERSION, None, timeout)
|
||||
if response is None:
|
||||
return None
|
||||
else:
|
||||
recv_cmd, recv_data = response
|
||||
if recv_cmd == CPY_PROT_CMD_VERSION | CPY_PROT_CMD_ACK:
|
||||
ver = recv_data.decode()
|
||||
print(ver)
|
||||
return ver
|
||||
else:
|
||||
return None
|
||||
|
||||
def daq_reset_factory(timeout=100):
|
||||
response = daq_exec_cmd(CPY_PROT_CMD_RESET_FACTORY, None, timeout)
|
||||
return response
|
||||
|
||||
def daq_read_param(timeout=100):
|
||||
param = {}
|
||||
response = daq_exec_cmd(CPY_PROT_CMD_PARAM_READ, None, timeout)
|
||||
if response is None:
|
||||
return None
|
||||
else:
|
||||
recv_cmd, recv_data = response
|
||||
if recv_cmd == CPY_PROT_CMD_PARAM_READ | CPY_PROT_CMD_ACK:
|
||||
param["usAdcPeriod"] = int.from_bytes(recv_data[0:2], "little")
|
||||
param["usAdcinterval"] = int.from_bytes(recv_data[2:4], "little")
|
||||
param["ucUseEncoder"] = recv_data[4]
|
||||
param["ucAverageCount"] = recv_data[5]
|
||||
return param
|
||||
else:
|
||||
return None
|
||||
|
||||
def daq_read_otp(timeout=100):
|
||||
info = {}
|
||||
response = daq_exec_cmd(CPY_PROT_CMD_OTP_READ, None, timeout)
|
||||
if response is None:
|
||||
return {}
|
||||
else:
|
||||
recv_cmd, recv_data = response
|
||||
if recv_cmd == CPY_PROT_CMD_OTP_READ | CPY_PROT_CMD_ACK and len(recv_data) == 96:
|
||||
try:
|
||||
data = bytearray(recv_data)
|
||||
# print(data.hex())
|
||||
#去除0x00和0xFF
|
||||
for i in range(len(data)):
|
||||
if data[i] == 0x00 or data[i] == 0xFF:
|
||||
data[i] = 0x20
|
||||
info["model"] = data[0:32].decode().strip()
|
||||
info["date"] = data[32:64].decode().strip()
|
||||
info["sn"] = data[64:96].decode().strip()
|
||||
return info
|
||||
except:
|
||||
return {}
|
||||
else:
|
||||
return {}
|
||||
|
||||
def daq_write_otp(info, timeout=100):
|
||||
data = bytearray()
|
||||
|
||||
# 处理model字段
|
||||
model_bytes = info["model"].encode()[:32] # 最多32字节
|
||||
data.extend(model_bytes)
|
||||
data.extend(b"\x00" * (32 - len(model_bytes))) # 不足32字节用0x00填充
|
||||
|
||||
# 处理date字段
|
||||
date_bytes = info["date"].encode()[:32]
|
||||
data.extend(date_bytes)
|
||||
data.extend(b"\x00" * (32 - len(date_bytes)))
|
||||
|
||||
# 处理sn字段
|
||||
sn_bytes = info["sn"].encode()[:32]
|
||||
data.extend(sn_bytes)
|
||||
data.extend(b"\x00" * (32 - len(sn_bytes)))
|
||||
|
||||
# print(data.hex())
|
||||
response = daq_exec_cmd(CPY_PROT_CMD_OTP_WRITE, data, timeout)
|
||||
if response is None:
|
||||
return None
|
||||
else:
|
||||
recv_cmd, recv_data = response
|
||||
if recv_cmd == CPY_PROT_CMD_OTP_WRITE | CPY_PROT_CMD_ACK:
|
||||
return True
|
||||
else:
|
||||
print(f"写入OTP失败, 响应: {recv_data.decode()}")
|
||||
return False
|
||||
|
||||
def daq_write_param(param, timeout=100):
|
||||
data = bytearray()
|
||||
data.extend(param["usAdcPeriod"].to_bytes(2, "little"))
|
||||
data.extend(param["usAdcinterval"].to_bytes(2, "little"))
|
||||
data.append(param["ucUseEncoder"])
|
||||
data.append(param["ucAverageCount"])
|
||||
|
||||
#打印hex data
|
||||
print(f"发送参数:{data.hex()}")
|
||||
|
||||
response = daq_exec_cmd(CPY_PROT_CMD_PARAM_WRITE, data, timeout)
|
||||
|
||||
if response is None:
|
||||
return None
|
||||
else:
|
||||
recv_cmd = response[0]
|
||||
if recv_cmd == CPY_PROT_CMD_PARAM_WRITE | CPY_PROT_CMD_ACK:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def daq_set_default_param():
|
||||
# 创建参数字典
|
||||
param = {}
|
||||
param["usAdcPeriod"] = 1000
|
||||
param["usAdcinterval"] = 40
|
||||
param["ucUseEncoder"] = 1
|
||||
param["ucAverageCount"] = 5
|
||||
# 明确使用位置参数调用daq_write_param,不使用任何关键字参数
|
||||
return daq_write_param(param)
|
||||
|
||||
def daq_set_timer_sample_param():
|
||||
# 创建参数字典
|
||||
param = {}
|
||||
param["usAdcPeriod"] = 1000
|
||||
param["usAdcinterval"] = 40
|
||||
param["ucUseEncoder"] = 0
|
||||
param["ucAverageCount"] = 5
|
||||
# 明确使用位置参数调用daq_write_param,不使用任何关键字参数
|
||||
return daq_write_param(param)
|
||||
|
||||
def daq_otp_test():
|
||||
info = {}
|
||||
info['model']='TGX-DAQ-01-RS485'
|
||||
info['date']='2026/02/28'
|
||||
info['sn']='1001'
|
||||
daq_write_otp(info)
|
||||
info = daq_read_otp()
|
||||
print(info)
|
||||
|
||||
def daq_check_is_online():
|
||||
start_time = time.ticks_ms()
|
||||
response = daq_exec_cmd(CPY_PROT_CMD_HEARTBEAT, None, 10)
|
||||
if response is None:
|
||||
print("超时未收到响应")
|
||||
return None
|
||||
else:
|
||||
recv_cmd, recv_data = response
|
||||
if recv_cmd == CPY_PROT_CMD_HEARTBEAT | CPY_PROT_CMD_ACK:
|
||||
#recv_data[0:4]表示时间
|
||||
#recv_data[4:6]表示温度
|
||||
# tim_ms = int.from_bytes(recv_data[0:4], "little")
|
||||
temp = int.from_bytes(recv_data[4:6], "little")
|
||||
# print(f"时间: {tim_ms}ms, 温度: {temp / 10.0:.1f}°C")
|
||||
# print(f'used time {time.ticks_ms() - start_time}ms')
|
||||
return temp
|
||||
else:
|
||||
print(f"收到错误响应: 命令=0x{recv_cmd:02X}, 数据{len(recv_data)}")
|
||||
return None
|
||||
|
||||
def daq_reset():
|
||||
response = daq_exec_cmd(CPY_PROT_CMD_RESET, None, 100)
|
||||
if response is None:
|
||||
print("超时未收到响应")
|
||||
return False
|
||||
else:
|
||||
recv_cmd, recv_data = response
|
||||
if recv_cmd == CPY_PROT_CMD_RESET | CPY_PROT_CMD_ACK:
|
||||
return True
|
||||
else:
|
||||
print(f"收到错误响应: 命令=0x{recv_cmd:02X}, 数据{len(recv_data)}")
|
||||
return False
|
||||
|
||||
def daq_blocking_bootloader(block):
|
||||
data = bytearray()
|
||||
if block:
|
||||
data.append(0x01)
|
||||
else:
|
||||
data.append(0x00)
|
||||
response = daq_exec_cmd(CPY_PROT_CMD_BOOT_BLOCKING, data, 100)
|
||||
if response is None:
|
||||
return False
|
||||
else:
|
||||
recv_cmd = response[0]
|
||||
if recv_cmd == CPY_PROT_CMD_BOOT_BLOCKING | CPY_PROT_CMD_ACK:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
def daq_get_sample_len(timeout=100):
|
||||
response = daq_exec_cmd(CPY_PROT_CMD_SAMPLE_LEN, None, timeout)
|
||||
if response is None:
|
||||
return None
|
||||
else:
|
||||
recv_cmd = response[0]
|
||||
if recv_cmd == CPY_PROT_CMD_SAMPLE_LEN | CPY_PROT_CMD_ACK:
|
||||
return int.from_bytes(response[1][0:2], "little")
|
||||
else:
|
||||
return None
|
||||
|
||||
def daq_get_sample_data(timeout=100):
|
||||
response = daq_exec_cmd(CPY_PROT_CMD_SAMPLE_DATA, None, timeout)
|
||||
if response is None:
|
||||
return None
|
||||
else:
|
||||
recv_cmd = response[0]
|
||||
if recv_cmd == CPY_PROT_CMD_SAMPLE_DATA | CPY_PROT_CMD_ACK:
|
||||
return response[1]
|
||||
else:
|
||||
return None
|
||||
|
||||
#压力测试
|
||||
def daq_pressure_test():
|
||||
count = 0
|
||||
for i in range(1000):
|
||||
payload_len = i + 1
|
||||
payload = bytes([urandom.randint(0, 255) for _ in range(payload_len)])
|
||||
|
||||
response = daq_exec_cmd(CPY_PROT_CMD_HEARTBEAT, payload)
|
||||
|
||||
if response is None:
|
||||
break
|
||||
else:
|
||||
recv_cmd, recv_data = response
|
||||
#print(f"接收成功: 命令=0x{recv_cmd:02X}, 数据{len(recv_data)}")
|
||||
count += 1
|
||||
|
||||
#手动触发垃圾回收
|
||||
gc.collect()
|
||||
gc.collect()
|
||||
print(f"发送{i + 1}条心跳包 成功接收{count}条响应")
|
||||
|
||||
Reference in New Issue
Block a user