예제 #1
0
from time import sleep_ms
import struct

k = 0


def cb0(bus, reason):
    '''
  print('cb0')
  if reason == 0:
      print('pending')
  if reason == 1:
      print('full')
  if reason == 2:
      print('overflow')
  '''
    global k
    k = 1


CAN(1).deinit()
can = CAN(1)
can.init(mode=CAN.NORMAL, extframe=False, prescaler=3, sjw=1, bs1=9, bs2=4)
# Baudrate is pyb.freq() / (sjw + bs1 + bs2) / prescaler = 1Mbps
can.setfilter(bank=0, mode=CAN.MASK16, fifo=0, params=(0, 0, 0, 0))
can.rxcallback(0, cb0)
while True:
    if k == 1:
        print(struct.unpack('<L', can.recv(0)[3])[0])
        k = 0
예제 #2
0
from pyb import CAN
import pyb

# test we can correctly create by id or name
for bus in (-1, 0, 1, 2, 3, "YA", "YB", "YC"):
    try:
        CAN(bus, CAN.LOOPBACK)
        print("CAN", bus)
    except ValueError:
        print("ValueError", bus)

CAN.initfilterbanks(14)
can = CAN(1)
print(can)

can.init(CAN.LOOPBACK)
print(can)
print(can.any(0))

# Catch all filter
can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0))

can.send('abcd', 123, timeout=5000)
print(can.any(0))
print(can.recv(0))

can.send('abcd', -1, timeout=5000)
print(can.recv(0))

