前面用python进行了数据的送,这篇示例如何在窗体上添加控件进行手动数据的发送:
【步骤】用tkinter创建一个button控件,button按下的回调函数为调动can发送函数,具体代码如下:
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import tkinter
import time
import can
from can.interface import Bus
# 配置CAN的参数
can.rc['bitrate'] = '500000'
can.rc['interface']='socketcan'can.rc['channel'] = 'can0'
can.rc['bitrate'] = '500000'
bus = Bus()
top=tkinter.Tk(className='飞凌OK3568CAN测试')
top.geometry("800x600")
#加上标签
label = tkinter.Label(top)
label['text'] = '电子发烧友、飞凌OK3568进行pythonCAN测试'
def send_one():
msg = can.Message(
arbitration_id=0xC0FFEE, data=[0, 25, 0, 1, 3, 1, 4, 1], is_extended_id=True
)
try:
bus.send(msg)
print(f"Message sent on {bus.channel_info}")
except can.CanError:
print("Message NOT sent")
#加上按钮
button = tkinter.Button(top, text="发送CAN数据",command=send_one)
label.pack(pady=50)
button.pack(padx=50)
#进入消息循环体
运行程序后窗休如下:
按下发送键,成功向外发送了一条数据:
更多回帖