python-3.x extended_id和socketcan_native的问题

q0qdq0h2  于 2023-01-22  发布在  Python
关注(0)|答案(1)|浏览(69)

早上好,我正在使用git hub示例作为基础,通过pican 2 duo can建立一个can总线连接,python can配置和pican 2驱动程序安装正确问题出现在运行代码时,因为运行程序几秒钟后,我得到extended_id和socketcan_native错误,它显示如下:“init()得到了一个意外的关键字参数'is_extended_id'”。我正在查看评论,它对其中的几个起作用了,有几个有同样的问题。如果有人有同样的问题或者已经解决了,你能指导我吗?我正在使用python can 4. 1. 0。
并试图通过下载旧版本的python can来修复它,但当输入命令时:sudo apt-get update sudo apt-get upgrade自动升级我到python的最新版本.我必须发送这些命令才能使用pican 2 duo can卡.我留下我正在使用https://github.com/skpang/PiCAN-Python-examples/blob/master/obdii_logger.py的url

#!/usr/bin/python3
#
## obdii_logger.py
# 
# This python3 program sends out OBDII request then logs the reply to the sd card.
# For use with PiCAN boards on the Raspberry Pi
# http://skpang.co.uk/catalog/pican2-canbus-board-for-raspberry-pi-2-p-1475.html
#
# Make sure Python-CAN is installed first http://skpang.co.uk/blog/archives/1220
#
#  24-08-16 SK Pang
#

import RPi.GPIO as GPIO
import can
import time
import os
import queue
from threading import Thread

led = 22
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(led,GPIO.OUT)
GPIO.output(led,True)

# For a list of PIDs visit https://en.wikipedia.org/wiki/OBD-II_PIDs
ENGINE_COOLANT_TEMP = 0x05
ENGINE_RPM          = 0x0C
VEHICLE_SPEED       = 0x0D
MAF_SENSOR          = 0x10
O2_VOLTAGE          = 0x14
THROTTLE            = 0x11

PID_REQUEST         = 0x7DF
PID_REPLY           = 0x7E8

outfile = open('log.txt','w')

print('\n\rCAN Rx test')
print('Bring up CAN0....')

# Bring up can0 interface at 500kbps
os.system("sudo /sbin/ip link set can0 up type can bitrate 500000")
time.sleep(0.1) 
print('Ready')

try:
    bus = can.interface.Bus(channel='can0', bustype='socketcan_native')
except OSError:
    print('Cannot find PiCAN board.')
    GPIO.output(led,False)
    exit()

def can_rx_task():  # Receive thread
    while True:
        message = bus.recv()
        if message.arbitration_id == PID_REPLY:
            q.put(message)          # Put message into queue

def can_tx_task():  # Transmit thread
    while True:

        GPIO.output(led,True)
        # Sent a Engine coolant temperature request
        msg = can.Message(arbitration_id=PID_REQUEST,data=[0x02,0x01,ENGINE_COOLANT_TEMP,0x00,0x00,0x00,0x00,0x00],extended_id=False)
        bus.send(msg)
        time.sleep(0.05)

        # Sent a Engine RPM request
        msg = can.Message(arbitration_id=PID_REQUEST,data=[0x02,0x01,ENGINE_RPM,0x00,0x00,0x00,0x00,0x00],extended_id=False)
        bus.send(msg)
        time.sleep(0.05)

        # Sent a Vehicle speed  request
        msg = can.Message(arbitration_id=PID_REQUEST,data=[0x02,0x01,VEHICLE_SPEED,0x00,0x00,0x00,0x00,0x00],extended_id=False)
        bus.send(msg)
        time.sleep(0.05)        

        # Sent a Throttle position request
        msg = can.Message(arbitration_id=PID_REQUEST,data=[0x02,0x01,THROTTLE,0x00,0x00,0x00,0x00,0x00],extended_id=False)
        bus.send(msg)
        time.sleep(0.05)
        
        GPIO.output(led,False)
        time.sleep(0.1)
                        
                        
q = queue.Queue()
rx = Thread(target = can_rx_task)  
rx.start()
tx = Thread(target = can_tx_task)
tx.start()

temperature = 0
rpm = 0
speed = 0
throttle = 0
c = ''
count = 0

# Main loop
try:
    while True:
        for i in range(4):
            while(q.empty() == True):   # Wait until there is a message
                pass
            message = q.get()

            c = '{0:f},{1:d},'.format(message.timestamp,count)
            if message.arbitration_id == PID_REPLY and message.data[2] == ENGINE_COOLANT_TEMP:
                temperature = message.data[3] - 40          #Convert data into temperature in degree C

            if message.arbitration_id == PID_REPLY and message.data[2] == ENGINE_RPM:
                rpm = round(((message.data[3]*256) + message.data[4])/4)    # Convert data to RPM

            if message.arbitration_id == PID_REPLY and message.data[2] == VEHICLE_SPEED:
                speed = message.data[3]                                     # Convert data to km

            if message.arbitration_id == PID_REPLY and message.data[2] == THROTTLE:
                throttle = round((message.data[3]*100)/255)                 # Conver data to %

        c += '{0:d},{1:d},{2:d},{3:d}'.format(temperature,rpm,speed,throttle)
        print('\r {} '.format(c))
        print(c,file = outfile) # Save data to file
        count += 1
            

 
    
except KeyboardInterrupt:
    #Catch keyboard interrupt
    GPIO.output(led,False)
    outfile.close()     # Close logger file
    os.system("sudo /sbin/ip link set can0 down")
    print('\n\rKeyboard interrtupt')
3yhwsihp

3yhwsihp1#

我通过实验发现,将'socketcan-native'更改为'socketcan'可以让我克服这个错误。总线= can.接口.总线(通道= 'can 0',总线类型= 'socketcan')
can.interface.Bus()调用在我使用以下命令时有效:消息= CAN。消息(仲裁标识符= 0x 111,数据=[0、1、2、3、4、5、6、7],扩展标识符=假)
当我得到示例代码时,它没有:is_exteneded_id而不是它具有:extended_id我注意到您的代码中有:扩展标识

相关问题