Esempio n. 1
0
def send_j1939():
    logger.debug("send_j1939, debug")
    logger.info("send_j1939, info")
    bus = j1939.Bus(channel='can0', bustype='socketcan',timeout=0.1)

    node1 = j1939.Node(bus, j1939.NodeName(0), [0x48])
    node2 = j1939.Node(bus, j1939.NodeName(0), [0x52])

    bus.connect(node1)
    bus.connect(node2)

    pgn_get = j1939.PGN(reserved_flag=False, data_page_flag=False, pdu_format=0xd9, pdu_specific=17)
    data = [0x10, 0x13, 0x11, 0x00, 0x00, 0xe9, 0xff, 0xff]

    node1.send_parameter_group(pgn_get, data, destination_device_name=19)

    if 0:
        pgn = j1939.PGN(reserved_flag=True,
                        pdu_specific=j1939.DESTINATION_ADDRESS_GLOBAL)

        arbitration_id = j1939.ArbitrationID(pgn=pgn, source_address=my_address)

        msg = j1939.PDU(arbitration_id=arbitration_id,
                        data=[0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 0x80])

    node1.start_address_claim()

    bus.flush_tx_buffer()
    bus.shutdown()
Esempio n. 2
0
def send_pgn(requested_pgn,
             data,
             channel='can0',
             speed=250,
             bustype='socketcan',
             length=4,
             src=0,
             dest=0x17,
             bus=None,
             timeout=10):
    countdown = timeout
    result = None

    keygetFunction = None
    if speed == 250:
        keygetFunction = security250.SeedToKey
    if speed == 500:
        keygetFunction = security500.SeedToKey

    if not isinstance(requested_pgn, int):
        raise ValueError("pgn must be an integer.")
    if bus is None:
        bus = j1939.Bus(channel=channel,
                        bustype=bustype,
                        timeout=0.01,
                        keygen=keygetFunction)
        close = True
    else:
        close = False
    pgn = j1939.PGN()
    if requested_pgn < 0xf000:
        requested_pgn |= dest
    pgn.value = requested_pgn  #0xea00 + dest # request_pgn mem-object
    aid = j1939.ArbitrationID(pgn=pgn,
                              source_address=src,
                              destination_address=dest)

    logger.info(data)
    pdu = j1939.PDU(timestamp=0.0,
                    arbitration_id=aid,
                    data=data,
                    info_strings=None)

    pdu.display_radix = 'hex'

    bus.send(pdu)
    if close:
        bus.shutdown()
    if 0:  #leaving in miller's if 0
        while countdown:
            pdu = bus.recv(timeout=1)
            if pdu and (pdu.pgn == 0xe800 or pdu.pgn == requested_pgn):
                result = list(pdu.data)
                break  # got what I was waiting for
            if pdu:
                countdown -= 1
        if not result:
            raise IOError(" no CAN response")
        return result
Esempio n. 3
0
def request_pgn(requested_pgn, channel='can0', bustype='socketcan', length=4, src=0, dest=0x17, bus=None, timeout=10):
    countdown = timeout
    result = None
    close = False

    if not isinstance(requested_pgn, int):
        raise ValueError("pgn must be an integer.")
#    if bus is None:
#        bus = j1939.Bus(channel=channel, bustype=bustype, timeout=0.01)
#        close = True
    if bus is None:
        bus = j1939.Bus(channel=channel, bustype=bustype, timeout=0.01, keygen=security.SeedToKey, broadcast=False)
        node = j1939.Node(bus, j1939.NodeName(), [src])
        bus.connect(node)
        close = True
        
    pgn = j1939.PGN()
    pgn.value = 0xea00 + dest # request_pgn mem-object
    aid = j1939.ArbitrationID(pgn=pgn, source_address=src, destination_address=dest)

    pgn0 = requested_pgn & 0xff
    pgn1 = (requested_pgn >> 8) & 0xff
    pgn2 = (requested_pgn >> 16) & 0xff

    data = [pgn0, pgn1, pgn2]
    pdu = j1939.PDU(timestamp=0.0, arbitration_id=aid, data=data, info_strings=None)

    pdu.display_radix='hex'

    bus.send(pdu)
    maxPdu = 50
    while countdown:
        pdu = bus.recv(timeout=1)
        if pdu and (pdu.pgn == 0xe800 or pdu.pgn == requested_pgn):
            result = list(pdu.data) 
            break # got what I was waiting for

        elif pdu:
            maxPdu -=1
            if maxPdu <= 0:
                raise IOError('Bus too busy')
        elif pdu is None:
            countdown -= 1
        else:
            raise Exception('WHAT HAPPENED')
    if close:
        bus.shutdown()
    if not result:
        raise IOError(" no CAN response")
    return result