can.send('abcd', 0x7FF + 1, timeout=5000)
print(can.recv(0))
예제 #3
0
파일: main.py 프로젝트: jsonnet/CANLogger
class CANLogger(object):
    def __init__(self):
        # Constants and variables #

        # UART cmd to en-/disable the GPS
        self.GPS_OFF = (0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0x00, 0x00, 0x08,
                        0x00, 0x16, 0x74)
        self.GPS_ON = (0xB5, 0x62, 0x06, 0x04, 0x04, 0x00, 0x00, 0x00, 0x09,
                       0x00, 0x17, 0x76)

        self.SIM_DISABLED = False
        self.GPS_LOG_TIME = 5000  # 5s
        self.SHUTOFF_TIME = 30000  # 30s of no CAN activity
        self.TOKEN = "REDACTED"

        self.VERSION = 1.0
        if 'sd' in os.listdir('/'):
            self.PATH = '/sd/'
        else:
            self.PATH = ''
        self.CAN_FILE = open(self.PATH + 'can.log', 'a+')

        # This will hold CAN IDs to be filtered for in the can log
        self.can_filter = []
        self.allowed_users = ["610574975"]
        self.interrupt = False
        self.shutdown = False

        # Init modules #

        # GPS init
        self.gps_uart = UART(1, 9600)  # init with given baudrate
        self.gps_uart.init(9600,
                           bits=8,
                           parity=None,
                           stop=1,
                           read_buf_len=512 // 2)  # init with given parameters
        self.gps = MicropyGPS()

        # CAN init (500 MHz)
        self.can = CAN(1, CAN.NORMAL)  # recv
        self.can2 = CAN(2, CAN.NORMAL)  # send
        self.can.init(CAN.NORMAL,
                      prescaler=4,
                      sjw=1,
                      bs1=14,
                      bs2=6,
                      auto_restart=True)
        self.can2.init(CAN.NORMAL,
                       prescaler=4,
                       sjw=1,
                       bs1=14,
                       bs2=6,
                       auto_restart=True)
        self.can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0))

        # SIM800L init
        sim_uart = UART(4, 9600, timeout=1000, read_buf_len=2048 // 4)
        self.modem = Modem(sim_uart)
        self.modem.initialize()

        try:
            self.modem.connect('internet.eplus.de')
        except:
            self.SIM_DISABLED = True
            print("LOG ONLY MODE (NO GSM)")

        # Clock init
        self.rtc = RTC()
        self.rtc.wakeup(5000)  # wakeup call every 5s

        # Interrupt Flag init
        self.interrupt = False
        pyb.ExtInt('X5', pyb.ExtInt.IRQ_FALLING, pyb.Pin.PULL_UP,
                   self.incoming_call)

        # Sleep pins for GSM
        self.gsm_sleep = pyb.Pin('X6', pyb.Pin.OUT_PP)
        self.gsm_sleep.value(0)

        if not self.SIM_DISABLED:
            # Software Update
            self.ota()

            # Telegram Bot
            self.telegram = TelegramBot(token=self.TOKEN, modem=self.modem)

    # Logs input to can.log
    # args will be separated by comma and printed each time a new line
    def log(self, *args, file='can.log'):
        # With this case writing to can.log is quite a lot faster, as closing a file takes ages due to writing to fs
        # But we must ensure to close the file at some point
        if file is not 'can.log':
            with open(self.PATH + file, 'a+') as f:
                print(','.join(args), file=f)
            os.sync()
        else:
            # ensure we have an open file
            # if self.CAN_FILE.closed:  # closed does not exists, thus need workaround below
            try:
                self.CAN_FILE.read()
            except OSError:
                self.CAN_FILE = open(self.PATH + 'can.log', 'a+')
            print(','.join(args), file=self.CAN_FILE)

    # Override is working
    def ota(self):
        url = 'https://raw.githubusercontent.com/jsonnet/CANLogger/master/version'
        response = self.modem.http_request(url, 'GET')

        # If a newer version is available
        if float(response.text) > self.VERSION:
            url = 'https://raw.githubusercontent.com/jsonnet/CANLogger/master/code/main.py'
            response = self.modem.http_request(url, 'GET')
            # Override existing main file and reboot
            with open(self.PATH + 'main.py', 'w') as f:
                print(response.text, file=f)
                # Force buffer write and restart
                os.sync()
                machine.soft_reset()

    # Callback function for incoming call to initiate attack mode
    def incoming_call(self, _):
        # Hangup call
        self.modem.hangup()

        # Reactivate logger if called during sleep phase
        if self.shutdown:
            self.shutdown = False
            self.gsm_sleep.value(0)
            self.sendGPSCmd(self.GPS_ON)

        for u in self.allowed_users:
            self.telegram.send(u, 'Ready in attack mode!')

        # light up yellow to indicate attack mode
        LED(3).intensity(16)

        self.interrupt = True

    # PoC for Telegram
    def message_handler(self, messages):
        for message in messages:
            # Check permitted users
            if message['id'] not in self.allowed_users:
                continue
            if message[2] == '/start':
                self.telegram.send(
                    message[0], 'CAN Logger in attack mode, ready for you!')
            else:
                if message['text'] == "log":
                    params = message['text'].strip().split(" ")[1:]
                    # get
                    if params[0] == 'get':
                        self.telegram.sendFile(
                            message[0], open(self.PATH + 'can.log', 'rb'))
                        # with open(self.PATH + 'can.log', 'r') as f:
                        #    data = f.read()  # Okay, will print \n explicitly!
                        # self.telegram.send(message[0], data)
                        os.remove(self.PATH + 'can.log')

                    # clear
                    elif params[0] == 'clear':
                        os.remove(self.PATH + 'can.log')

                    else:
                        self.helpMessage(message)

                elif message['text'] == "replay":
                    # Find first message of id and resend x times
                    params = message['text'].strip().split(" ")[1:]
                    if len(params) < 2:
                        self.helpMessage(message)
                        continue

                    id, times = params[0:1]

                    while True:
                        can_id, _, _, can_data = self.can.recv(0)

                        if can_id == id:
                            for _ in times:
                                self.can2.send(can_data, can_id, timeout=1000)
                            self.log("sent {} from {} {} times".format(
                                can_data, can_id, times))
                            break
                elif message['text'] == "injection":
                    params = message['text'].split(" ")[1:]

                    if len(params) < 4:
                        self.helpMessage(message)
                        continue

                    can_id, can_data, times, _delay = params[0:2]
                    for _ in times:
                        self.can2.send(can_data, can_id, timeout=1000)
                        pyb.delay(_delay)
                elif message['text'] == "reply":

                    params = message['text'].strip().split(" ")[1:]
                    if len(params) < 4:
                        self.helpMessage(message)
                        continue

                    id, message, id_a, answer = params[0:3]

                    while True:
                        can_id, _, _, can_data = self.can.recv(0)

                        if can_id == id and can_data == message:
                            self.can2.send(answer, id_a, timeout=1000)
                            break
                elif message[
                        'text'] == "busoff":  # TODO WIP feature only manual at that point
                    params = message['text'].strip().split(" ")[1:]

                    if len(params) < 4:
                        self.helpMessage(message)
                        continue

                    mark_id, vic_id, payload, _delay = params[0:3]

                    self.can.setfilter(0, CAN.LIST16, 0, (
                        mark_id,
                        vic_id,
                    ))

                    # Clear buffer (maybe/hopefully)
                    for _ in range(5):
                        if not self.can.any(0):
                            break
                        self.can.recv(0)

                    count = 0
                    while count <= 5:
                        can_id, _, _, can_data = self.can.recv(0)
                        if can_id == mark_id:
                            pyb.delay(_delay)
                            self.can2.send(payload, vic_id, timeout=1000)

                        while True:
                            can_id, _, _, can_data = self.can.recv(0)
                            if can_id == vic_id and can_data != payload:
                                count = 0
                                break
                            count += 1

                    # reset filter
                    self.can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0))
                elif message['text'] == "filter":  # CAN Log Filter by ID
                    params = message['text'].strip().split(" ")[1:]

                    # add
                    if params[0] == 'add':
                        for id in params[1:]:
                            self.can_filter.append(id)
                    # remove
                    elif params[0] == 'remove':
                        for id in params[1:]:
                            self.can_filter.remove(id)
                    # clear
                    elif params[0] == 'clear':
                        self.can_filter.clear()

                    else:
                        self.helpMessage(message)

                elif message['text'] == "ota":
                    self.ota()

                elif message['text'] == "help":
                    self.helpMessage(message)

                elif message['text'] == "exit":
                    LED(3).off()
                    self.interrupt = False

                self.telegram.send(message[0], 'Executed!')

    def helpMessage(self, message):
        helpme = """
                        log get|clear - Retrieve or clear saved can data log
                        replay id freq - Replay messages of given id
                        reply id message answer - Reply to a specified message with an answer
                        injection id data freq delay - Inject given can packet into bus at a given frequency
                        busoff marker victim payload freq - Manual BUS off attack for given victim
                        filter add|remove|clear id - Set a filter for when logging
                        ota - Check and update newest version
                        help - Displays this message
                        exit - Exit this mode and return to logging                
                    """
        self.telegram.send(message[0], helpme)

    def sendGPSCmd(self, cmd):
        for i in range(len(cmd)):
            self.gps_uart.writechar(cmd[i])

    def loop(self):
        gps_time = utime.ticks_ms()

        while True:
            # Check if new messages arrived after shutdown
            if self.shutdown and not self.can.any(0):
                pyb.stop()  # soft sleep (500 uA)
                continue
            elif self.shutdown and self.can.any(0):
                self.shutdown = False
                self.gsm_sleep.value(0)
                self.sendGPSCmd(self.GPS_ON)

            # Main loop
            if not self.interrupt:
                # Free memory
                # gc.collect()
                ## Logging mode ##

                # Only log gps once a few seconds
                if utime.ticks_ms() - gps_time >= self.GPS_LOG_TIME:
                    gps_time = utime.ticks_ms()

                    # if module retrieved data: update and log
                    if self.gps_uart.any():
                        self.gps.updateall(self.gps_uart.read())
                        self.log(str(self.rtc.datetime()),
                                 self.gps.latitude_string(),
                                 self.gps.longitude_string(),
                                 self.gps.speed_string())

                # Log new incoming can messages
                try:
                    # throws OSError
                    can_id, _, _, can_data = self.can.recv(
                        0, timeout=self.SHUTOFF_TIME)
                    # Filter for CAN Log
                    if not self.can_filter or can_id in self.can_filter:
                        self.log(str(self.rtc.datetime()), str(can_id),
                                 binascii.hexlify(can_data).decode('utf-8'))

                except OSError:
                    # We timed out from can connection -> could mean car is shut down
                    self.shutdown = True
                    self.CAN_FILE.close()
                    os.sync()
                    self.gsm_sleep.value(1)
                    self.sendGPSCmd(self.GPS_OFF)
                    continue

            else:
                ## Attack mode ##
                self.CAN_FILE.close()  # Close log file first
                os.sync()
                while self.interrupt:
                    self.telegram.listen(self.message_handler)
