def testSendPeriodicAndChangeFrameAndStopExtended(self): """Start periodic CAN transmission, update frame data (not interval), and finally stop transmission.""" RESULT_ALLOWED_DIFFERENCE = 5 # Number of missed frames, or when stopping sending too late. TRANSMISSION_INTERVAL_MILLISECONDS = 20 MEASUREMENT_TIME = 1 # seconds self.simulated_can_process = subprocess.Popen(['candump', VIRTUAL_CAN_BUS_NAME], shell=False, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) time.sleep(0.1) frame_zeros = canframe.CanFrame(self.FRAME_ID_SEND, b'\x00\x00\x00\x00\x00\x00\x00\x00', 'extended') frame_ones = canframe.CanFrame(self.FRAME_ID_SEND, b'\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF', 'extended') self.interface.setup_periodic_send(frame_zeros, TRANSMISSION_INTERVAL_MILLISECONDS) time.sleep(MEASUREMENT_TIME) self.interface.setup_periodic_send(frame_ones, restart_timer=False) time.sleep(MEASUREMENT_TIME) self.interface.stop_periodic_send(self.FRAME_ID_SEND, 'extended') time.sleep(MEASUREMENT_TIME) self.simulated_can_process.terminate() out, err = self.simulated_can_process.communicate() projected_number_of_frames = MEASUREMENT_TIME * 1000 / TRANSMISSION_INTERVAL_MILLISECONDS number_of_frames_zeros = out.count(" {:08.0f} [8] 00 00 00 00 00 00 00 00".format(self.FRAME_ID_SEND)) self.assertGreaterEqual(number_of_frames_zeros, projected_number_of_frames - RESULT_ALLOWED_DIFFERENCE) self.assertLessEqual(number_of_frames_zeros, projected_number_of_frames + RESULT_ALLOWED_DIFFERENCE) number_of_frames_ones = out.count(" {:08.0f} [8] FF FF FF FF FF FF FF FF".format(self.FRAME_ID_SEND)) self.assertGreaterEqual(number_of_frames_ones, projected_number_of_frames - RESULT_ALLOWED_DIFFERENCE) self.assertLessEqual(number_of_frames_ones, projected_number_of_frames + RESULT_ALLOWED_DIFFERENCE)
def testConstructor(self): frame1 = canframe.CanFrame(0x7FF, b'\x02', 'standard') self.assertEqual(frame1.frame_id, 0x7FF) self.assertEqual(frame1.frame_data, b'\x02') self.assertEqual(frame1.frame_format, 'standard') frame2 = canframe.CanFrame(0x1FFFFFFF, b'\x02\x03', 'extended') self.assertEqual(frame2.frame_id, 0x1FFFFFFF) self.assertEqual(frame2.frame_data, b'\x02\x03') self.assertEqual(frame2.frame_format, 'extended')
def testConstructorNamedArguments(self): frame = canframe.CanFrame(frame_id=3, frame_data=b'\x04', frame_format='extended') self.assertEqual(frame.frame_id, 3) self.assertEqual(frame.frame_data, b'\x04') self.assertEqual(frame.frame_format, 'extended')
def testStopNonexistingPeriodicTask(self): self.assertRaises(exceptions.CanException, self.interface.stop_periodic_send, self.FRAME_ID_SEND) TRANSMISSION_INTERVAL_MILLISECONDS = 20 frame_zeros = canframe.CanFrame(self.FRAME_ID_SEND, b'\x00\x00\x00\x00\x00\x00\x00\x00', 'standard') self.interface.setup_periodic_send(frame_zeros, TRANSMISSION_INTERVAL_MILLISECONDS) self.assertRaises(exceptions.CanException, self.interface.stop_periodic_send, self.FRAME_ID_SEND, 'extended')
def testSignalvalueGetDoubleLittle(self): testsig1 = cansignal.CanSignalDefinition('testsignal1', 0, 64, endianness='little', signaltype='double') # Example from https://en.wikipedia.org/wiki/Single-precision_floating-point_format frame1 = canframe.CanFrame(1, b'\x00\x00\x00\x00\x00\x00\xF0\x3F') self.assertAlmostEqual(frame1.get_signalvalue(testsig1), 1.0)
def testSignalvalueGetSingle(self): testsig1 = cansignal.CanSignalDefinition('testsignal1', 56, 32, endianness='big', signaltype='single') # Example from https://en.wikipedia.org/wiki/Single-precision_floating-point_format frame1 = canframe.CanFrame(1, b'\x00\x00\x00\x00\x3E\x20\x00\x00') self.assertAlmostEqual(frame1.get_signalvalue(testsig1), 0.15625)
def testSignalvalueGetDouble(self): testsig1 = cansignal.CanSignalDefinition('testsignal1', 56, 64, endianness='big', signaltype='double') # Example from https://en.wikipedia.org/wiki/Single-precision_floating-point_format frame1 = canframe.CanFrame(1, b'\x40\x00\x00\x00\x00\x00\x00\x00') self.assertAlmostEqual(frame1.get_signalvalue(testsig1), 2.0) frame1 = canframe.CanFrame(1, b'\x3F\xF0\x00\x00\x00\x00\x00\x00') self.assertAlmostEqual(frame1.get_signalvalue(testsig1), 1.0) frame1 = canframe.CanFrame(1, b'\x3F\xF0\x00\x00\x00\x00\x00\x01') self.assertAlmostEqual(frame1.get_signalvalue(testsig1), 1.0) frame1 = canframe.CanFrame(1, b'\x7F\xEF\xFF\xFF\xFF\xFF\xFF\xFF') self.assertAlmostEqual(frame1.get_signalvalue(testsig1), 1.7976931348623157e308)
def testSignalvalueSetDoubleLittle(self): testsig1 = cansignal.CanSignalDefinition('testsignal1', 0, 64, endianness='little', signaltype='double') frame1 = canframe.CanFrame(1, b'\x00\x00\x00\x00\x00\x00\x00\x00') # Example from https://en.wikipedia.org/wiki/Double-precision_floating-point_format frame1.set_signalvalue(testsig1, 2.0) self.assertEqual(frame1.frame_data, b'\x00\x00\x00\x00\x00\x00\x00\x40')
def testSendBcmFrame(self): self.simulated_can_process = subprocess.Popen(['candump', VIRTUAL_CAN_BUS_NAME, '-n', '1'], shell=False, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) time.sleep(0.1) frame_to_send = canframe.CanFrame(FRAME_ID_SEND, b"\x02\x03\x00\x05\x04\x00\x00\x01") self.canbus_bcm.send_frame(frame_to_send) out, err = self.simulated_can_process.communicate(timeout=2) known_result = "[8] 02 03 00 05 04 00 00 01" self.assertIn(known_result, out)
def testSignalvalueSetSingleLittle(self): testsig1 = cansignal.CanSignalDefinition('testsignal1', 32, 32, endianness='little', signaltype='single') print(testsig1.get_descriptive_ascii_art()) frame1 = canframe.CanFrame(1, b'\x00\x00\x00\x00\x00\x00\x00\x00') # Example from https://en.wikipedia.org/wiki/Single-precision_floating-point_format frame1.set_signalvalue(testsig1, 0.15625) self.assertEqual(frame1.frame_data, b'\x00\x00\x00\x00\x00\x00\x20\x3E')
def setUp(self): self.frame = canframe.CanFrame(1, b'\x00\x02\x00\x08\x00\x00\x00\xff') self.testsig1 = cansignal.CanSignalDefinition( 'testsignal1', 56, 1) # Least significant bit in last byte self.testsig2 = cansignal.CanSignalDefinition( 'testsignal2', 8, 16, endianness='big') # Two leftmost bytes self.testsig3 = cansignal.CanSignalDefinition( 'testsignal3', 24, 16, endianness='little', maxvalue=1200) # Two center bytes self.testsig4 = cansignal.CanSignalDefinition( 'testsignal4', 48, 8, signaltype='signed') # Second last byte self.frame_def = canframe_definition.CanFrameDefinition( 1, 'testmessage') self.frame_def.signaldefinitions.append(self.testsig1) self.frame_def.signaldefinitions.append(self.testsig2) self.frame_def.signaldefinitions.append(self.testsig3) self.frame_def.signaldefinitions.append(self.testsig4)
def testSetupPeriodicSendWrongType(self): frame_zeros = canframe.CanFrame(self.FRAME_ID_SEND, b'\x00\x00\x00\x00\x00\x00\x00\x00') self.assertRaises(exceptions.CanException, self.interface.setup_periodic_send, 1) self.assertRaises(exceptions.CanException, self.interface.setup_periodic_send, frame_zeros, "ABC")