Esempio n. 4
0
def get_mem_object(pointer, extension, channel='can0', bustype='socketcan', length=4, src=0, dest=0x17, bus=None, timeout=10):
    countdown = timeout
    result = None
    close = False

    if bus is None:
        bus = j1939.Bus(channel=channel, bustype=bustype, timeout=0.01, broadcast=False)
        node = j1939.Node(bus, j1939.NodeName(), [src])
        bus.connect(node)
        close = True
    pgn = j1939.PGN()
    pgn.value = 0xd900 + dest # Request a DM14 mem-object
    aid = j1939.ArbitrationID(pgn=pgn, source_address=src, destination_address=dest)

    data = [length, 0x13, pointer, 0x00, 0x00, extension, 0xff, 0xff]
    pdu = j1939.PDU(timestamp=0.0, arbitration_id=aid, data=data, info_strings=None)
    assert(pdu != None)
    pdu.display_radix='hex'
    bus.send(pdu)
    while countdown:
        pdu = bus.recv(timeout=1)
        if pdu is not None:
            logger.info(pdu)
            if pdu.pgn == 0xd700:
                value = list(pdu.data)
                length = value[0]
                if length == 1:
                    result = value[1]
                elif length == 2:
                    result = (value[2] << 8) + value[1]
                elif length == 4:
                    result = (value[4] << 24) + (value[3] << 16) + (value[2] << 8) + value[1]
                else:
                    result = value[1:]
                break # got what I was waiting for
        countdown -= 1
    if close:
        bus.shutdown()
    if result is None:
        raise IOError(" no CAN response")
    return result
Esempio n. 5
0
import time
import j1939

if __name__ == "__main__":

    # code to broadcast a DM1 message at 1 Hz
    # 18FECAFE#FFFF00000000FFFF

    channel = 'can0'
    bustype = 'socketcan'
    sourceaddr = 0xFE
    destaddr = 0xFF

    bus = j1939.Bus(channel=channel,
                    bustype=bustype,
                    timeout=0.01,
                    broadcast=False)

    data = [0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF]
    pgn = j1939.PGN()
    pgn.value = 0xFECA  # DM1
    aid = j1939.ArbitrationID(pgn=pgn,
                              source_address=sourceaddr,
                              destination_address=destaddr)
    pdu = j1939.PDU(timestamp=0.0,
                    arbitration_id=aid,
                    data=data,
                    info_strings=None)

    while True:
        bus.send(pdu)
Esempio n. 6
0
    if args.source is not None:
        for src in args.source:
            if src.startswith("0x"):
                src = int(src[2:], base=16)
            filters.append({"source": int(src)})
    if args.filter is not None:
        filters = json.load(args.filter)
        print("Loaded filters from file: ", filters)

    print("args.channel  : ", args.channel)
    print("args.interface: ", args.interface)
    print("filter PGN's  : ", args.pgn)
    print("filter source : ", args.source)
    print("filters       : ", filters)

    bus = j1939.Bus(channel=args.channel, bustype=args.interface, j1939_filters=filters, timeout=0.1)
    print("channel info  : ", bus.can_bus.channel_info)
    log_start_time = datetime.datetime.now()
    print('can.j1939 logger started on {}\n'.format(log_start_time))
    logger.info('can.j1939 logger started on {}\n'.format(log_start_time))

    try:
        for msg in bus:
            if args.hex_out:
                msg.display_radix = 'hex'
            print(msg)
            logger.info(msg)
    except KeyboardInterrupt:
        bus.shutdown()
        print()
