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 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 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))
#!/usr/bin/python import ics, sys, getopt, re, string import random, time, sched, threading linenm = 0 startime = time.time() schd = sched.scheduler(time.time, time.sleep) devices = ics.find_devices() msg = ics.SpyMessage() pre_miltm = 0.0 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),
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")