0

The can.Logger Listener in the Python-can package does not seem to be logging data to a file.

I know there is CAN bus data because the can.Printer() Listener is functioning, it is printing the CAN traffic to the output terminal. The can.Logger Listener does seem to create the .asc or .blf file (what ever the file type is in file arg it will create that file) in the directory but it doesn't write any information to it.

I thought it could have been the mode arg but changing that from 'w' or 'a' etc doesn't seem to make a difference.

Any idea why this maybe happening?

I'm using Python 3.8.3, PyCharm 2020.1.2 Community Edition, Windows 10

def bus_log():
    bus = can.interface.Bus(bustype='pcan', channel='PCAN_USBBUS1', bitrate=500000)
    logger = can.Logger('bus_log.asc', 'w')
    notifier = can.Notifier(bus, [can.Printer(), logger])

    try:
        while True:
            time.sleep(0.001)
    except KeyboardInterrupt:
        bus.shutdown()
        notifier.stop()


if __name__ == "__main__":
    bus_log()

EDIT 1: After rearranging the code and some trial and error the Try-Except block seems to be the issue. I still don't understand why it was not working. But I suspect it has something to do with PyCharm's keyboard interrupt feature. In my origional code I expect the KeyboardInterrupt to occur and when it does I close out the bus and notifier. However my theory is that this is not happening so my file is not closing. Here is my new code that works:

def bus_log():
    bus = can.interface.Bus(bustype='pcan', channel='PCAN_USBBUS1', bitrate=500000)
    logger = can.Logger('bus_log.asc', 'a')
    notifier = can.Notifier(bus, [can.Printer(), logger])

    time_1 = time.perf_counter()
    while True:
        time_2 = time.perf_counter()
        if time_2 >= time_1 + 5:
            notifier.stop()
            bus.shutdown()
        return


if __name__ == "__main__":
    bus_log()
Tim51
  • 141
  • 3
  • 13
  • Update: I've been re-arranging the Try Except block and my issue seems to be stemming from it. However I am unable to consistently get the log. I got it twice and then no dice. I think the log is not closing after the keyboardinterrupt. So maybe the .stop() isn't being called for the notifier. – Tim51 Jun 23 '20 at 01:37

1 Answers1

0

The issue stems from the fact Pycharm does not send a KeyboardInterrupt (CTRL - C) command when the Stop button is selected, the script just stops. Also (CTRL-F2) aka the Stop button is not the same as CTRL-C. This may seem obvious now but escaped me for the better half of the day.

Solution:

Navigate to Run > Edit Configurations > Select Emulate terminal in output Console

From there the KeyboardInterrupt feature (CTRL-C) will work. From then on my script will properly terminate the Notifier, Listener and bus, thereby saving the log file.

Tim51
  • 141
  • 3
  • 13