Skip to content

Anay1440/pyCandapter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

pyCandapter

pyCandapter is a Python module for interfacing with the Candapter USB to CAN adapter. It uses the pySerial module to communicate with the Candapter. The python-can module is used to create and read CAN messages. The module is designed to be used with Python 3.8 and above.

Usage

  • Install required modules.

    pip install pyserial
    pip install python-can
  • Download the pyCandapter module and place it in the same directory as your Python script.

  • Import necessary modules.

    import pyCandapter
    import can
    import signal
  • Identify the COM port and baud rate of the Candapter. (This can be found through the Device Manager in Windows). Also define the CAN baud rate.

    PORT = 'COM7'
    SERIALBAUDRATE = 9600
    CANBAUDRATE = 250000
  • Create a Candapter object and open the CAN bus on the Candapter.

    candapter = pyCandapter.pyCandapter(PORT, SERIALBAUDRATE)
    candapter.openCANBus(CANBAUDRATE)
  • Set up a signal handler to close the CAN bus and serial port when the script is terminated.

    def signal_handler(sig, frame):
        candapter.closeCANBus()
        exit(0) 
    
    signal.signal(signal.SIGINT, signal_handler)
  • To receive CAN messages, use the following code.

    while True:
        message = candapter.readCANMessage()
        if message is not None:
            print(message)

    The message is a python-can message object. The documentation for the same can be found here.

  • To send a CAN message, use the following code.

    message = can.Message(arbitration_id=0x123, data=[0, 1, 2, 3, 4, 5, 6, 7], is_extended_id=False)
    candapter.sendCANMessage(message)

The full file can be found here.

Known Issues

  • If the error "Error setting baudrate" appears, it is likely that the previous connection was not closed properly. Disconnect the Candapter and reconnect it, then try again.
  • If the error "SerialException: could not open port 'COMx': PermissionError(13, 'Access is denied.', None, 5)" is encountered, it is likely that the COM port is already open in another application. Close the application and try again.
  • If the error "SerialException: could not open port 'COMx': FileNotFoundError(2, 'The system cannot find the file specified.', None, 2)" is encountered, it is likely that the COM port is not connected to the computer. Connect the Candapter and try again.
  • There is no support for reading extended CAN messages at the moment.

About

pyCandapter is a Python module for interfacing with the [Candapter](https://www.candapter.com/) USB to CAN adapter.

Topics

Resources

License

Stars

Watchers

Forks

Contributors

Languages