Skip to main content

Python CAN Monitor 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.

  1. Change the current directory to the directory that contains the source code.
  2. Run Python3 setup.py build to build the Python wrapper sample.
  3. Run Python3 -m pip install . to install the Python wrapper sample built in step 2.

After installation, run mpx_canfd_monsample.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​

ItemDescription
CH1Operates in monitor mode.
Calls MPXGetLog every 10 ms to obtain logs and displays them in the console.
CH2Not used

Log Display Details​

ItemDescription
TIMEDisplays the timestamp.
PROTOCOLDisplays the CAN protocol (CAN or CAN FD).
DIRDisplays the log direction.
Rx: receive log / Tx: transmit log
IDDisplays the ID format (see below for meaning) and CAN ID (hexadecimal).
Std: standard ID / Ext: extended ID
DLDisplays the data length (0 to 64).
DLDisplays data (D1 to D64) in hexadecimal.

Sample Source Code​

Source: MPXCtrl_CANFD_MonSample/mpx_canfd_monsample.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, 1)
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('Monitoring...')
time.sleep(20.0) # メインスレッドだけ20秒待つ。read_threadは動き続ける

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.

  1. Open Device
  • MPXOpen() searches for MicroPeckerX devices and obtains the serial number.
  1. Initialize Device
  • MPXSampleCANInit() initializes the MicroPeckerX device with the obtained serial number.
  1. Start Monitoring
  • MPXMonitorStart() starts monitoring.
  1. Start Monitor Thread
  • Starts the thread that runs read_exec(), which periodically obtains logs with DispMonintor().
  1. Stop Monitor Thread
  • Sets stop_event and terminates the thread that runs read_exec().
  1. Stop Monitoring
  • MPXMonitorStop() stops monitoring.
  1. Close Device
  • MPXClose() closes the MicroPeckerX device with the obtained serial number.