Esempio n. 7
0
def set_mem_object(pointer,
                   extension,
                   value,
                   channel='can0',
                   bustype='socketcan',
                   length=4,
                   src=0,
                   dest=0x17,
                   bus=None,
                   timeout=10):
    countdown = timeout
    result = -1
    close = False

    # only watch for the memory object pgn's
    filt = [{'pgn': 0xd800, 'source': dest}, {'pgn': 0xd400, 'source': dest}]

    if sys.platform == 'win32':
        if bus is None:
            bus = j1939.Bus(timeout=0.01,
                            keygen=security.SeedToKey,
                            broadcast=None,
                            name='j1939StartGeneral',
                            ignoreCanSendError=True,
                            j1939_filters=filt)
            node = j1939.Node(bus, j1939.NodeName(), [src])
            bus.connect(node)
            close = True
    else:
        if bus is None:
            bus = j1939.Bus(channel=channel,
                            bustype=bustype,
                            timeout=0.01,
                            keygen=security.SeedToKey,
                            broadcast=False,
                            j1939_filters=filt)
            node = j1939.Node(bus, j1939.NodeName(), [src])
            bus.connect(node)
            close = True

    dm14data = [length, 0x15, pointer, 0x00, 0x00, extension, 0xff, 0xff]

    dm14pgn = j1939.PGN(pdu_format=0xd9, pdu_specific=dest)
    dm14aid = j1939.ArbitrationID(pgn=dm14pgn,
                                  source_address=src,
                                  destination_address=dest)
    dm14pdu = j1939.PDU(timestamp=0.0,
                        arbitration_id=dm14aid,
                        data=dm14data,
                        info_strings=None)
    dm14pdu.display_radix = 'hex'

    bus.send(dm14pdu)

    sendBuffer = []
    sendBuffer.append(length)
    for i in range(0, length):
        sendBuffer.append(0)

    logger.info("## length=%d, value=%s " % (length, value))
    if isinstance(value, int) and length < 8:
        if length < 8:
            for i in range(0, length):
                sendBuffer[i] = (value >> (8 * i)) & 0xff
        else:
            raise ValueError("Don't know how to send a %d byte integer" %
                             length)

    elif isinstance(value, list):
        for i in range(len(value)):
            sendBuffer[i + 1] = value[i]

    elif isinstance(value, str):
        assert (len(value) <= length)
        sendBuffer[0] = length + 1
        for i in range(len(value)):
            sendBuffer[i + 1] = ord(value[i])

    else:
        raise ValueError("Data type not supported.")

    logger.info("## sendBuffer=%s ", sendBuffer)

    dm16pgn = j1939.PGN(pdu_format=0xd7, pdu_specific=dest)
    dm16aid = j1939.ArbitrationID(pgn=dm16pgn,
                                  source_address=src,
                                  destination_address=dest)
    dm16pdu = j1939.PDU(timestamp=0.0, arbitration_id=dm16aid, data=sendBuffer)
    dm16pdu.display_radix = 'hex'

    logger.info("## PDU=%s ", dm16pdu)

    # Wait around for a while looking for the second proceed
    proceedCount = 0
    while countdown:
        countdown -= 1
        rcvPdu = bus.recv(2)
        if rcvPdu:
            rcvPdu.display_radix = 'hex'
            logger.debug("received PDU: %s", rcvPdu)
            if rcvPdu.pgn == 0xd800:
                if rcvPdu.data[0] == 1 and rcvPdu.data[1] == 0x11:
                    proceedCount += 1
                    if proceedCount == 2:
                        bus.send(dm16pdu)
                        logger.info('Sent %s', dm16pdu)
                elif rcvPdu.data[0] == 0 and rcvPdu.data[1] == 0x19:
                    logger.info("Value Sent")
                    result = 1
                    break
                elif rcvPdu.data[0] == 0 and rcvPdu.data[1] == 0x1B:
                    logger.info("Rejected")
                    result = 0
                    break
    if close:
        bus.shutdown()
    return result