예제 #4
0
파일: can.py 프로젝트: thebigG/pycopy
for bus in (-1, 0, 1, 3):
    try:
        CAN(bus, CAN.LOOPBACK)
        print("CAN", bus)
    except ValueError:
        print("ValueError", bus)
CAN(1).deinit()

CAN.initfilterbanks(14)
can = CAN(1)
print(can)

# Test state when de-init'd
print(can.state() == can.STOPPED)

can.init(CAN.LOOPBACK)
print(can)
print(can.any(0))

# Test state when freshly created
print(can.state() == can.ERROR_ACTIVE)

# Test that restart can be called
can.restart()

# Test info returns a sensible value
print(can.info())

# Catch all filter
can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0))
'''
description: 根据传家宝007_Elmo改写的MPY平台上的Elmo驱动
status: 未完成
issue: 报文发送失败,FIFO溢出
'''

from pyb import Pin, CAN
can = CAN(1)
can.init(mode=CAN.NORMAL, prescaler=3, sjw=1, bs1=9, bs2=4)
# Baudrate is pyb.freq() / (sjw + bs1 + bs2) / prescaler = 1Mbps
can.setfilter(bank=0, mode=CAN.MASK32, fifo=0, params=(0x0, 0x0))

COBID_NMT = 0x000
COBID_RSDO = 0x600
COBID_RPDO2 = 0x300

