Python CAN Simulation Program
Setting Up the Execution Environment
This section describes one way to prepare the sample program environment: create a virtual environment with venv, then build and install the sample in that environment.
Perform the following steps while the virtual environment is active.
- Change the current directory to the directory containing the source code.
- Run
python setup.py buildto build the Python wrapper sample. - Run
python -m pip install .to install the Python wrapper sample built in step 2. - Run
python mpx_canfd_simsample.pyto start the sample program.
Operation Specification
| Item | Description |
|---|---|
| CH1 | Operates in simulation mode. Sends a CAN frame 10,000 times using direct transmission (see Preset Frames for the CAN frame contents). Also calls MPXGetLog every 10 ms to acquire logs and display them in the console. |
| CH2 | Not used |
Preset Frames
The preset frames are as follows.
| Frame Type | Slot | ID | DL | Data (Hexadecimal) | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Format | CAN ID | D1 | D2 | D3 | D4 | D5 | D6 | D7 | D8 | |||
| Direct transmission frame *1 | - | Standard ID | 000H | 8 | 00H | 01H | 02H | 03H | 04H | 05H | 06H | 07H |
*1: The CAN ID and D1 value are incremented by one for each transmission.
Log Display Details
| Item | Description |
|---|---|
| TIME | Displays the timestamp. |
| PROTOCOL | Displays the CAN protocol (CAN or CAN FD). |
| DIR | Displays the log direction. Rx: received log; Tx: transmitted log. |
| ID | Displays the ID format (see below) and CAN ID in hexadecimal. Std: standard ID; Ext: extended ID. |
| DL | Displays the data length (0 to 64). |
| DL | Displays data (D1 to D64) in hexadecimal. |
Sample Source Code
Source: MPXCtrl_CANFD_MonSample/mpx_canfd_simsample.py
import os
import sys
from pathlib import Path
if sys.platform == 'win32':
_dll_dir = os.add_dll_directory(
str(Path(__file__).resolve().parent)
)
import MPX_ADL_WR
import threading
import time
stop_event = threading.Event()
#read thread
def read_exec():
while not stop_event.is_set():
ret = MPX_ADL_WR.DispMonitor(serial, 1)
if not ret is None:
for i in range(ret[0]):
data = ret[1 + i][7]
data_text = '\\'.join(hex(b) for b in data)
print('TIME:' + str(ret[1 + i][0]) +'[ms] ' + str(ret[1 + i][1]) +'[us]' +
' PROTOCOL:' + ret[1 + i][2] +
' DIR:' + ret[1 + i][3] +
' ID:' + ret[1 + i][4] + '/' + hex(ret[1 + i][5]) +
' DL:' + str(ret[1 + i][6]) +
' DATA:' + data_text)
time.sleep(0.01)
ret = MPX_ADL_WR.MPXOpen()
if ret[0] != 0:
print('MPXOpen Error:' + str(ret[0]))
print('Serial No.')
for i in range(ret[1]):
print(' ' + str(ret[2][i]))
serial = ret[2][0]
print('MPX CAN INIT.')
ret = MPX_ADL_WR.MPXSampleCANInit(serial, 2)
if ret != 0:
print('CAN INIT error:' + str(ret))
print('MPX MonitorStart.')
ret = MPX_ADL_WR.MPXMonitorStart(serial)
print('Start ReadThread...')
read_thread = threading.Thread(target = read_exec)
read_thread.start()
print('CAN_SEND...')
senddata = bytearray(b'\x00\x01\x02\x03\x04\x05\x06\x07')
for i in range(10000):
ret = MPX_ADL_WR.MPXCANDirectSend(serial, 1, i, 8, senddata)
senddata[0] = (senddata[0] + 1) % 256
if ret != 0:
print('SendError:' + str(ret))
stop_event.set()
read_thread.join(timeout = 2.0)
ret = MPX_ADL_WR.MPXMonitorStop(serial)
if ret[0] != 0:
print('MonitorStop :Error' + str(ret[0]))
else:
print('MonitorStop :Time' + str(ret[1]) + '[ms]' + str(ret[2]) + '[us]')
ret = MPX_ADL_WR.MPXClose()
The processing flow of this source code is as follows.
- Open the Device
- MPXOpen() searches for MicroPeckerX devices and obtains their serial numbers.
- Initialize the Device
- MPXSampleCANInit() initializes the MicroPeckerX device identified by the obtained serial number.
- Start Monitoring
- MPXMonitorStart() starts monitoring.
- Start the Monitoring Thread
- DispMonintor() starts a thread that runs read_exec() to acquire logs periodically.
- Transmit CAN Frames
- MPXDirectSend() is called 10,000 times to transmit CAN frames.
- The third argument (CAN ID) is incremented by one for each transmission.
- For the fifth argument (transmission payload), only D1 is incremented by one for each transmission.
- Stop the Monitoring Thread
- stop_eventis set to terminate the thread that runs read_exec().
- Stop Monitoring
- MPXMonitorStop() stops monitoring.
- Close the Device
- MPXClose() closes the MicroPeckerX device identified by the obtained serial number.