在前面CAN简易分析仪的基础上,与我在试用的瑞萨RA4M2单片机组成了一套电压检测系统。
【 硬件平台 】
1、飞凌OK3568开发板一块。
2、瑞萨RA4M2开发板一块。
3、跳线器一个。
4、CAN分析仪一个。
5、数据电源一台。
【 联网方式 】CAN
【通讯协议】
can波特率为500K
ID:001为电压值
data[0]为电压值高8位,data[1]为电压值低8位。转换公式为(data[0]<<8 + data[1]) /4096 *3.3
【具体代码】
ok3568的py文件内存为:
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
import tkinter
import time
import can
import threading
import os
from can.interface import Bus
try:
os.system("sudo ip link set can0 type can bitrate 500000")
os.system('sudo ifconfig can0 up')
except:
print("err")
can.rc['bitrate'] = '500000'
can.rc['interface']='socketcan'
can.rc['channel'] = 'can0'
bus = Bus()
top=tkinter.Tk(className='电压检测系统')
top.geometry("800x600")
#加上标签
label = tkinter.Label(top,font=('黑体',40))
label['text'] = 'OK3568 CAN 电压检测系统'
label_v= tkinter.Label(top,font=('宋体',20))
label_v['text'] = '0.00V'
def print_data(msg):
try:
str_data = ""
str_disp = ""
dlc = msg.dlc
id = msg.arbitration_id
rcv_data = msg.data
if id == 0x001:
get_v = int(msg.data[0]) * 256 + int(msg.data[1])
get_v =(float(get_v)/4096)*3.3
tr_disp_v = '%.3f' % get_v
#print(tr_disp_v)
label_v["text"] = "电压值:" + tr_disp_v + 'V'
for i in range(0, dlc):
str_data =str_data + "0x" + str(int(msg.data[i])) + " "
str_disp = "ID: " + str(id) + " DLC: " + str(dlc) + " DATA: " + str_data + "\r\n"
except Exception as e:
text.insert("end","print data err!\r\n")
text.insert("end",str_disp)
def rcv_can():
while True:
try:
msg = bus.recv(1.0)
if msg:
print(msg)
print_data(msg)
except Exception as e:
print("recv err: " + str(e))
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 Exception as e:
print("Message NOT sent: " + str(e))
#加上按钮
button = tkinter.Button(top, text="发送CAN数据",command=send_one)
text = tkinter.Text(top,width=100,height=20)
label.pack(pady=50)
button.pack(padx=50)
label_v.pack(pady=50)
text.pack(padx=50)
thread = threading.Thread(target=rcv_can)
thread.start()
#进入消息循环体
top.mainloop()
实现在界面如下:
更多回帖