-
Notifications
You must be signed in to change notification settings - Fork 2
/
collect_data.py
191 lines (184 loc) · 7.2 KB
/
collect_data.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
'''
Author : velvet
Date : 2020-08-07 22:35:47
LastEditTime : 2020-12-29 10:49:11
LastEditors : velvet
Description :
'''
#coding=utf-8
import serial.tools.list_ports
import time
import dicttoxml
from xml.dom.minidom import parseString
import xml.dom.minidom
'''
description: 查询参数、设置参数、收包解包
param {type}
return {type}
'''
def parse_param(datas):
"""
:param datas: str()
:return: dict()
"""
HEAD = datas[:12]
if HEAD != 'FAF501814000'.lower() and HEAD != 'FAF502814000'.lower(): # 前一个是查询,后一个是设置
print('parse_param error!!!')
return {}
return {
'HEAD': datas,
'GAIN_0': eval('0x' + datas[12:14]),
'GAIN_1': eval('0x' + datas[14:16]),
'VOL': eval('0x' + datas[16:18]),
'VOH': eval('0x' + datas[18:20]),
'M_T': eval('0x' + datas[20:28]),
'COUNT': eval('0x' + datas[28:36]),
'MODE': eval('0x' + datas[36:38]),
'SOURCE': eval('0x' + datas[38:40]),
'RESERVE': datas[40:76],
'SN': datas[76:108],
'ver': datas[108:124],
'HOUR': eval('0x' + datas[124:126]),
'MIN': eval('0x' + datas[126:128]),
'SEC': eval('0x' + datas[128:130]),
'YR': eval('0x' + datas[130:134]),
'MON': eval('0x' + datas[134:136]),
'DAY': eval('0x' + datas[136:138]),
'WD': eval('0x' + datas[138:140]),
}
'''
description: 查询数据、查询清零数据、收包接包
param {type}
return {type}
'''
def parse_signal(datas):
"""
:param datas: str()
:return: dict()
"""
HEAD = datas[:12]
# print(HEAD)
if HEAD != 'FAF501820010'.lower() and HEAD != 'FAF502820010'.lower():# 前一个是查询数据,后一个是查询清零数据
print('parse_param error!!!')
return {}
signal = []
for i in range(0,1024):
signal.append(eval('0x' + datas[12+8*i: 20+8*i]))
return {
'HEAD': HEAD,
'DATA': signal
}
'''
description: 查询数据和参数、查询数据和参数并清零数据
param: str()
return: dict()
'''
def parse_signal_and_params(datas):
HEAD = datas[:8]
# print(HEAD)
# if HEAD != 'FAF503824010'.lower() and HEAD != 'FAF504824810'.lower():# 前一个是查询数据和参数,后一个是查询数据和参数并清零数据
# print('parse_param error!!!')
# return {}
signal = []
for i in range(0,1024):
signal.append(eval('0x' + datas[12+6*i: 14+6*i] + datas[10+6*i: 12+6*i] + datas[8+6*i: 10+6*i]))
# print(datas[12+6*i: 18+6*i][::-1])
params = HEAD + datas[14+6*1024:]
return {
'HEAD': HEAD,
'DATA': signal,
# 'GAIN_0': eval('0x' + params[12:14]),
# 'GAIN_1': eval('0x' + params[14:16]),
# 'VOL': eval('0x' + params[16:18]),
# 'VOH': eval('0x' + params[18:20]),
# 'M_T': eval('0x' + params[20:28]),
# 'COUNT': eval('0x' + params[28:36]),
# 'MODE': eval('0x' + params[36:38]),
# 'SOURCE': eval('0x' + params[38:40]),
# 'RESERVE': params[40:76],
# 'SN': params[76:108],
# 'ver': params[108:124],
# 'HOUR': eval('0x' + params[124:126]),
# 'MIN': eval('0x' + params[126:128]),
# 'SEC': eval('0x' + params[128:130]),
# 'YR': eval('0x' + params[130:134]),
# 'MON': eval('0x' + params[134:136]),
# 'DAY': eval('0x' + params[136:138]),
# 'WD': eval('0x' + params[138:140]),
}
def save_file(dictt, filename="example.xml"):
xmll = dicttoxml.dicttoxml(dictt)
# print(xmll)
domm = parseString(xmll)
# print(dom.toprettyxml())
with open(filename, 'w', encoding='UTF-8') as fh:
domm.writexml(fh, indent='', addindent='\t', newl='\n', encoding='UTF-8')
# print('OK')
def load_file(filename="example.xml"):
# with open(filename,'r',encoding='utf8') as fh:
dom = xml.dom.minidom.parse(filename)
root = dom.documentElement
return {
'HEAD': root.getElementsByTagName('HEAD')[0].firstChild.data,
'DATA': [int(node.firstChild.data) for node in root.getElementsByTagName('item')],
# 'GAIN_0': root.getElementsByTagName('GAIN_0')[0].firstChild.data,
# 'GAIN_1': root.getElementsByTagName('GAIN_1')[0].firstChild.data,
# 'VOL': root.getElementsByTagName('VOL')[0].firstChild.data,
# 'VOH': root.getElementsByTagName('VOH')[0].firstChild.data,
# 'M_T': root.getElementsByTagName('M_T')[0].firstChild.data,
# 'COUNT': root.getElementsByTagName('COUNT')[0].firstChild.data,
# 'MODE': root.getElementsByTagName('MODE')[0].firstChild.data,
# 'SOURCE': root.getElementsByTagName('SOURCE')[0].firstChild.data,
# 'RESERVE': root.getElementsByTagName('RESERVE')[0].firstChild.data,
# 'SN': root.getElementsByTagName('SN')[0].firstChild.data,
# 'ver': root.getElementsByTagName('ver')[0].firstChild.data,
# 'HOUR': root.getElementsByTagName('HOUR')[0].firstChild.data,
# 'MIN': root.getElementsByTagName('MIN')[0].firstChild.data,
# 'SEC': root.getElementsByTagName('SEC')[0].firstChild.data,
# 'YR': root.getElementsByTagName('YR')[0].firstChild.data,
# 'MON': root.getElementsByTagName('MON')[0].firstChild.data,
# 'DAY': root.getElementsByTagName('DAY')[0].firstChild.data,
# 'WD': root.getElementsByTagName('WD')[0].firstChild.data,
}
'''
description: 测试指令发送
param {type}
return {type}
'''
# if 0:
# bps = 115200
# time_out = 1
# port_list = list(serial.tools.list_ports.comports())
# for i in range(len(port_list)):
# print(i, '---', serial.Serial(list(port_list[i])[0], bps, timeout=time_out).name)
# COM_NUM = input('Please input an order number to choose a COM:')
# ser = serial.Serial(list(port_list[int(COM_NUM)])[0], bps, timeout=time_out)
# cmd_query_data = bytes.fromhex('fa f5 01 02 00 00 0e fe') # 查询数据
# cmd_query_param = bytes.fromhex('fa f5 01 01 00 00 0f fe') # 查询参数
# cmd_enable_MCA = bytes.fromhex('fa f5 01 00 00 00 10 fe') # enable_MCA
# cmd_disable_MCA = bytes.fromhex('fa f5 02 00 00 00 0f fe') # disable_MCA
# cmd_query_data_and_clear = bytes.fromhex('fa f5 02 02 00 00 0d fe') # 查询数据并清零
# cmd_query_data_and_param = bytes.fromhex('fa f5 03 02 00 00 0c fe') # 查询数据和参数
# cmd_query_data_and_param_and_clear = bytes.fromhex('fa f5 04 02 00 00 0b fe') # 查询数据和参数并清零数据
# while True:
# # if ser.in_waiting:
# if True:
# #发送命令
# ser.write(cmd_query_data_and_param_and_clear)
# #原码
# recv = ser.read(10240).hex()
# # print(recv)
# #解包后是个字典
# parsed = parse_signal_and_params(recv)
# print(parsed)
# #字典转换成XML再保存
# save_file(parsed)
# time.sleep(1)
# ser.close() # close serial
'''
description: 测试文件读取
param {type}
return {type}
'''
# if 0:
# print(load_file())