上位机程序问题记录:
启动定时器后台一直在发送can报文,上位机界面会卡死,报”无响应“。
原造成上位机界面卡死的程序代码:
def loop_func(self, func, second):
# 每隔second秒执行func函数
while True:
timer = Timer(second, func)
timer.start()
scr_frame.send_working_msg(self.working_can_device, self.working_can_channel)
timer.join()
def send_working_msg(self, event):
self.working_can_device = scr_frame.channel_working_msg_dict["device_type"]
self.working_can_channel = scr_frame.channel_working_msg_dict["device_idx"]
self.loop_func(scr_frame.frame_config_10ms, 0.01)
使用线程处理定时报文发送成功解决了界面卡死问题,程序代码如下:
class SendMsgThread(Thread):
def __init__(self, tick_10ms_func, working_can_device, working_can_channel):
Thread.__init__(self)
self.tick_10ms_func = tick_10ms_func
self.working_can_device = working_can_device
self.working_can_channel = working_can_channel
def run(self):
# 每隔second秒执行func函数
while True:
timer = Timer(0.01, self.tick_10ms_func)
timer.start()
scr_frame.send_working_msg(self.working_can_device, self.working_can_channel)
timer.join()
def send_working_msg(self, event):
self.working_can_device = scr_frame.channel_working_msg_dict["device_type"]
self.working_can_channel = scr_frame.channel_working_msg_dict["device_idx"]
submit_thread = SendMsgThread(scr_frame.frame_config_10ms, self.working_can_device, self.working_can_channel)
submit_thread.start()
参考文章: