|
| 1 | +import ics |
| 2 | + |
| 3 | +enable_print_message = False |
| 4 | +enable_use_server = True |
| 5 | + |
| 6 | +# Helper Functions ########################################################## |
| 7 | +def dev_name(device): |
| 8 | + if int("AA0000", 36) < device.SerialNumber < int("ZZZZZZ", 36): |
| 9 | + return device.Name + " " + ics.base36enc(device.SerialNumber) |
| 10 | + else: |
| 11 | + return device.Name + " " + str(device.SerialNumber) |
| 12 | + |
| 13 | +def print_message(msg): |
| 14 | + if not enable_print_message: |
| 15 | + if isinstance(msg, ics.SpyMessage): |
| 16 | + print('\tArbID: {}\tData: {}'.format(hex(msg.ArbIDOrHeader), [hex(x) for x in msg.Data])) |
| 17 | + return |
| 18 | + print('\t' + str(type(msg))) |
| 19 | + for attribute in dir(msg): |
| 20 | + if attribute.startswith("_"): |
| 21 | + continue |
| 22 | + length = len(attribute) |
| 23 | + if attribute == 'data': |
| 24 | + print("\t\t{}:{}{}".format(attribute, " "*(30-length), msg.data[:msg.num_bytes])) |
| 25 | + else: |
| 26 | + value = getattr(msg, attribute) |
| 27 | + try: |
| 28 | + value = hex(value) |
| 29 | + except: |
| 30 | + pass |
| 31 | + print("\t\t{}:{}{}".format(attribute, " "*(30-length), value)) |
| 32 | + print() |
| 33 | + |
| 34 | +def open_device(index=0): |
| 35 | + device = None |
| 36 | + if enable_use_server: |
| 37 | + # ics.open_device() won't open a device if we have handles open already |
| 38 | + # so we need to find them and specify which ones to connect to. |
| 39 | + devices = ics.find_devices() |
| 40 | + print("Opening Device {} (Open Client handles: {})...".format(dev_name(devices[index]), devices[index].NumberOfClients)) |
| 41 | + ics.open_device(devices[index]) |
| 42 | + device = devices[index] |
| 43 | + else: |
| 44 | + print("Opening Device...") |
| 45 | + device = ics.open_device() |
| 46 | + print("Opened Device %s." % dev_name(device)) |
| 47 | + return device |
| 48 | + |
| 49 | +# Iso15765 Fuctions ######################################################### |
| 50 | +def transmit_iso15765_msg(device, netid=ics.NETID_HSCAN, is_canfd=False): |
| 51 | + number_of_bytes = 64 |
| 52 | + msg = ics.CmISO157652TxMessage() |
| 53 | + msg.id = 0x7E0 |
| 54 | + msg.vs_netid = netid |
| 55 | + msg.num_bytes = number_of_bytes |
| 56 | + msg.padding = 0xAA |
| 57 | + # Flow Control |
| 58 | + msg.fc_id = 0x7E8 |
| 59 | + msg.fc_id_mask = 0xFFF |
| 60 | + msg.flowControlExtendedAddress = 0xFE |
| 61 | + msg.fs_timeout = 0x10 # ms |
| 62 | + msg.fs_wait = 0x3000 # ms |
| 63 | + msg.blockSize = 0 |
| 64 | + msg.stMin = 0 |
| 65 | + # CmISO157652TxMessage.flags bitfield union isn't implemented as of 2.12, we need to do it manually. |
| 66 | + msg.flags = 0 |
| 67 | + # paddingEnable |
| 68 | + msg.flags |= (1 << 6) |
| 69 | + # CANFD: Enable + BRS |
| 70 | + if is_canfd: |
| 71 | + msg.flags |= (1 << 7) | (1 << 8) |
| 72 | + # tx_dl |
| 73 | + msg.flags |= (8 << 24) |
| 74 | + # Data |
| 75 | + msg.data = [x for x in range(number_of_bytes)] |
| 76 | + |
| 77 | + # Transmit the message |
| 78 | + print("Transmitting iso15765 message on {}...".format(dev_name(device))) |
| 79 | + ics.iso15765_transmit_message(device, netid, msg, 3000) |
| 80 | + # Wait for the messages to be transmitted, this can be calculated a lot better but works here. |
| 81 | + time.sleep((((number_of_bytes/8)*msg.fs_timeout)/1000.0)+0.5) |
| 82 | + #print_message(msg) |
| 83 | + print("Transmitted iso15765 message on {}.".format(dev_name(device))) |
| 84 | + |
| 85 | +def setup_rx_iso15765_msg(device, netid=ics.NETID_HSCAN, is_canfd=False): |
| 86 | + msg = ics.CmISO157652RxMessage() |
| 87 | + |
| 88 | + msg.id = 0x7E0 |
| 89 | + msg.vs_netid = netid |
| 90 | + msg.padding = 0xAA |
| 91 | + msg.id_mask = 0xFFF |
| 92 | + msg.fc_id = 0x7E8 |
| 93 | + msg.blockSize = 100 |
| 94 | + msg.stMin = 10 |
| 95 | + msg.cf_timeout = 1000 |
| 96 | + # CmISO157652RxMessage.flags bitfield union isn't implemented as of 2.12, we need to do it manually. |
| 97 | + msg.flags = 0 |
| 98 | + # paddingEnable |
| 99 | + msg.flags |= (1 << 6) |
| 100 | + # CANFD: Enable + BRS |
| 101 | + if is_canfd: |
| 102 | + msg.flags |= (1 << 7) | (1 << 8) |
| 103 | + |
| 104 | + print_message(msg) |
| 105 | + print("Setting up iso15765 message on {}...".format(dev_name(device))) |
| 106 | + ics.iso15765_receive_message(device, netid, msg) |
| 107 | + print("Setup iso15765 message on {}.".format(dev_name(device))) |
| 108 | + |
| 109 | + |
| 110 | +def get_iso15765_msgs(device): |
| 111 | + msgs, error_count = ics.get_messages(device) |
| 112 | + print("Received {} messages with {} errors.".format(len(msgs), error_count)) |
| 113 | + for i, m in enumerate(msgs): |
| 114 | + print('Message #{}\t'.format(i+1), end='') |
| 115 | + print_message(m) |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + import time |
| 119 | + netid = ics.NETID_HSCAN |
| 120 | + |
| 121 | + tx_device = open_device(0) |
| 122 | + rx_device = open_device(1) |
| 123 | + |
| 124 | + ics.iso15765_enable_networks(tx_device, netid) |
| 125 | + ics.iso15765_enable_networks(rx_device, netid) |
| 126 | + setup_rx_iso15765_msg(rx_device) |
| 127 | + transmit_iso15765_msg(tx_device) |
| 128 | + get_iso15765_msgs(rx_device) |
| 129 | + |
| 130 | + ics.iso15765_disable_networks(tx_device) |
| 131 | + ics.iso15765_disable_networks(rx_device) |
0 commit comments