NMT_OPERATIONAL = 1
NMT_RESET_COMMUNICATION = 130


def NMT(elmoID, cmd):
    nmt = bytearray(2)
    nmt[0] = cmd
    nmt[1] = elmoID  # Elmo ID
    can.send(nmt, id=COBID_NMT)


def RSDO(elmoID, index, subIndex, data):
    rsdo = bytearray(8)
    rsdo[0] = 0x22
    rsdo[1] = index & 0xFF
예제 #6
0
class frc_can:
    def __init__(self, devid):
        self.can = CAN(2, CAN.NORMAL)
        # Device id [0-63]
        self.devid = devid
        # Mode of the device
        self.mode = 0
        # Configuration data
        self.config_simple_targets = 0
        self.config_line_segments = 0
        self.config_color_detect = 0
        self.config_advanced_targets = 0
        self.frame_counter = 0
        # Buffer for receiving data in our callback.
        self.read_buffer = bytearray(8)
        self.read_data = [0, 0, 0, memoryview(self.read_buffer)]

        # Initialize CAN based on which type of board we're on
        if omv.board_type() == "H7":
            print("H7 CAN Interface")
            self.can.init(CAN.NORMAL,
                          extframe=True,
                          baudrate=1000000,
                          sampling_point=75)  # 1000Kbps H7
            #self.can.init(CAN.NORMAL, extframe=True, prescaler=4,  sjw=1, bs1=8, bs2=3)
        elif omv.board_type() == "M7":
            self.can.init(CAN.NORMAL,
                          extframe=True,
                          prescaler=3,
                          sjw=1,
                          bs1=10,
                          bs2=7)  # 1000Kbps on M7
            self.can.setfilter(0, CAN.LIST32, 0, [
                self.my_arb_id(self.api_id(1, 3)),
                self.my_arb_id(self.api_id(1, 4))
            ])
            print("M7 CAN Interface")
        else:
            print("CAN INTERFACE NOT INITIALIZED!")

        self.can.restart()

    # Set config: This is reported to rio when we send config.
    def set_config(self, simple_targets, line_segments, color_detect,
                   advanced_targets):
        self.config_simple_targets = simple_targets
        self.config_line_segments = line_segments
        self.config_color_detect = color_detect
        self.config_advanced_targets = advanced_targets

    # Update counter should be called after each processed frame. Sent to Rio in heartbeat.
    def update_frame_counter(self):
        self.frame_counter = self.frame_counter + 1

    def get_frame_counter(self):
        return self.frame_counter

    # Arbitration ID helper packs FRC CAN indices into a CANBus arbitration ID
    def arbitration_id(devtype, mfr, devid, apiid):
        retval = (devtype & 0x1f) << 24
        retval = retval | ((mfr & 0xff) << 16)
        retval = retval | ((apiid & 0x3ff) << 6)
        retval = retval | (devid & 0x3f)
        return retval

    # Arbitration ID helper, assumes devtype, mfr, and instance devid.
    def my_arb_id(self, apiid):
        devtype = 10  # FRC Miscellaneous is our device type
        mfr = 173  # Team 1073 ID to avoid conflict with all COTS devices
        retval = (devtype & 0x1f) << 24
        retval = retval | ((mfr & 0xff) << 16)
        retval = retval | ((apiid & 0x3ff) << 6)
        retval = retval | (self.devid & 0x3f)
        return retval

    # Return the combined API number of an API class and index:
    def api_id(self, api_class, api_index):
        return ((api_class & 0x3f) << 4) | (api_index & 0x0f)

    # Send message to my API id: Sends from OpenMV to Rio with our address.
    def send(self, apiid, bytes):
        sendid = self.my_arb_id(apiid)
        try:
            self.can.send(bytes, sendid, timeout=33)
        except:
            print("CANbus exception.")
            self.can.restart()

    # API Class - 1:  Configuration
    # Whenever we set the mode from here we send it to the RoboRio
    def set_mode(self, mode):
        self.mode = mode
        self.send_config_data()

    # Allows us to read back mode in case Rio sets the mode.
    def get_mode(self):
        return self.mode

    # Called by filter when FIFO 0 gets a message.
    def incoming_callback_0(can, reason):
        if reason:
            print("CAN Message FIFO 0 REASON %d" % reason)
        else:
            print("CAN FIFO 0 CB: NULL REASON")

        message = can.recv(0, list=None, timeout=10)

        print("ARBID %d" % message[0])

    # Send our Config data to RoboRio
    def send_config_data(self):
        cb = bytearray(8)
        cb[0] = self.mode
        cb[2] = self.config_simple_targets
        cb[3] = self.config_line_segments
        cb[4] = self.config_color_detect
        cb[5] = self.config_advanced_targets
        self.send(self.api_id(1, 0), cb)

    # Send our camera status data to RoboRio
    def send_camera_status(self, width, height):
        cb = bytearray(8)
        cb[0] = int(width / 4)
        cb[1] = int(height / 4)
        self.send(self.api_id(1, 1), cb)

    # Called to update mode if it is changed.
    def check_mode(self):
        try:
            self.can.recv(0, self.read_data, timeout=10)
            if self.read_data[0] == self.my_arb_id(self.api_id(1, 3)):
                self.mode = self.read_data[3][0]
                print("GOT MODE: %d" % self.mode)
            return True
        except:
            return False

    # Send the RIO the heartbeat message with our mode and frame counter:
    def send_heartbeat(self):
        hb = bytearray(3)
        hb[0] = (self.mode & 0xff)
        hb[1] = (self.frame_counter & 0xff00) >> 8
        hb[2] = (self.frame_counter & 0x00ff)
        self.send(self.api_id(1, 2), hb)

    # API Class - 2: Simple Target Tracking

    # Send tracking data for a given tracking slot to RoboRio.
    def send_track_data(self, slot, cx, cy, vx, vy, ttype, qual):
        tdb = bytearray(7)
        tdb[0] = (cx & 0xff0) >> 4
        tdb[1] = (cx & 0x00f) << 4 | (cy & 0xf00) >> 8
        tdb[2] = (cy & 0x0ff)
        tdb[3] = (vx & 0xff)
        tdb[4] = (vy & 0xff)
        tdb[5] = (ttype & 0xff)
        tdb[6] = (qual & 0xff)
        self.send(self.api_id(2, slot), tdb)

    # Track is empty when quality is zero, send empty slot /w 0 quality.
    def clear_track_data(self, slot):
        # Assume fills with zero.
        tdb = bytearray(7)
        self.send(self.api_id(2, slot), tdb)

    # Line Segment Tracking API Class: 3

    # Send line segment data to a slot to RoboRio.
    def send_line_data(self, slot, x0, y0, x1, y1, ttype, qual):
        ldb = bytearray(8)
        ldb[0] = (x0 & 0xff0) >> 4
        ldb[1] = ((x0 & 0x00f) << 4) | ((y0 & 0xf00) >> 8)
        ldb[2] = (y0 & 0x0ff)
        ldb[3] = (x1 & 0xff0) >> 4
        ldb[4] = ((x1 & 0x00f) << 4) | ((y1 & 0xf00) >> 8)
        ldb[5] = (y1 & 0x0ff)
        ldb[6] = (ttype & 0xff)
        ldb[7] = (qual & 0xff)
        self.send(self.api_id(3, slot), ldb)

    # Send null, 0 quality line to clear a slot for RoboRio.
    def clear_line_data(self, slot):
        ldb = bytearray(8)
        self.send(self.api_id(3, slot), ldb)

    # Color Detection API Class: 4

    # Send the given color segmentation data to the RoboRio
    def send_color_data(self, c0, p0, c1, p1, c2, p2, c3, p3):
        cdb = bytearray(8)
        cdb[0] = c0 & 0xff
        cdb[1] = p0 & 0xff
        cdb[2] = c1 & 0xff
        cdb[3] = p1 & 0xff
        cdb[4] = c2 & 0xff
        cdb[5] = p2 & 0xff
        cdb[6] = c3 & 0xff
        cdb[7] = p3 & 0xff
        self.send(self.api_id(4, 0), cdb)

    # Send empty color data / invalid colors to RoboRio.
    def clear_color_data(self):
        cdb = bytearray(8)
        self.send(self.api_id(4, 0), cdb)

    # Advanced Target Tracking API Class: 5

    # Send advanced target tracking data to RoboRio
    def send_advanced_track_data(self,
                                 cx,
                                 cy,
                                 area,
                                 ttype,
                                 qual,
                                 skew,
                                 slot=1):
        atb = bytearray(8)
        atb[0] = (cx & 0xff0) >> 4
        atb[1] = ((cx & 0x00f) << 4) | ((cy & 0xf00) >> 8)
        atb[2] = (cy & 0x0ff)
        atb[3] = (area & 0xff00) >> 8
        atb[4] = (area & 0x00ff)
        atb[5] = (ttype & 0xff)
        atb[6] = (qual & 0xff)
        atb[7] = (skew & 0xff)
        self.send(self.api_id(5, slot), atb)

    # Send a null / 0 quality update to clear track data to RoboRio
    def clear_advanced_track_data(self, slot=1):
        atb = bytearray(8)
        self.send(self.api_id(5, slot), atb)

    #send LiDar range sensing data to the RIO using API class 6
    #r stands for range
    def send_range_data(self, r, qual):
        atb = bytearray(3)
        atb[0] = (r & 0xff00) >> 8
        atb[1] = (r & 0x00ff)
        atb[2] = (qual & 0xff)
        self.send(self.api_id(6, 1), atb)
