def transmit_can(device): msg = ics.SpyMessage() msg.ArbIDOrHeader = 0x01 # CAN Arbitration ID msg.Data = (1, 2, 3, 4, 5, 6, 7, 8) # Data Bytes go here msg.NetworkID = ics.NETID_HSCAN # First channel of CAN on the device # msg parameter here can also be a tuple of messages ics.transmit_messages(device, msg)
def tx_random_message(chnl=0,msgid=None,tmfm=None): global linenm, startime, devices, msg, msgdtstr, msgcyc, pre_miltm linenm += 1 msg.NetworkID = ics.NETID_HSCAN txch = msg.NetworkID if msgid == None: msg.ArbIDOrHeader = random.randint(0,0x7FF)# message ID else: msg.ArbIDOrHeader = msgid if msgdtstr != None : msgtxstr = re.sub(r'[^0-9a-fA-F]',random.choice(string.hexdigits),msgdtstr) else: msgtxstr = ''.join(random.choice(string.hexdigits) for i in range(random.randint(0,0x07))) if len(msgtxstr) > 12: msgdt = (int('07'), int(msgtxstr[:2],16), int(msgtxstr[2:4],16), int(msgtxstr[4:6],16), int(msgtxstr[6:8],16), int(msgtxstr[8:10],16), int(msgtxstr[10:12],16), int(msgtxstr[12:],16) ) elif len(msgtxstr) > 10: msgdt = (int('06'), int(msgtxstr[:2],16), int(msgtxstr[2:4],16), int(msgtxstr[4:6],16), int(msgtxstr[6:8],16), int(msgtxstr[8:10],16), int(msgtxstr[10:],16), int('00',16) ) elif len(msgtxstr) > 8: msgdt = (int('05'), int(msgtxstr[:2],16), int(msgtxstr[2:4],16), int(msgtxstr[4:6],16), int(msgtxstr[6:8],16), int(msgtxstr[8:],16), int('00',16), int('00',16) ) elif len(msgtxstr) > 6: msgdt = (int('04'), int(msgtxstr[:2],16), int(msgtxstr[2:4],16), int(msgtxstr[4:6],16), int(msgtxstr[6:],16), int('00',16), int('00',16), int('00',16) ) elif len(msgtxstr) > 4: msgdt = (int('03'), int(msgtxstr[:2],16), int(msgtxstr[2:4],16), int(msgtxstr[4:],16), int('00',16), int('00',16), int('00',16), int('00',16) ) elif len(msgtxstr) > 2: msgdt = (int('02'), int(msgtxstr[:2],16), int(msgtxstr[2:],16), int('00',16), int('00',16), int('00',16), int('00',16), int('00',16) ) elif len(msgtxstr) > 0: msgdt = (int('01'), int(msgtxstr[0:],16), int('00',16), int('00',16), int('00',16), int('00',16), int('00',16), int('00',16) ) else: msgdt = (random.randint(0,0x07), random.randint(0,0xFF), random.randint(0,0xFF), random.randint(0,0xFF), random.randint(0,0xFF), random.randint(0,0xFF), random.randint(0,0xFF), random.randint(0,0xFF)) msg.Data = msgdt miltm = time.time() - startime rel_miltm = miltm - pre_miltm pre_miltm = miltm ics.transmit_messages(devices[chnl], msg) txid = format(msg.ArbIDOrHeader,'#04x').replace('0X','').zfill(3) txds = '[{}]'.format(' '.join(format(i, '#04x') for i in msg.Data)) txds = txds.replace('[','').replace(']','').replace('0x','') if re.search('rel',tmfm,re.IGNORECASE): print(linenm, format(rel_miltm,'.9f'), txch, 'Tx', txid, txds,flush=True) else: print(linenm, format(miltm,'.9f'), txch, 'Tx', txid, txds,flush=True)
def transmit_can(device): msg = ics.SpyMessage() msg.ArbIDOrHeader = 0xFFFFFF # CAN Arbitration ID msg.Data = (1, 2, 3, 4, 5, 6, 7, 8) # Data Bytes go here msg.NetworkID = ics.NETID_HSCAN # First channel of CAN on the device msg.NumberBytesHeader = 4 msg.StatusBitField = ics.SPY_STATUS_XTD_FRAME # msg parameter here can also be a tuple of messages ics.transmit_messages(device, msg)
def testLIN(self): message = ics.SpyMessageJ1850() message.Header = (0x3C, 1, 2) message.StatusBitField = ics.SPY_STATUS_NETWORK_MESSAGE_TYPE | \ ics.SPY_STATUS_LIN_MASTER message.Protocol = ics.SPY_PROTOCOL_LIN for network in (ics.NETID_LIN, ics.NETID_LIN2, ics.NETID_LIN3, \ ics.NETID_LIN4): message.NetworkID = network message.Data = tuple([random.randint(x, 0xFF) for x in range(6)]) # Generate the checksum checksum = 0 for byte in message.Data+message.Header[1:3]: checksum += byte if (checksum > 255): checksum -= 255 message.Data += ((~checksum & 0xFF),) self.assertTrue(ics.transmit_messages(self.dev1, message) == None) self.assertFalse(ics.get_error_messages(self.dev1)) self.assertFalse(ics.get_error_messages(self.dev2)) messages, error_count = ics.get_messages(self.dev1, False, 0.1) self.assertEqual(error_count, 0) self.assertEqual(len(messages), 1) self.assertEqual(message.Header, messages[0].Header) self.assertEqual(message.NetworkID, messages[0].NetworkID) self.assertEqual(message.Data[0:-1], messages[0].Data) self.assertFalse(ics.get_error_messages(self.dev1)) self.assertFalse(ics.get_error_messages(self.dev2))
def send(self, msg, timeout=None): if not ics.validate_hobject(self.dev): raise CanError("bus not open") message = ics.SpyMessage() flag0 = 0 if msg.is_extended_id: flag0 |= ics.SPY_STATUS_XTD_FRAME if msg.is_remote_frame: flag0 |= ics.SPY_STATUS_REMOTE_FRAME flag3 = 0 if msg.is_fd: message.Protocol = ics.SPY_PROTOCOL_CANFD if msg.bitrate_switch: flag3 |= ics.SPY_STATUS3_CANFD_BRS if msg.error_state_indicator: flag3 |= ics.SPY_STATUS3_CANFD_ESI message.ArbIDOrHeader = msg.arbitration_id msg_data = msg.data message.NumberBytesData = len(msg_data) message.Data = tuple(msg_data[:8]) if msg.is_fd and len(msg_data) > 8: message.ExtraDataPtrEnabled = 1 message.ExtraDataPtr = tuple(msg_data) message.StatusBitField = flag0 message.StatusBitField2 = 0 message.StatusBitField3 = flag3 if msg.channel is not None: message.NetworkID = msg.channel elif len(self.channels) == 1: message.NetworkID = self.channels[0] else: raise ValueError( "msg.channel must be set when using multiple channels.") try: ics.transmit_messages(self.dev, message) except ics.RuntimeError: raise ICSApiError(*ics.get_last_api_error(self.dev))
def send(self, msg, timeout=None): if not self.dev.IsOpen: raise CanError("bus not open") flags = 0 if msg.is_extended_id: flags |= ics.SPY_STATUS_XTD_FRAME if msg.is_remote_frame: flags |= ics.SPY_STATUS_REMOTE_FRAME message = ics.SpyMessage() message.ArbIDOrHeader = msg.arbitration_id message.NumberBytesData = len(msg.data) message.Data = tuple(msg.data) message.StatusBitField = flags message.StatusBitField2 = 0 message.NetworkID = self.network try: ics.transmit_messages(self.dev, message) except ics.RuntimeError: raise ICSApiError(*ics.get_last_api_error(self.dev))
def _test_misc(self, state): arbs = (0x203, 0x204, 0x205, 0x206, 0x201, 0x202) for arb in arbs: message = ics.SpyMessage() message.ArbIDOrHeader = arb message.NetworkID = ics.NETID_DEVICE message.Data = (state,) self.assertEqual(ics.transmit_messages(self.dev1, message), None) # Wait for the report message to get sent time.sleep(0.2) self.assertFalse(ics.get_error_messages(self.dev1)) self.assertFalse(ics.get_error_messages(self.dev2)) messages, error_count = ics.get_messages(self.dev2, False, 0.5) self.assertEqual(error_count, 0) self.assertNotEqual(len(messages), 0) self.assertEqual(messages[-1].ArbIDOrHeader, 0x100) for i, arb in enumerate(arbs): #print(messages[-1].Data, messages[-1].Data[1]>>i & 1) self.assertEqual(messages[-1].Data[1]>>i & 1, state, \ "MISC%d PIN state is wrong" % (arb-0x200)) self.assertFalse(ics.get_error_messages(self.dev1)) self.assertFalse(ics.get_error_messages(self.dev2))
def testCAN(self): message = ics.SpyMessage() message.ArbIDOrHeader = 0xFF message.Data = (1,2,3,4,5,6,7,8) for network in (ics.NETID_HSCAN, ics.NETID_MSCAN, ics.NETID_HSCAN2, \ ics.NETID_HSCAN3, ics.NETID_LSFTCAN, ics.NETID_SWCAN): message.NetworkID = network try: self.assertTrue(ics.transmit_messages(self.dev1, message) == None, network) except ics.RuntimeError as ex: print(ex, network) raise ex self.assertFalse(ics.get_error_messages(self.dev1)) self.assertFalse(ics.get_error_messages(self.dev2)) messages, error_count = ics.get_messages(self.dev1, False, 0.1) self.assertEqual(error_count, 0) self.assertEqual(len(messages), 1) self.assertEqual(message.ArbIDOrHeader, messages[0].ArbIDOrHeader) self.assertEqual(message.NetworkID, messages[0].NetworkID) self.assertEqual(message.Data, messages[0].Data) self.assertFalse(ics.get_error_messages(self.dev1)) self.assertFalse(ics.get_error_messages(self.dev2))
def send(self, msg, timeout=0): """Transmit a message to the CAN bus. :param Message msg: A message object. :param float timeout: If > 0, wait up to this many seconds for message to be ACK'ed. If timeout is exceeded, an exception will be raised. None blocks indefinitely. :raises can.CanError: if the message could not be sent """ if not ics.validate_hobject(self.dev): raise CanError("bus not open") message = ics.SpyMessage() flag0 = 0 if msg.is_extended_id: flag0 |= ics.SPY_STATUS_XTD_FRAME if msg.is_remote_frame: flag0 |= ics.SPY_STATUS_REMOTE_FRAME flag3 = 0 if msg.is_fd: message.Protocol = ics.SPY_PROTOCOL_CANFD if msg.bitrate_switch: flag3 |= ics.SPY_STATUS3_CANFD_BRS if msg.error_state_indicator: flag3 |= ics.SPY_STATUS3_CANFD_ESI message.ArbIDOrHeader = msg.arbitration_id msg_data = msg.data message.NumberBytesData = len(msg_data) message.Data = tuple(msg_data[:8]) if msg.is_fd and len(msg_data) > 8: message.ExtraDataPtrEnabled = 1 message.ExtraDataPtr = tuple(msg_data) message.StatusBitField = flag0 message.StatusBitField2 = 0 message.StatusBitField3 = flag3 if msg.channel is not None: message.NetworkID = msg.channel elif len(self.channels) == 1: message.NetworkID = self.channels[0] else: raise ValueError( "msg.channel must be set when using multiple channels.") msg_desc_id = next(description_id) message.DescriptionID = msg_desc_id receipt_key = (msg.arbitration_id, msg_desc_id) if timeout != 0: self.message_receipts[receipt_key].clear() try: ics.transmit_messages(self.dev, message) except ics.RuntimeError: raise ICSApiError(*ics.get_last_api_error(self.dev)) # If timeout is set, wait for ACK # This requires a notifier for the bus or # some other thread calling recv periodically if timeout != 0 and not self.message_receipts[receipt_key].wait( timeout): raise CanError("Transmit timeout")
import ics if __name__ == "__main__": # Create a CAN Message like normal but set the StatusBitFields msg = ics.SpyMessage() msg.ArbIDOrHeader = 0x01 msg.NetworkID = ics.NETID_HSCAN msg.Protocol = ics.SPY_PROTOCOL_CANFD msg.StatusBitField = ics.SPY_STATUS_CANFD msg.StatusBitField3 = ics.SPY_STATUS3_CANFD_BRS msg.ExtraDataPtr = tuple([x for x in range(64)]) # Open Device and transmit the message device = ics.open_device() ics.transmit_messages(device, msg)