def setUp(self): enable_virtual_can_bus() self.canbus_raw = canbus.CanBus(copy.deepcopy(TESTCONFIG1), VIRTUAL_CAN_BUS_NAME, timeout=1.0) self.canbus_bcm = canbus.CanBus(copy.deepcopy(TESTCONFIG1), VIRTUAL_CAN_BUS_NAME, timeout=1.0, use_bcm=True) parent_directory = os.path.dirname(__file__) self.input_filename = os.path.join(parent_directory, INPUT_FILENAME) self.input_filename_no_busdefinition = os.path.join(parent_directory, INPUT_FILENAME_NO_BUSDEFINITION) self.simulated_can_process = None
def testPeriodicSendingUpdateSignalsAndStop(self): RESULT_ALLOWED_DIFFERENCE = 3 # Number of missed frames, or when stopping sending too late. TRANSMISSION_INTERVAL_MILLISECONDS = 20 MEASUREMENT_TIME = 0.5 # seconds config = copy.deepcopy(TESTCONFIG1) config.framedefinitions[ FRAME_ID_SEND].cycletime = TRANSMISSION_INTERVAL_MILLISECONDS self.canbus_bcm = canbus.CanBus(config, VIRTUAL_CAN_BUS_NAME, timeout=1.0, use_bcm=True) 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) signalvalues_to_send = { 'testsignal1': 1, 'testsignal2': 1, 'testsignal3': 256 * 4 + 5 } self.canbus_bcm.send_signals(signalvalues_to_send) time.sleep(MEASUREMENT_TIME) signalvalues_to_send = { 'testsignal1': 1, 'testsignal2': 255, 'testsignal3': 256 * 4 + 5 } self.canbus_bcm.send_signals(signalvalues_to_send) time.sleep(MEASUREMENT_TIME) self.canbus_bcm.stop_sending() 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_A = out.count( " {:03.0f} [8] 00 01 00 05 04 00 00 01".format(FRAME_ID_SEND)) self.assertGreaterEqual( number_of_frames_A, projected_number_of_frames - RESULT_ALLOWED_DIFFERENCE) self.assertLessEqual( number_of_frames_A, projected_number_of_frames + RESULT_ALLOWED_DIFFERENCE) number_of_frames_B = out.count( " {:03.0f} [8] 00 FF 00 05 04 00 00 01".format(FRAME_ID_SEND)) self.assertGreaterEqual( number_of_frames_B, projected_number_of_frames - RESULT_ALLOWED_DIFFERENCE) self.assertLessEqual( number_of_frames_B, projected_number_of_frames + RESULT_ALLOWED_DIFFERENCE)
def testStartSendingAllSignals(self): RESULT_ALLOWED_DIFFERENCE = 3 # Number of missed frames, or when stopping sending too late. TRANSMISSION_INTERVAL_MILLISECONDS = 20 MEASUREMENT_TIME = 0.5 # seconds config = copy.deepcopy(TESTCONFIG1) config.framedefinitions[FRAME_ID_SEND].cycletime = TRANSMISSION_INTERVAL_MILLISECONDS config.framedefinitions[FRAME_ID_SEND].signaldefinitions[2].defaultvalue = 25.5 self.canbus_bcm = canbus.CanBus(config, VIRTUAL_CAN_BUS_NAME, timeout=1.0, use_bcm=True) 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) self.canbus_bcm.start_sending_all_signals() time.sleep(MEASUREMENT_TIME) self.canbus_bcm.stop() time.sleep(MEASUREMENT_TIME) self.canbus_bcm.stop() # Handle cases with already stopped transmission and reception 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 = out.count(" {:03.0f} [8] 00 00 00 19 00 00 00 00".format(FRAME_ID_SEND)) self.assertGreaterEqual(number_of_frames, projected_number_of_frames - RESULT_ALLOWED_DIFFERENCE) self.assertLessEqual(number_of_frames, projected_number_of_frames + RESULT_ALLOWED_DIFFERENCE)
def testConstructor(self): self.canbus_raw.caninterface.close() config = configuration.Configuration() a = canbus.CanBus(config, VIRTUAL_CAN_BUS_NAME, timeout=1.0) self.assertEqual(a.caninterface._interfacename, VIRTUAL_CAN_BUS_NAME) a.caninterface.close() self.assertEqual(a.caninterface._interfacename, VIRTUAL_CAN_BUS_NAME) b = canbus.CanBus(config, VIRTUAL_CAN_BUS_NAME, timeout=1.0) self.assertEqual(b.caninterface._interfacename, VIRTUAL_CAN_BUS_NAME) b.caninterface.close() self.assertEqual(b.caninterface._interfacename, VIRTUAL_CAN_BUS_NAME) c = canbus.CanBus(config, VIRTUAL_CAN_BUS_NAME, timeout=1.0, use_bcm=True) self.assertEqual(c.caninterface._interfacename, VIRTUAL_CAN_BUS_NAME) c.caninterface.close() self.assertEqual(c.caninterface._interfacename, VIRTUAL_CAN_BUS_NAME)