コード例 #1
0
def _init_cans():
    can1 = CAN(1, CAN.NORMAL, prescaler=2, sjw=1, bs1=14, bs2=6)
    can2 = CAN(2, CAN.NORMAL, prescaler=2, sjw=1, bs1=14, bs2=6)
    can1.setfilter(0, CAN.LIST16, 0, (11, 12, 13, 14))
    can1.setfilter(1, CAN.LIST16, 1, (21, 22, 23, 24))

    can1.rxcallback(0, cb10)
    #    can1.rxcallback(1, cb11)

    #    can2.rxcallback(0, cb2)
    #    can2.rxcallback(1, cb2)

    return can1, can2
コード例 #2
0
ファイル: can.py プロジェクト: AbhinayBandaru/micropython
    if reason == 1:
        print('full')
    if reason == 2:
        print('overflow')

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


can.rxcallback(0, cb0)
can.rxcallback(1, cb1)

can.send('11111111',1, timeout=5000)
can.send('22222222',2, timeout=5000)
can.send('33333333',3, timeout=5000)
can.rxcallback(0, cb0a)
can.send('44444444',4, timeout=5000)

can.send('55555555',5, timeout=5000)
can.send('66666666',6, timeout=5000)
can.send('77777777',7, timeout=5000)
can.rxcallback(1, cb1a)
can.send('88888888',8, timeout=5000)

print(can.recv(0))
コード例 #3
0
ファイル: DT35.py プロジェクト: callmeToy/UESTC-Robocon-2018
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
コード例 #4
0
from pyb import CAN

buf = bytearray(8)
data_lst = [0, 0, 0, memoryview(buf)]


def can_cb1(bus, reason, fifo_num):
    if reason == CAN.CB_REASON_RX:
        bus.recv(fifo=fifo_num, list=data_lst)
        print(data_lst)
    if reason == CAN.CB_REASON_ERROR_WARNING:
        print('Error Warning')
    if reason == CAN.CB_REASON_ERROR_PASSIVE:
        print('Error Passive')
    if reason == CB_REASON_ERROR_BUS_OFF:
        print('Bus off')


can0 = CAN(0, mode=CAN.NORMAL, extframe=True, baudrate=500000)
can1 = CAN(1, mode=CAN.NORMAL, extframe=True, baudrate=500000)

can1.setfilter(id=0x55, fifo=10, mask=0xf0)
can0.send(data='mess 1!', id=0x50)
can1.recv(fifo=10)

can1.rxcallback(can_cb1)
can0.send(data='mess 2!', id=0x50)
コード例 #5
0
ファイル: can.py プロジェクト: thebigG/pycopy
        print("full")
    if reason == 2:
        print("overflow")


def cb1a(bus, reason):
    print("cb1a")
    if reason == 0:
        print("pending")
    if reason == 1:
        print("full")
    if reason == 2:
        print("overflow")


can.rxcallback(0, cb0)
can.rxcallback(1, cb1)

can.send("11111111", 1, timeout=5000)
can.send("22222222", 2, timeout=5000)
can.send("33333333", 3, timeout=5000)
can.rxcallback(0, cb0a)
can.send("44444444", 4, timeout=5000)

can.send("55555555", 5, timeout=5000)
can.send("66666666", 6, timeout=5000)
can.send("77777777", 7, timeout=5000)
can.rxcallback(1, cb1a)
can.send("88888888", 8, timeout=5000)

print(can.recv(0))
コード例 #6
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])
コード例 #7
0
ファイル: CAN.py プロジェクト: liudannew/NuMicroPy
import pyb
from pyb import CAN

buf = bytearray(8)
data_lst = [0, 0, 0, memoryview(buf)]
can_write = 0


def can_cb(bus, reason, fifo_num):
    if reason == CAN.CB_REASON_RX:
        bus.recv(fifo=fifo_num, list=data_lst)
        can_write = 1
        print(buf)
    if reason == CAN.CB_REASON_ERROR_WARNING:
        print('Error Warning')
    if reason == CAN.CB_REASON_ERROR_PASSIVE:
        print('Error Passive')
    if reason == CAN.CB_REASON_ERROR_BUS_OFF:
        print('Bus off')


can0 = CAN(0, mode=CAN.NORMAL, extframe=True, baudrate=500000)
can0.setfilter(id=0x12345, fifo=10, mask=0xFFFF0)
can0.rxcallback(can_cb)

while True:
    pyb.delay(500)
    if can_write == 1:
        can0.send(data='ABC', id=0x567)
        can_write = 0
コード例 #8
0

def my_handler_mainloop(reason):
    (id, isRTR, filterMatchIndex, telegram) = can.recv(0)
    print("received:", telegram)
    if telegram == b'on':
        ledBlue.on()
    else:
        ledBlue.off()


def my_canbus_interrupt(bus, reason):
    # Don't handle code in the interrupt service routine.
    # Schedule a task to be handled soon
    if reason == 0:
        # print('pending')
        micropython.schedule(my_handler_mainloop, reason)
        return
    if reason == 1:
        print('full')
        return
    if reason == 2:
        print('overflow')
        return
    print('unknown')


can.rxcallback(0, my_canbus_interrupt)

print("Now listening on these CAN-id's: ", IDs)