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()