栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

上位机实现can报文的周期性发送(python3)

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

上位机实现can报文的周期性发送(python3)

上位机程序问题记录:
启动定时器后台一直在发送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()

参考文章:

  1. 基于Python实现简单的定时器详解
  2. python装饰器(超详实例)
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/938813.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号