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

0

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

python
error-handling
embedded
can-bus
automotive
asked on Stack Overflow Aug 7, 2019 by Rasmi Ranjan Nayak • edited Aug 8, 2019 by Rasmi Ranjan Nayak

1 Answer

0

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)))
answered on Stack Overflow Oct 15, 2019 by Rasmi Ranjan Nayak

User contributions licensed under CC BY-SA 3.0