添加现有代码V1.6
This commit is contained in:
+229
@@ -0,0 +1,229 @@
|
||||
import gc
|
||||
import json
|
||||
import binascii
|
||||
|
||||
from daq import *
|
||||
from json_config import read_config
|
||||
from net import httpc_post_data, udp_send, udp_recv
|
||||
from webserver_daq_test import send_ws,send_ws_list
|
||||
|
||||
run_flag = False
|
||||
|
||||
daq_sample_start_time = 0
|
||||
daq_sample_len = 0
|
||||
daq_online = 0
|
||||
daq_temp = 0
|
||||
|
||||
xray_pin = pyb.Pin(pyb.Pin.cpu.C0, pyb.Pin.OUT_PP)
|
||||
xray_pin.value(0)
|
||||
|
||||
wdt_pin = pyb.Pin(pyb.Pin.cpu.E4, pyb.Pin.OUT_PP)
|
||||
wdt_pin.value(0)
|
||||
|
||||
config = read_config()
|
||||
|
||||
daq_info = {}
|
||||
|
||||
def watch_dog_feed():
|
||||
if wdt_pin.value() == 0:
|
||||
wdt_pin.value(1)
|
||||
else:
|
||||
wdt_pin.value(0)
|
||||
|
||||
def update_status():
|
||||
status = {
|
||||
'id': config['code'],
|
||||
'run': run_flag,
|
||||
'online': daq_online > 0,
|
||||
'temp': daq_temp,
|
||||
'info': daq_info
|
||||
}
|
||||
packet = json.dumps(status)
|
||||
|
||||
send_ws_list('/dev/daq/status', packet)
|
||||
# print(f'发送状态: {status}')
|
||||
|
||||
def prepare_before_scanning():
|
||||
global daq_sample_start_time
|
||||
daq_set_relay(True)
|
||||
xray_pin.value(1)
|
||||
loop_wait(1000)
|
||||
daq_sample_start_time = time.ticks_ms()
|
||||
daq_set_sample(True)
|
||||
|
||||
def sweep_after_scaning():
|
||||
global daq_sample_start_time
|
||||
|
||||
daq_set_sample(False)
|
||||
xray_pin.value(0)
|
||||
daq_sample_time = time.ticks_diff(time.ticks_ms(), daq_sample_start_time)
|
||||
loop_wait(300)
|
||||
daq_set_relay(False)
|
||||
print(f'采集完成, 耗时: {daq_sample_time}ms')
|
||||
daq_sample_start_time = 0
|
||||
|
||||
|
||||
def loop_wait(timeout):
|
||||
start_time = time.ticks_ms()
|
||||
update_time = start_time
|
||||
while True:
|
||||
if time.ticks_diff(time.ticks_ms(), start_time) > timeout:
|
||||
break
|
||||
time.sleep_ms(10)
|
||||
if time.ticks_diff(time.ticks_ms(), update_time) > 300:
|
||||
update_status()
|
||||
update_time = time.ticks_ms()
|
||||
udp_data_check()
|
||||
watch_dog_feed()
|
||||
|
||||
def read_daq():
|
||||
data_len = daq_get_sample_len()
|
||||
if data_len is None or data_len == 0:
|
||||
return None
|
||||
print(f'数据长度: {data_len}')
|
||||
recv_data = daq_get_sample_data()
|
||||
if recv_data is None:
|
||||
print("读取数据失败")
|
||||
else:
|
||||
l = len(recv_data)
|
||||
print(f"接收数据: {l/4}个点,总长{l}字节")
|
||||
return recv_data
|
||||
|
||||
def read_and_send_data():
|
||||
gc.collect()
|
||||
recv_data = read_daq()
|
||||
if recv_data is None:
|
||||
print('读取数据失败')
|
||||
return
|
||||
packet = {
|
||||
'data': binascii.b2a_base64(recv_data).decode('utf-8')
|
||||
}
|
||||
recv_data = None
|
||||
gc.collect()
|
||||
print(gc.mem_free())
|
||||
|
||||
if send_ws_list('/dev/daq/data', packet):
|
||||
print('发送数据成功')
|
||||
else:
|
||||
print('发送数据失败')
|
||||
print()
|
||||
gc.collect()
|
||||
|
||||
def udp_data_check():
|
||||
global run_flag, daq_sample_len
|
||||
__udp_cmd = udp_recv()
|
||||
if not __udp_cmd:
|
||||
return
|
||||
try:
|
||||
msg = json.loads(__udp_cmd.decode('utf-8'))
|
||||
cmd = msg['cmd']
|
||||
except:
|
||||
print('命令格式错误')
|
||||
return
|
||||
|
||||
if cmd == 'start':
|
||||
try:
|
||||
daq_sample_len = int(msg['len'])
|
||||
except:
|
||||
print('参数格式错误')
|
||||
return
|
||||
run_flag = True
|
||||
print('start cmd')
|
||||
elif cmd == 'stop':
|
||||
run_flag = False
|
||||
print('stop cmd')
|
||||
elif cmd == 'daq_set_sn':
|
||||
try:
|
||||
sn = msg['sn']
|
||||
info = {}
|
||||
info['date'] = sn.split('-')[0]
|
||||
info['sn'] = sn.split('-')[-1]
|
||||
info['model'] = config['model']
|
||||
daq_write_otp(info)
|
||||
time.sleep_ms(100)
|
||||
daq_info.update(daq_read_otp())
|
||||
except Exception as e:
|
||||
print(f'设置SN失败: {e}')
|
||||
return
|
||||
else:
|
||||
print(f"未知命令{cmd}")
|
||||
return
|
||||
|
||||
_daq_check_time = 0
|
||||
def check_daq_state():
|
||||
global daq_online, daq_temp, _daq_check_time
|
||||
if time.ticks_diff(time.ticks_ms(), _daq_check_time) < 1000:
|
||||
return
|
||||
_daq_check_time = time.ticks_ms()
|
||||
temp = daq_check_is_online()
|
||||
if temp is not None:
|
||||
daq_temp = temp
|
||||
daq_online = 2
|
||||
else:
|
||||
if daq_online > 0:
|
||||
daq_online -= 1
|
||||
|
||||
def check_sample_cmd():
|
||||
global daq_sample_start_time, run_flag
|
||||
if daq_sample_start_time == 0:
|
||||
if run_flag:
|
||||
prepare_before_scanning()
|
||||
else:
|
||||
check_daq_state()
|
||||
else:
|
||||
if not run_flag or time.ticks_diff(time.ticks_ms(), daq_sample_start_time) > daq_sample_len:
|
||||
sweep_after_scaning()
|
||||
|
||||
read_and_send_data()
|
||||
run_flag = False
|
||||
daq_sample_start_time = 0
|
||||
time.sleep_ms(1000)
|
||||
|
||||
|
||||
# 运行测试
|
||||
def daq_test_loop():
|
||||
global run_flag, daq_sample_start_time, daq_sample_len
|
||||
run_flag = False
|
||||
daq_sample_start_time = 0
|
||||
daq_sample_len = 1000
|
||||
|
||||
watch_dog_feed()
|
||||
|
||||
daq_init()
|
||||
|
||||
watch_dog_feed()
|
||||
|
||||
daq_info.update(daq_read_otp())
|
||||
daq_info.update({'param': daq_read_param()})
|
||||
daq_info['version'] = daq_read_version()
|
||||
|
||||
status_update_time = time.ticks_ms()
|
||||
|
||||
if daq_info['param']:
|
||||
if daq_info['param'].get('ucUseEncoder', -1) != 0:
|
||||
daq_set_timer_sample_param()
|
||||
daq_info.update({'param': daq_read_param()})
|
||||
|
||||
while True:
|
||||
try:
|
||||
if time.ticks_diff(time.ticks_ms(), status_update_time) > 1000:
|
||||
update_status()
|
||||
status_update_time = time.ticks_ms()
|
||||
watch_dog_feed()
|
||||
check_sample_cmd()
|
||||
loop_wait(10)
|
||||
# pyb.info()
|
||||
# print("gc collect")
|
||||
# gc.collect()
|
||||
|
||||
except KeyboardInterrupt:
|
||||
xray_pin.value(0)
|
||||
daq_set_relay(False)
|
||||
daq_set_sample(False)
|
||||
print("测试结束")
|
||||
run_flag = False
|
||||
|
||||
break
|
||||
|
||||
if __name__ == "__main__":
|
||||
daq_test_loop()
|
||||
Reference in New Issue
Block a user