Esempio n. 8
0
def set_mem_object(pointer,
                   extension,
                   value,
                   channel='can0',
                   bustype='socketcan',
                   length=4,
                   src=0,
                   dest=0x17,
                   speed=250,
                   bus=None,
                   timeout=10):
    countdown = timeout
    result = -1
    close = False

    logger.debug(
        "------------------------------- Set Mem Object: speed={}, security250={}, security500={}"
        .format(speed, security250, security500))

    keygetFunction = None

    if int(speed) == 250:
        keygetFunction = security250.SeedToKey
        logger.debug(
            "PI02f speed=500, keygetFunction={}".format(keygetFunction))
    elif int(speed) == 500:
        keygetFunction = security500.SeedToKey
        logger.debug(
            "PI02f speed=500, keygetFunction={}".format(keygetFunction))
    else:
        logger.debug(
            "PI02f speed=Unknown, keygetFunction={}".format(keygetFunction))
        pass

    logger.debug("------------------------------- keygetFunction={}".format(
        keygetFunction))

    # only watch for the memory object pgn's
    filt = [{'pgn': 0xd800, 'source': dest}, {'pgn': 0xd400, 'source': dest}]

    if sys.platform == 'win32':
        if bus is None:
            bus = j1939.Bus(timeout=0.01,
                            keygen=keygetFunction,
                            broadcast=None,
                            name='j1939StartGeneral',
                            ignoreCanSendError=True,
                            j1939_filters=filt)
            node = j1939.Node(bus, j1939.NodeName(), [src])
            bus.connect(node)
            close = True
    else:
        if bus is None:
            bus = j1939.Bus(channel=channel,
                            bustype=bustype,
                            timeout=0.01,
                            keygen=keygetFunction,
                            broadcast=False,
                            j1939_filters=filt)
            node = j1939.Node(bus, j1939.NodeName(), [src])
            bus.connect(node)
            close = True

    pLow = pointer & 0x0000FF
    pMid = (pointer >> 8) & 0x0000FF
    pHigh = (pointer >> 16) & 0x0000FF

    dm14data = [length, 0x15, pLow, pMid, pHigh, extension, 0xff, 0xff]

    dm14pgn = j1939.PGN(pdu_format=0xd9, pdu_specific=dest)
    dm14aid = j1939.ArbitrationID(pgn=dm14pgn,
                                  source_address=src,
                                  destination_address=dest)
    dm14pdu = j1939.PDU(timestamp=0.0,
                        arbitration_id=dm14aid,
                        data=dm14data,
                        info_strings=None)
    dm14pdu.display_radix = 'hex'

    bus.send(dm14pdu)

    sendBuffer = []
    #sendBuffer.append(length)
    #for i in range(0, length):
    #    sendBuffer.append(0)

    logger.info("---------------## length=%d, value=%s " % (length, value))
    if isinstance(value, int) and length < 8:
        logger.info("-----value")
        if length < 8:
            sendBuffer.append(length)
            for i in range(0, length):
                sendBuffer.append((value >> (8 * i)) & 0xff)
        else:
            raise ValueError("Don't know how to send a %d byte integer" %
                             length)

    elif isinstance(value, list):
        # if sending a list use the exact list elements as the data
        logger.info("-----list of {} byte(s)".format(len(value)))
        sendBuffer.append(len(value))
        for i in range(len(value)):
            logger.info("---------------## b[{}]=0x{:02x}".format(i, value[i]))
            sendBuffer.append(value[i])

    elif isinstance(value, str):
        logger.info("-----str, value={}, len(value)={}. length={}".format(
            value, len(value), length))
        assert (len(value) <= length)
        sendBuffer.append(length + 1)
        for i in range(len(value)):
            sendBuffer.append(ord(value[i]))

    else:
        raise ValueError("Data type not supported.")

    logger.info("---------------## sendBuffer=%s ", sendBuffer)

    dm16pgn = j1939.PGN(pdu_format=0xd7, pdu_specific=dest)
    dm16aid = j1939.ArbitrationID(pgn=dm16pgn,
                                  source_address=src,
                                  destination_address=dest)
    dm16pdu = j1939.PDU(timestamp=0.0, arbitration_id=dm16aid, data=sendBuffer)
    dm16pdu.display_radix = 'hex'

    logger.info("----------------## PDU=%s ", dm16pdu)

    # Wait around for a while looking for the second proceed
    while countdown:
        countdown -= 1
        rcvPdu = bus.recv(timeout=0.25)
        if rcvPdu:
            rcvPdu.display_radix = 'hex'
            logger.debug("received PDU: %s", rcvPdu)
            if rcvPdu.pgn == 0xd800:
                if rcvPdu.data[0] == 1 and rcvPdu.data[1] == 0x11:
                    if rcvPdu.data[6] == 0xff and rcvPdu.data[7] == 0xff:
                        bus.send(dm16pdu)
                        logger.info('Sent %s', dm16pdu)
                elif rcvPdu.data[0] == 0 and rcvPdu.data[1] == 0x19:
                    logger.info("Value Sent")
                    result = 1
                    break
                elif rcvPdu.data[0] == 0 and rcvPdu.data[1] == 0x1B:
                    logger.info("Rejected")
                    result = 0
                    break
    if close:
        bus.shutdown()
    return result
