D
D
Da3z1e2018-01-04 16:47:15
Python
Da3z1e, 2018-01-04 16:47:15

What is the problem with connecting CAN to Raspberry Pi 2?

How to connect to the 2515 module via can-bus in Python?
Here is the code:

from __future__ import print_function
import can

bus = can.interface.Bus(channel='can0', bustype='socketcan_native') #согласование модуля и Raspberry
bitrate = 100000

def send_one():
    #bus = can.interface.Bus()
    msg = can.Message(arbitration_id=0,
                      data=[67, 10, 100, 0, 0, 0, 0, 0],
                      extended_id=False)
    try:
        bus.send(msg)
        print("Message sent on {}".format(bus.channel_info))
        
    except can.CanError:
        print("Message NOT sent")
        
def main():
    
    while True:
        os.system(
        "sudo /sbin/ip link set can0 up type can bitrate {0}".format(bitrate)
        ) #настройка CAN через консоль Linux

        try:
            #recv_begin()
            send_one()
            
        except can.CanError:
            print("Don't work")
            
if __name__ == "__main__":
    main()

The sending device is a Raspberry Pi 2. I'm using the python-can package, but I'm getting errors:
Traceback (most recent call last):
  File "/home/pi/Desktop/CAN/CAN.py", line 4, in <module>
    bus = can.interface.Bus(channel='can0', bustype='socketcan_native') #согласование модуля и Raspberry
  File "/usr/local/lib/python3.4/dist-packages/python_can-1.4.1-py3.4.egg/can/interfaces/interface.py", line 54, in __new__
    return cls(channel, **kwargs)
  File "/usr/local/lib/python3.4/dist-packages/python_can-1.4.1-py3.4.egg/can/interfaces/socketcan_native.py", line 296, in __init__
    bindSocket(self.socket, channel)
  File "/usr/local/lib/python3.4/dist-packages/python_can-1.4.1-py3.4.egg/can/interfaces/socketcan_native.py", line 200, in bindSocket
    sock.bind((channel,))
OSError: [Errno 19] No such device

Previously, everything worked
. Did the library change?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question