Python CAN Simulation Program
Building the Runtime Environment
Ubuntu/Debian-based systems reject direct installation of packages into the Python environment.
As one way to build the sample program runtime environment, this section describes creating a virtual environment with venv and installing the package there.
Perform the following steps with the virtual environment enabled.
- Change the current directory to the directory that contains the source code.
- Run
Python3 setup.py buildto build the Python wrapper sample. - Run
Python3 -m pip install .to install the Python wrapper sample built in step 2.
After installation, run mpx_canfd_simsample.py to start the sample program.
If User Write Permission Settings have not been performed, specify the Python executable in the virtual environment.
Operation Specifications
| Item | Description |
|---|---|
| CH1 | Operates in simulation mode. Sends CAN frames 10,000 times by using the direct transmit function (for the CAN frame contents, see Preset Frame). Also calls MPXGetLog every 10 ms to obtain logs and displays them in the console. |
| CH2 | Not used |
Preset Frame
The preset frame contents are as follows.
| Frame Type | Slot | ID | DL | Data (Hex) | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Format | CAN ID | D1 | D2 | D3 | D4 | D5 | D6 | D7 | D8 | |||
| Direct transmit frame (*1) | - | Standard ID | 000H | 8 | 00H | 01H | 02H | 03H | 04H | 05H | 06H | 07H |
*1: The CAN ID and D1 value are incremented by 1 on 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: receive log / Tx: transmit log |
| ID | Displays the ID format (see below for meaning) and CAN ID (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 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()
This source code performs the following processing flow.
- Open Device
MPXOpen()searches for MicroPeckerX devices and obtains the serial number.
- Initialize Device
MPXSampleCANInit()initializes the MicroPeckerX device with the obtained serial number.
- Start Monitoring
MPXMonitorStart()starts monitoring.
- Start Monitor Thread
- Starts the thread that runs
read_exec(), which periodically obtains logs withDispMonintor().
- Send CAN Frames
- Repeatedly calls
MPXDirectSend()10,000 times to send CAN frames.- The 3rd argument (CAN ID) is incremented by 1 on each transmission.
- In the 5th argument (transmit payload), only D1 is incremented by 1 on each transmission.
- Stop Monitor Thread
- Sets
stop_eventand terminates the thread that runsread_exec().
- Stop Monitoring
MPXMonitorStop()stops monitoring.
- Close Device
MPXClose()closes the MicroPeckerX device with the obtained serial number.