Esempio n. 9
0
def get_mem_object(pointer,
                   extension,
                   channel='can0',
                   bustype='socketcan',
                   length=4,
                   src=0,
                   dest=0x17,
                   bus=None,
                   speed=250,
                   timeout=10):
    logger.info("{}: begin".format(inspect.stack()[0][3]))
    countdown = timeout
    result = None
    close = False

    if bus is None:
        bus = j1939.Bus(channel=channel,
                        bustype=bustype,
                        timeout=0.01,
                        broadcast=False)
        node = j1939.Node(bus, j1939.NodeName(), [src])
        bus.connect(node)
        close = True

    pgn = j1939.PGN()
    pgn.value = 0xd900 + dest  # Request a DM14 mem-object
    aid = j1939.ArbitrationID(pgn=pgn,
                              source_address=src,
                              destination_address=dest)

    # Get the 3 bytes of the pointer and put them in the correct locations
    pointer0 = pointer & 0xff
    pointer1 = (pointer & 0xff00) >> 8
    pointer2 = (pointer & 0xff0000) >> 16

    data = [length, 0x13, pointer0, pointer1, pointer2, extension, 0xff, 0xff]
    pdu = j1939.PDU(timestamp=0.0,
                    arbitration_id=aid,
                    data=data,
                    info_strings=None)
    assert (pdu != None)
    pdu.display_radix = 'hex'
    logger.info("{}: Sending Request PDU: {}".format(inspect.stack()[0][3],
                                                     pdu))
    bus.send(pdu)

    #
    # Wait for the response
    #
    while countdown:
        pdu = bus.recv(timeout=1)
        if pdu is not None:
            logger.info("{}: Received PDU: {}".format(inspect.stack()[0][3],
                                                      pdu))
            logger.info(pdu)
            if pdu.pgn == 0xd700:
                value = list(pdu.data)
                length = value[0]
                if length == 1:
                    result = value[1]
                elif length == 2:
                    result = (value[2] << 8) + value[1]
                elif length == 4:
                    result = (value[4] << 24) + (value[3] << 16) + (
                        value[2] << 8) + value[1]
                else:
                    result = value[1:]

                #logger.info("{}: d700 received, result: {}/0x{:08x}".format(inspect.stack()[0][3], result, result))

                break  # got what I was waiting for

        countdown -= 1

    if close:
        logger.info("{}: Closing Bus".format(inspect.stack()[0][3]))
        bus.shutdown()

    if result is None:
        raise IOError(" no CAN response")

    return result
ch.setLevel(lLevel)
formatter = logging.Formatter('%(asctime)s | %(name)20s | %(threadName)20s | %(levelname)5s | %(message)s')
chformatter = logging.Formatter('%(name)25s | %(threadName)10s | %(levelname)5s | %(message)s')
fh.setFormatter(formatter)
ch.setFormatter(chformatter)
logger.addHandler(ch)
logger.addHandler(fh)


if __name__ == "__main__":

    #from can.interfaces.interface import *

    filters = [{'pgn':0xd900},{'pgn':0xd800},{'pgn':0xd700},{'pgn':0xd400}]

    bus = j1939.Bus(channel='can0', bustype='socketcan', j1939_filters=filters, timeout=0.01)
    node = j1939.Node(bus, j1939.NodeName(), [19])
    bus.connect(node)

    logger.info("bus: %s"
    node.start_address_claim()
    node.claim_address(18)

    time.sleep(5)
    
    pgn = j1939.PGN()
    pgn.value = 0xd900
    logger.info("pgn: ", pgn)
    aid = j1939.ArbitrationID(priority=7, pgn=pgn, source_address=0x19, destination_address=0x17)
    logger.info("aid: ", aid)
    if pgn.is_destination_specific: