DESKTOP-2QGM7IR\Lab01 5de3c3cc90 第一次提交
2025-04-09 09:49:42 +08:00

286 lines
11 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import pickle
#NFC 中的数据解析
s_info = { "dev_state":None,
"zig_mac": None,
"hw_ver": None,
"sf_ver": None,
"mf_pn" : None,
"mf_sn" : None,
"pw_date": None,
"wakeup_times": None,
"evel_times": None,
"wave_times": None,
"bat_vol": None,
"product": None,
"zig_rss": None
}
s_conf = { 'conf_flag': None,
'wakeup_period': None,
'wave_period': None,
'zig_pid': None,
'zig_channel': None,
'zig_local_addr': None,
'zig_dest_addr': None,
'zig_power_level': None,
'zig_retry_num': None,
'zig_retry_time': None,
'acc_acq_pram': None,
'acc_acq_time': None,
'freq_pw_s1': None,
'freq_pw_e1': None,
'freq_pw_s2': None,
'freq_pw_e2': None,
'freq_pw_s3': None,
'freq_pw_e3': None,
'freq_pw_s4': None,
'freq_pw_e4': None,
'freq_pw_s5': None,
'freq_pw_e5': None,
'envel_freq_s': None,
'envel_freq_e': None,
'fail_freq1': None,
'fail_freq2': None,
'fail_freq3': None,
'fail_freq4': None,
'conf_date' : None,
'speed_freq': None
}
def senser_info_detect(data):
s_info_text = ''
s_info['dev_state'] = data[0]
s_info['zig_mac'] = array_to_hex_str(data[1:9])
s_info['hw_ver'] = data[9]/10
s_info['sf_ver'] = data[10]/10
s_info['mf_pn'] = array_num_four(data[11:15])
s_info['mf_sn'] = array_num_four(data[15:19])
s_info['pw_date'] = array_num_four(data[19:23])
s_info['wakeup_times'] = array_num_four(data[23:27])
s_info['evel_times'] = array_num_four(data[27:31])
s_info['wave_times'] = array_num_four(data[31:35])
s_info['bat_vol'] = array_num_two(data[35:37])
s_info['product'] = data[37]
s_info['zig_rss'] = data[38]
s_info_text = s_info_text + dev_state_detect(s_info['dev_state'])
s_info_text = s_info_text + "Zigbee MAC:"+s_info['zig_mac']+'\n'
s_info_text = s_info_text + "硬件版本:"+str(s_info['hw_ver'])+'\n'
s_info_text = s_info_text + "软件版本:"+str(s_info['sf_ver'])+'\n'
s_info_text = s_info_text + "生产批号:"+str(s_info['mf_pn'])+'\n'
s_info_text = s_info_text + "生产序列号:"+str(s_info['mf_sn'])+'\n'
s_info_text = s_info_text + "首次上电日期"+str(s_info['pw_date'])+'\n'
s_info_text = s_info_text + "唤醒次数:"+str(s_info['wakeup_times'])+'\n'
s_info_text = s_info_text + "特征值发送次数:"+str(s_info['evel_times'])+'\n'
s_info_text = s_info_text + "原始波形发送次数:" + str(s_info['wave_times']) + '\n'
s_info_text = s_info_text + "电池电压:"+str(s_info['bat_vol'])+'mV\n'
s_info_text = s_info_text + "设备型号:"+str(s_info['product'])+'\n'
s_info_text = s_info_text + "Zigbee RSS:"+str(s_info['zig_rss'])+'\n'
return s_info_text
def senser_conf_detect(data):
s_conf_text = '\n'
s_conf['conf_flag'] = data[0]
s_conf['wakeup_period'] = array_num_two(data[1:3]);
s_conf['wave_period'] = data[3]
s_conf['zig_pid'] = array_to_hex_str(data[4:6])
s_conf['zig_channel'] = data[6]
s_conf['zig_local_addr'] = array_to_hex_str(data[7:9])
s_conf['zig_dest_addr'] = array_to_hex_str(data[9:11])
s_conf['zig_power_level'] = data[11]
s_conf['zig_retry_num'] = data[12]
s_conf['zig_retry_time'] = data[13]
s_conf['acc_acq_pram'] = data[14]
s_conf['acc_acq_time'] = data[15]
s_conf['freq_pw_s1'] = array_num_two(data[16:18])
s_conf['freq_pw_e1'] = array_num_two(data[18:20])
s_conf['freq_pw_s2'] = array_num_two(data[20:22])
s_conf['freq_pw_e2'] = array_num_two(data[22:24])
s_conf['freq_pw_s3'] = array_num_two(data[24:26])
s_conf['freq_pw_e3'] = array_num_two(data[26:28])
s_conf['freq_pw_s4'] = array_num_two(data[28:30])
s_conf['freq_pw_e4'] = array_num_two(data[30:32])
s_conf['freq_pw_s5'] = array_num_two(data[32:34])
s_conf['freq_pw_e5'] = array_num_two(data[34:36])
s_conf['envel_freq_s'] = array_num_two(data[36:38])
s_conf['envel_freq_e'] = array_num_two(data[38:40])
s_conf['fail_freq1'] = array_num_two(data[40:42])
s_conf['fail_freq2'] = array_num_two(data[42:44])
s_conf['fail_freq3'] = array_num_two(data[44:46])
s_conf['fail_freq4'] = array_num_two(data[46:48])
s_conf['conf_date'] = array_num_two(data[48:52])
s_conf['speed_freq'] = data[52]
s_conf_text = s_conf_text + "配置标志:" + conf_state_detect(s_conf['conf_flag']) +'\n'
s_conf_text = s_conf_text + "唤醒周期:" + str(s_conf['wakeup_period']) + '\n'
s_conf_text = s_conf_text + "原始波形发送周期:" + str(s_conf['wave_period']) + '\n'
s_conf_text = s_conf_text + "Zigbee PID" + s_conf['zig_pid']+ '\n'
s_conf_text = s_conf_text + "Zigbee 信道:" + str(s_conf['zig_channel']) + '\n'
s_conf_text = s_conf_text + "Zigbee 本地地址:" + s_conf['zig_local_addr'] + '\n'
s_conf_text = s_conf_text + "Zigbee 网关地址:" + s_conf['zig_dest_addr'] + '\n'
s_conf_text = s_conf_text + "Zigbee 发射功率:" + str(s_conf['zig_power_level']) + '\n'
s_conf_text = s_conf_text + "Zigbee 重发次数:" + str(s_conf['zig_retry_num']) + '\n'
s_conf_text = s_conf_text + "Zigbee 重发间隔:" + str(s_conf['zig_retry_time']) + '\n'
s_conf_text = s_conf_text + "ACC 量程:" + acc_range_detect(s_conf['acc_acq_pram']) + '\n'
s_conf_text = s_conf_text + "ACC 采样率:" + acc_rate_detect(s_conf['acc_acq_pram']) + '\n'
s_conf_text = s_conf_text + "ACC 采样时间:" + str(s_conf['acc_acq_time']) + '\n'
s_conf_text = s_conf_text + "频带能量起始频率1" + str(s_conf['freq_pw_s1']) + '\n'
s_conf_text = s_conf_text + "频带能量截至频率1" + str(s_conf['freq_pw_e1']) + '\n'
s_conf_text = s_conf_text + "频带能量起始频率2" + str(s_conf['freq_pw_s2']) + '\n'
s_conf_text = s_conf_text + "频带能量截至频率2" + str(s_conf['freq_pw_e2']) + '\n'
s_conf_text = s_conf_text + "频带能量起始频率3" + str(s_conf['freq_pw_s3']) + '\n'
s_conf_text = s_conf_text + "频带能量截至频率3" + str(s_conf['freq_pw_e3']) + '\n'
s_conf_text = s_conf_text + "频带能量起始频率4" + str(s_conf['freq_pw_s4']) + '\n'
s_conf_text = s_conf_text + "频带能量截至频率4" + str(s_conf['freq_pw_e4']) + '\n'
s_conf_text = s_conf_text + "频带能量起始频率5" + str(s_conf['freq_pw_s5']) + '\n'
s_conf_text = s_conf_text + "频带能量截至频率5" + str(s_conf['freq_pw_e5']) + '\n'
s_conf_text = s_conf_text + "冲击能量起始频率:" + str(s_conf['envel_freq_s']) + '\n'
s_conf_text = s_conf_text + "冲击能量截至频率:" + str(s_conf['envel_freq_e']) + '\n'
s_conf_text = s_conf_text + "故障频率1" + str(s_conf['fail_freq1']) + '\n'
s_conf_text = s_conf_text + "故障频率2" + str(s_conf['fail_freq2']) + '\n'
s_conf_text = s_conf_text + "故障频率3" + str(s_conf['fail_freq3']) + '\n'
s_conf_text = s_conf_text + "故障频率4" + str(s_conf['fail_freq4']) + '\n'
s_conf_text = s_conf_text + "速度积分起始频率:" + str(s_conf['speed_freq']) + '\n'
s_conf_text = s_conf_text + "配置时间:" + str(s_conf['conf_date']) + '\n'
return s_conf_text
def acc_rate_detect(acc_parm):
acc_rate = acc_parm & 0x03
if acc_rate == 0:
acc_rate = '±3.2KHz'
elif acc_rate == 1:
acc_rate = '±6.4KHz'
elif acc_rate == 2:
acc_rate = '±12.8KHz'
elif acc_rate == 3:
acc_rate = '±25.6KHz'
return acc_rate
def acc_range_detect(acc_parm):
acc_range = (acc_parm>>2)&0x03
if acc_range == 0:
acc_range = '±8g'
elif acc_range == 1:
acc_range = '±16g'
elif acc_range == 2:
acc_range = '±32g'
elif acc_range == 3:
acc_range = '±64g'
return acc_range
def conf_state_detect(conf_state):
if conf_state==0xAA:
return "配置标志OK"
else:
return "配置标志None"
def dev_state_detect(dev_state):
s_info_text = ''
if (dev_state & 0x80):
s_info_text = s_info_text + "初始化标志OK\n"
else:
s_info_text = s_info_text + "初始化标志ERROR\n"
if (dev_state & 0x40):
s_info_text = s_info_text + "加速度传感状态OK\n"
else:
s_info_text = s_info_text + "加速度传感状态ERROR\n"
if (dev_state & 0x20):
s_info_text = s_info_text + "Zigbee模块状态OK\n"
else:
s_info_text = s_info_text + "Zigbee模块状态ERROR\n"
if (dev_state & 0x10):
s_info_text = s_info_text + "设备温度传感器状态OK\n"
else:
s_info_text = s_info_text + "设备温度传感器状态ERROR\n"
if (dev_state & 0x08):
s_info_text = s_info_text + "环境温度传感器状态OK\n"
else:
s_info_text = s_info_text + "环境温度传感器状态ERROR\n"
if (dev_state & 0x04):
s_info_text = s_info_text + "设备状态:运行\n"
else:
s_info_text = s_info_text + "设备状态:休眠\n"
return s_info_text
def array_to_hex_str(data):
uhex = ['{:02X}'.format(d) for d in data]
hex_s = ''
for u in uhex:
hex_s = hex_s + u
return hex_s
def array_num_four(data):
num = (data[0]<<24)+(data[1]<<16)+(data[2]<<8)+data[3]
return num
def array_num_two(data):
num = (data[0] << 8) + data[1]
return num
#NFC 配置信息解析
class CONF_ANALYSIS(object):
def __init__(self):
super().__init__()
self.conf_info = {"senser_info":None,
"senser_conf":None }
self.senser_info = None
self.senser_conf = None
#从配置文件中获取配置信息
def get_conf_info(self):
try:
with open("conf.dat", 'rb') as conf_file:
self.conf_info = pickle.load(conf_file)
self.senser_info = self.conf_info["senser_info"]
self.senser_conf = self.conf_info["senser_conf"]
return "获取配置信息成功"
except:
return "获取配置信息失败"
#将配置信息写入配置文件
def save_conf_info(self):
try:
with open("conf.dat", 'wb') as conf_file:
self.conf_info["senser_info"] = self.senser_info
self.conf_info["senser_conf"] = self.senser_conf
pickle.dump(self.conf_info, conf_file)
self.conf_info = pickle.load(conf_file)
return "保存配置文件成功"
except:
return "保存配置文件失败"
def senser_info_detect(self, data):
info = senser_info_detect(data)
return info
def senser_conf_detect(self, data):
conf = senser_conf_detect(data)
return conf