0

Using python-can library here. Here's a simple code to print out can messages:

from can.interface import Bus

bus = Bus(bustype='pcan', channel='PCAN_USBBUS1', bitrate=500000)
for msg in bus:
    print(msg)

It runs just fine until I stop the program (running this in Pycharm IDE) and try to run it again. It will show this error:

can.interfaces.pcan.pcan.PcanError: A PCAN Channel has not been initialized yet or the initialization process has failed

I can't figure out why that is. It's fixed by 'restarting' the program, either by reinserting the CAN Dongle or by restarting Pycharm. Is it because the previous instance was cached somewhere? Would appreciate advice on this.

cullzie
  • 2,705
  • 2
  • 16
  • 21
miyesven
  • 13
  • 6

2 Answers2

0

If I'm not mistaken you never shut down the first instance of the bus. You must call bus.shutdown() at the end of your script. If you do not call bus.shutdown() before you run it again, you are trying to instantiate and already existing bus instance.

Tim51
  • 141
  • 3
  • 13
-1

This will happen, when your application will not close the channel with CAN_UnInitialize(), or when you break the application with a debugger. So please uninitialize the channel before you close your application, or reboot your system once.

  • 1
    No need for rebooting your system. On windows 10 it's enough to restart the python interpreter. – Swedgin Aug 05 '19 at 08:15