Example #1
0
def setup_interfaces():
    """
    Sets up the CAN interfaces over socketCan using system messages.
    These cannot be set with pyvit or socketCan in python and must be
    taken outside to a bash script.
    """
    kill_interfaces()
    print("Setting up CAN interfaces")
    script_interfaces = """
    sudo modprobe can
    sudo modprobe can-dev
    sudo modprobe can-raw
    sudo modprobe vcan
    sudo ip link set can1 up type can bitrate 250000
    sudo ifconfig can1 down
    sudo ifconfig can1 txqueuelen 1000
    sudo ifconfig can1 up
    sudo ip link add dev vcan1 type vcan
    sudo ip link set up vcan1
    sudo ip link show can1
    sudo ip link show vcan1
    """
    os.system("bash -c '%s'" % script_interfaces)
    global dev
    dev = socketcan.SocketCanDev("can1")
    sleep(1)
Example #2
0
 def open_CAN_connection(self):
     print('Opening CAN socket')
     try:
         self.dev = socketcan.SocketCanDev("can0")
         self.dev.start()
     except:
         print('Could not open SocketCAN port')
Example #3
0
def initCAN_UDSInterface(candevice,
                         tx_id,
                         rx_id,
                         extended_id=False,
                         timeout=2):
    ''' CAN Hardware für Steuergerät tx_id aktivieren'''
    dev = socketcan.SocketCanDev(candevice)
    disp = Dispatcher(dev, single_process=False)
    uds_i = UDSInterface(disp,
                         tx_id,
                         rx_id,
                         extended_id=extended_id,
                         functional_timeout=timeout)
    disp.start()

    return disp, uds_i
Example #4
0
    def updateBitrate(self, bitrate):
        """
        Updates the bitrate of the SocketCAN interface (if possible).

        :param bitrate: The desired bitrate in bit/s
        :return: A boolean value indicating success of updating the bitrate value
        """

        # Physical CAN or virtual CAN ?
        if not self.VCAN:
            # Put interface down first so the new bitrate can be applied
            cmd = "ip link set " + self.ifaceName + " down"

            process = subprocess.Popen(cmd.split(),
                                       stdout=subprocess.PIPE,
                                       stderr=subprocess.PIPE)
            output, error = process.communicate()

            # prepare cmd for next call
            cmd = "ip link set " + self.ifaceName + \
                " up type can bitrate " + str(bitrate)

        else:
            cmd = "ip link set up " + self.ifaceName

        process = subprocess.Popen(cmd.split(),
                                   stdout=subprocess.PIPE,
                                   stderr=subprocess.PIPE)
        output, error = process.communicate()

        if output.decode("utf-8") == "" and error.decode("utf-8") == "":
            if self.VCAN:
                self.bitrate = -1
            else:
                self.bitrate = bitrate

            self.iface = socketcan.SocketCanDev(self.ifaceName)
            self.iface.start()

            return True

        else:
            CANData.logger.error(error.decode("utf-8"))
            return False
Example #5
0
    def __init__(self, ifaceName, bitrate=500000):
        """
        This method initializes the pyvit CAN interface using the passed parameters and the start() method.
        Please note the active-flag which protects the object from being deleted while being in use.

        :param ifaceName: Name of the interface as displayed by ``ifconfig -a``
        :param bitrate: Desired bitrate of the interface
        """

        self.ifaceName = ifaceName
        self.VCAN = self.checkVCAN()

        self.bitrate = bitrate if not self.VCAN else -1

        self.iface = socketcan.SocketCanDev(ifaceName)
        self.updateBitrate(bitrate)

        #: 100 ms read timeout for async reads (see :func:`readPacketAsync`)
        self.timeout = 0.1

        self.active = False
        self.iface.start()
Example #6
0
from pyvit import can
from pyvit.hw import socketcan

dev = socketcan.SocketCanDev(
    "vcan0")  # /dev/serial/by-id/usb-1a86_USB2.0-Serial-if00-port0
dev.set_bitrate(500000)
dev.start()

speed = pedal_pos = 0
while True:
    frame = dev.recv()
    if frame.arb_id == 0x257:  # SG_ UIspeed_signed257 : 12|12@1+ (0.08,-40) [-40|287.6] "KPH"  Receiver
        d1 = frame.data[2]
        d2 = frame.data[1] / 16
        speed = int((d1 * 16 + d2) * 0.08 - 40)

    if frame.arb_id == 0x118:  # SG_ PedalPosition118 : 32|8@1+ (0.4,0) [0|100] "%"  Receiver
        d3 = frame.data[4]
        pedal_pos = int(d3 * 0.4)

    info = 'Speed:{:3d} km/h   PedalPos:{:3d} %'.format(speed, pedal_pos)
    print(info, end='\r', flush=True)
Example #7
0
from pyvit import can
from pyvit.hw import socketcan

dev = socketcan.SocketCanDev("vcan0")

dev.start()

frame = can.Frame(0)
frame.data = [0] * 8

while True:
    dev.send(frame)
def init():
    global dev
    dev = socketcan.SocketCanDev(iface)
    dev.start()
    return dev
Example #9
0
from pyvit.file import jsondb
from pyvit.hw import socketcan

parser = jsondb.JsonDbParser()
b = parser.parse('examples/example_db.json')

dev = socketcan.SocketCanDev('vcan0')
dev.start()

while True:
    frame = dev.recv()
    signals = b.parse_frame(frame)
    if signals:
        for s in signals:
            print(s)