1

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

Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122
  • "code never shows up when I run my code" What does this mean? – Lundin Aug 08 '19 at 07:03
  • I don't know much about this lib, but listen_only seems wrong if you intend to run on a live CAN bus. You need at least 2 nodes that acknowledge messages sent on the bus - otherwise you will get error frames. – Lundin Aug 08 '19 at 07:05
  • What kind of errors do you have in your network activity ? If it is only Arbitration Lost it's normal that you don't see error frames with your Python script. – Benoît Aug 08 '19 at 07:10
  • Moreover, from the [Issues](https://bitbucket.org/hardbyte/python-can/issues/29/sending-error-frames) of the python can project : "You can't send an error Frame, since it's something illegal in the CAN bus. [...] is_error_frame is normally intended only for reading purposes." – Benoît Aug 08 '19 at 07:12
  • @Benoit: when I am using `sudo candump -e 0~0:,#FFFFFFFF`. It does show me all the can error frames. But when I am trying to read the can frames using python no error frames shows up. – Rasmi Ranjan Nayak Aug 08 '19 at 21:57
  • I have updated the question – Rasmi Ranjan Nayak Aug 08 '19 at 21:58
  • From the python-can [doc](https://python-can.readthedocs.io/en/master/bus.html): "set_filters(filters=None) [...] All messages that match at least one filter are returned. If filters is None or a zero length sequence, all messages are matched." The above code is filtering everything that doesn't match id = 0 – Benoît Aug 09 '19 at 07:06
  • @Benoît what if I didn't add a filter? as in `bus.set_filters()`. I'm trying the same thing, but no error messages ever gets through to my notifier. – Swedgin Oct 15 '19 at 14:56
  • @Swedgin : If you are using Python 2 then you m,ay not get it, try using `python 3` you will see the error messages. – Rasmi Ranjan Nayak Oct 15 '19 at 15:30
  • So you got this working then? My code is a bit different as yours, since I made a class with asyncio methods to read the bus. But I do work with python3.6.8. (fwiw, my code:https://github.com/hardbyte/python-can/issues/707) – Swedgin Oct 15 '19 at 15:34
  • If yes, could you answer your own question please? Thanks – Swedgin Oct 15 '19 at 15:38

1 Answers1

1

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)))
Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122