|
| 1 | +import unittest |
| 2 | + |
| 3 | +import can |
| 4 | + |
| 5 | + |
| 6 | +BITRATE = 500000 |
| 7 | +TIMEOUT = 0.1 |
| 8 | + |
| 9 | +INTERFACE_1 = 'virtual' |
| 10 | +CHANNEL_1 = 0 |
| 11 | +INTERFACE_2 = 'virtual' |
| 12 | +CHANNEL_2 = 0 |
| 13 | + |
| 14 | + |
| 15 | +class Back2BackTestCase(unittest.TestCase): |
| 16 | + """ |
| 17 | + Use two interfaces connected to the same CAN bus and test them against |
| 18 | + each other. |
| 19 | + """ |
| 20 | + |
| 21 | + def setUp(self): |
| 22 | + self.bus1 = can.interface.Bus(channel=CHANNEL_1, |
| 23 | + bustype=INTERFACE_1, |
| 24 | + bitrate=BITRATE) |
| 25 | + self.bus2 = can.interface.Bus(channel=CHANNEL_2, |
| 26 | + bustype=INTERFACE_2, |
| 27 | + bitrate=BITRATE) |
| 28 | + |
| 29 | + def tearDown(self): |
| 30 | + self.bus1.shutdown() |
| 31 | + self.bus2.shutdown() |
| 32 | + |
| 33 | + def _send_and_receive(self, msg): |
| 34 | + # Send with bus 1, receive with bus 2 |
| 35 | + self.bus1.send(msg) |
| 36 | + recv_msg = self.bus2.recv(TIMEOUT) |
| 37 | + self.assertIsNotNone(recv_msg, |
| 38 | + "No message was received on %s" % INTERFACE_2) |
| 39 | + self.assertEqual(recv_msg, msg) |
| 40 | + |
| 41 | + # Send with bus 2, receive with bus 1 |
| 42 | + # Add 1 to arbitration ID to make it a different message |
| 43 | + msg.arbitration_id += 1 |
| 44 | + self.bus2.send(msg) |
| 45 | + recv_msg = self.bus1.recv(TIMEOUT) |
| 46 | + self.assertIsNotNone(recv_msg, |
| 47 | + "No message was received on %s" % INTERFACE_1) |
| 48 | + self.assertEqual(recv_msg, msg) |
| 49 | + |
| 50 | + def test_standard_message(self): |
| 51 | + msg = can.Message(extended_id=False, |
| 52 | + arbitration_id=0x100, |
| 53 | + data=[1, 2, 3, 4, 5, 6, 7, 8]) |
| 54 | + self._send_and_receive(msg) |
| 55 | + |
| 56 | + def test_extended_message(self): |
| 57 | + msg = can.Message(extended_id=True, |
| 58 | + arbitration_id=0x123456, |
| 59 | + data=[10, 11, 12, 13, 14, 15, 16, 17]) |
| 60 | + self._send_and_receive(msg) |
| 61 | + |
| 62 | + def test_remote_message(self): |
| 63 | + msg = can.Message(extended_id=False, |
| 64 | + arbitration_id=0x200, |
| 65 | + is_remote_frame=True, |
| 66 | + dlc=4) |
| 67 | + self._send_and_receive(msg) |
| 68 | + |
| 69 | + def test_dlc_less_than_eight(self): |
| 70 | + msg = can.Message(extended_id=False, |
| 71 | + arbitration_id=0x300, |
| 72 | + data=[4, 5, 6]) |
| 73 | + self._send_and_receive(msg) |
| 74 | + |
| 75 | + |
| 76 | +if __name__ == '__main__': |
| 77 | + unittest.main() |
0 commit comments