예제 #7
0
class can_tmcl_interface(tmcl_module_interface, tmcl_host_interface):
    def __init__(self,
                 port=1,
                 data_rate=None,
                 host_id=2,
                 module_id=1,
                 debug=False,
                 can_mode=CanModeNormal()):
        del data_rate
        tmcl_module_interface.__init__(self, host_id, module_id, debug)
        tmcl_host_interface.__init__(self, host_id, module_id, debug)

        self.__silent = Pin(Pin.cpu.B10, Pin.OUT_PP)
        self.__mode = can_mode
        self.__flag_recv = False
        self.__can = None

        CAN.initfilterbanks(14)

        # PCLK1 = 42 MHz, Module_Bitrate = 1000 kBit/s
        # With prescaler = 3, bs1 = 11, bs2 = 2
        # Sample point at 85.7 %, accuracy = 100 %

        if (isinstance(self.__mode, CanModeNormal)):
            self.__silent.low()
            self.__can = CAN(port, CAN.NORMAL)
            self.__can.init(CAN.NORMAL,
                            prescaler=3,
                            bs1=11,
                            bs2=2,
                            auto_restart=True)
            self.__can.setfilter(0, CAN.LIST16, 0,
                                 (host_id, host_id, module_id, module_id))
        elif (isinstance(self.__mode, CanModeSilent)):
            self.__silent.high()
            self.__can = CAN(port, CAN.SILENT)
            self.__can.init(CAN.SILENT,
                            prescaler=3,
                            bs1=11,
                            bs2=2,
                            auto_restart=True)
            self.__can.setfilter(0, CAN.LIST16, 0,
                                 (host_id, host_id, module_id, module_id))
        elif (isinstance(self.__mode, CanModeOff)):
            raise ValueError()  # Not supported by TJA1051T/3

        self.__can.rxcallback(0, self.__callback_recv)

    def __enter__(self):
        return self

    def __exit__(self, exitType, value, traceback):
        del exitType, value, traceback
        self.close()

    def close(self):
        pass

    def data_available(self, hostID=None, moduleID=None):
        del hostID, moduleID
        return self.__can.any(0)

    def _send(self, hostID, moduleID, data):
        del hostID, moduleID

        self.__can.send(data[1:], data[0])

    def __callback_recv(self, bus, reason):
        if (reason != 0):
            pass
        self.__flag_recv = True

    def _recv(self, hostID, moduleID):
        del hostID, moduleID

        while (not (self.__flag_recv)):
            pass
        self.__flag_recv = False
        received = self.__can.recv(0, timeout=1000)
        read = struct.pack("B", received[0]) + received[3]

        return read

    def printInfo(self):
        pass

    def enableDebug(self, enable):
        self._debug = enable

    def get_mode(self):
        return self.__mode

    def get_can(self):
        return self.__can

    @staticmethod
    def supportsTMCL():
        return True

    @staticmethod
    def supportsCANopen():
        return False

    @staticmethod
    def available_ports():
        return set([2])
