简体   繁体   中英

How to see the error frames of a CAN network with Python-CAN

I wrote the program below,

can = CAN("can0", bitrate=50000, listen_only=True, error_reporting=True)
while True:
        msg = can.recv()
        print "Msg: ", msg

But it only displays the standard S or Extended X flags even though when I run the command in Terminal to check the network activity , I can see that the error counter is increasing.

import can
import CAN
import time
import logging
#logging.basicConfig(level=logging.DEBUG)

print("Initializing Listener")
can1 = CAN('can0', bitrate=500000, listen_only=True, err_reporting=True)
#print "Bus is : ", can1.bus.get_can_bus()
can1.bus.set_filters(can_filters=[{"can_mask":0x7FF, "can_id":0x00000000, "extended":False}])
CAN_ERR_FLAG = 0x20000000

while 1:
   msg = can1.recv()
   if (msg.arbitration_id & CAN_ERR_FLAG) == CAN_ERR_FLAG:
        print "Can Error Caught"
   elif msg.is_error_frame:
        print "Finally Error Frame"

How can I read the error-frames of the CAN-bus ? Things work fine when I use commnad candump -e any,0:0,#FFFFFFFF

Use Python - 3

import binascii

channel_name = "vcan0"
socketID = socket.socket(socket.AF_CAN, socket.SOCK_RAW, socket.CAN_RAW)

# socketID.setsockopt(socket.SOL_CAN_RAW, socket.CAN_RAW_ERR_FILTER, 0x1FFFFFFF)

error = socketID.bind((channel_name,))
print(binascii.hexlify(socketID.recv(32)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM