You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.4 KiB
Python

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import os
import datetime
import can #需要安装python-can库 pip install python-can
from can import bus
speed=0
def sped():
global speed
bus.set_filters=[{"can_id": 0x4f2, "can_mask": 0xFFFF, "extended": False}]
#连接can1
can1 = can.interface.Bus(channel = 'can0', bustype = 'socketcan',can_filters=bus.set_filters) #python-can 4.0需要用这个
# can1 = can.interface.Bus(channel = 'can0', bustype = 'socketcan') #python-can 4.0需要用这个
#一直循环接收数据
a = 1
while a == 1 :
msg = can1.recv(3.0) #recv()中定义超时接收时间
# print(msg)
b = str(msg).replace(' ', '')
c = b.find('ID')
d = b.find('DL')
e = b[c + 3:c + 7] # id
# print(b, '\n')
if e == '04f2':
# print('成功接收到速度报文04f2 接收时间为:',datetime.datetime.now())
speed16 = b[d + 6:d + 10]
# print('接收到的速度16进制为', speed16) # 速度
speed10 = int(speed16, 16)
speed = speed10 * 0.01
# print('接收到的速度10进制为', speed10)
# print('接收到的表显速度为:%skm/h' % speed, '\n')
# file = open('speed.txt', 'a')
# file.write(str(speed))
# file.write('\n')
else:
print('未接收到速度报文')
#关闭can
# os.system('sudo ifconfig can1 down')