예제 #8
0
# test we can correctly create by id (2 handled in can2.py test)
for bus in (-1, 0, 1, 3):
    try:
        CAN(bus, CAN.LOOPBACK)
        print("CAN", bus)
    except ValueError:
        print("ValueError", bus)
CAN(1).deinit()

can = CAN(1)
print(can)

# Test state when de-init'd
print(can.state() == can.STOPPED)

can.init(CAN.LOOPBACK, num_filter_banks=14)
print(can)
print(can.any(0))

# Test state when freshly created
print(can.state() == can.ERROR_ACTIVE)

# Test that restart can be called
can.restart()

# Test info returns a sensible value
print(can.info())

# Catch all filter
can.setfilter(0, CAN.MASK16, 0, (0, 0, 0, 0))
예제 #9
0
uart = UART(1, 115200)
uart.init(115200, bits=8, parity=None, stop=1)
usb = pyb.USB_VCP()

can = CAN(1, CAN.NORMAL)

# Set CAN Frequency/Speed, in KB/s.
# Possible speed: [125,250,500,1000] KB/s
can_freq = 1000

# Set frequency for the CAN bus by setting the prescaler value.
# The baudrate (bitrate) of the CANBUS (=speed) is the frequency of the
# micropython divided by the prescaler factor
# Lower prescaler == higher CAN speed
if can_freq == 125:
    can.init(CAN.NORMAL, extframe=False, prescaler=16, sjw=1, bs1=14, bs2=6)
elif can_freq == 250:
    can.init(CAN.NORMAL, extframe=False, prescaler=8, sjw=1, bs1=14, bs2=6)
elif can_freq == 500:
    can.init(CAN.NORMAL, extframe=False, prescaler=4, sjw=1, bs1=14, bs2=6)
else:
    can.init(CAN.NORMAL, extframe=False, prescaler=2, sjw=1, bs1=14, bs2=6)

if can.any(0):
    (mid, nu0, nu1, msg) = can.recv(0)
    can.send(bytearray([0x00, 0x00, 0x00]), 0x181)

SIMULATE_STEERING = True

LOG_FILE_MODE = True