Skip to main content
Version: 4.00

Python LIN Master 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.

  1. Change the current directory to the directory containing the source code.
  2. Run python setup.py build to build the Python wrapper sample.
  3. Run python -m pip install . to install the Python wrapper sample built in step 2.
  4. Run python mpx_lin_mstsample.py to start the sample program.

Operation Specification​

ItemDescription
CH1Operates in master simulation mode.
Sends LIN frames using scheduled transmission (see Transmission Schedule for LIN frame contents).
Also calls MPXGetLog every 10 ms to acquire logs and display them in the console.
CH2Not used

Transmission Schedule​

The transmission schedule is as follows.

No.DirIDDelayDLData (Hexadecimal)
D1D2D3D4D5D6D7D8
0Tx10H500ms240HC8H
Rx11H500ms-

Log Display Details​

ItemDescription
TIMEDisplays the timestamp.
DIRDisplays the log direction.
Rx: received log; Tx: transmitted log.
TYPEDisplays the log type.
IDDisplays the LIN ID in hexadecimal.
SBLDisplays the SyncBreak width.
DLDisplays the data length (0 to 64).
DATADisplays data (D1 to D64) in hexadecimal. Data bytes are separated by spaces.
CSDisplays the checksum in hexadecimal.
WPLDisplays the wakeup pulse width.

Sample Source Code​

Source: MPXCtrl_LIN_MstSample/mpx_lin_mstsample.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]):
if ret[1 + i][4] == 'Header':
print('TIME:' + str(ret[1 + i][0]) + '[ms] ' + str(ret[1 + i][1]) + '[us]' +
' DIR:' + ret[1 + i][3] +
' TYPE:' + ret[1 + i][4] +
' ID:' + hex(ret[1 + i][5]) +
' SBL:' + str(ret[1 + i][6]) + '[bit]')
elif ret[1 + i][4] == 'Response':
data = ret[1 + i][8]
data_text = '\\'.join(hex(b) for b in data)
print('TIME:' + str(ret[1 + i][0]) + '[ms] ' + str(ret[1 + i][1]) + '[us]' +
' DIR:' + ret[1 + i][3] +
' TYPE:' + ret[1 + i][4] +
' ID:' + hex(ret[1 + i][5]) +
' DL:' + str(ret[1 + i][7]) +
' DATA:' + data_text +
' CS:' + hex(ret[1 + i][9]))
elif ret[1 + i][4] == 'Wakeup':
print('TIME:' + str(ret[1 + i][0]) + '[ms] ' + str(ret[1 + i][1]) + '[us]' +
' DIR:' + ret[1 + i][3] +
' TYPE:' + ret[1 + i][4] +
' ID:' + hex(ret[1 + i][5]) +
' WPL:' + str(ret[1 + i][10]) + '[ms]')
elif ret[1 + i][4] == 'Sleep':
data = ret[1 + i][8]
data_text = '\\'.join(hex(b) for b in data)
print('TIME:' + str(ret[1 + i][0]) + '[ms] ' + str(ret[1 + i][1]) + '[us]' +
' DIR:' + ret[1 + i][3] +
' TYPE:' + ret[1 + i][4] +
' ID:' + hex(ret[1 + i][5]) +
' DL:' + str(ret[1 + i][7]) +
' DATA:' + data_text +
' CS:' + hex(ret[1 + i][9]))
else:
print('TIME:' + str(ret[1 + i][0]) + '[ms] ' + str(ret[1 + i][1]) + '[us]' +
' TYPE:' + ret[1 + i][4])
pass
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 LIN INIT.')
ret = MPX_ADL_WR.MPXSampleLINInit(serial, 1)
if ret != 0:
print('LIN INIT error:' + str(ret))

print('MPX LIN Master Set Schedule.')
ret = MPX_ADL_WR.MPXSampleLINSetMasterSchedule(serial, 0)
if ret != 0:
print('LIN Master Set Schedule error:' + str(ret))

print('MPX LIN Master Set Simulation.')
ret = MPX_ADL_WR.MPXSampleLINSetMasterSim(serial, 0)
if ret != 0:
print('LIN Master Set Simulation error:' + str(ret))

print('MPX LIN Master Change Schedule.')
ret = MPX_ADL_WR.MPXSampleLINChangeMasterSchedule(serial, 0)
if ret != 0:
print('LIN Master Change Schedule error:' + str(ret))

print('MPX LIN Master Set Status.')
ret = MPX_ADL_WR.MPXSampleLINSetStatus(serial, 0)
if ret != 0:
print('LIN Master Set Status 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(10.0) # メインスレッドだけ10秒待つ。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()

The processing flow of this source code is as follows.

  1. Open the Device
  • MPXOpen() searches for MicroPeckerX devices and obtains their serial numbers.
  1. Initialize the Device
  • MPXSampleLINInit() initializes the MicroPeckerX device identified by the obtained serial number.
  1. Configure Master Simulation
  • MPXSampleLINSetMasterSchedule() configures the master simulation schedule.
  1. Select the Master Simulation to Run
  • MPXSampleLINChangeMasterSchedule() selects the schedule to run.
  1. Set LIN Status
  • MPXSampleLINSetStatus() sets the LIN status.
  1. Start Monitoring
  • MPXMonitorStart() starts monitoring.This also starts transmission of LIN frames according to the schedule configured in step 3.
  1. Start the Monitoring Thread
  • DispMonintor() starts a thread that runs read_exec() to acquire logs periodically.
  1. Stop the Monitoring Thread
  • stop_eventis set to terminate the thread that runs read_exec().
  1. Stop Monitoring
  • MPXMonitorStop() stops monitoring.This also stops transmission of LIN frames according to the schedule configured in step 3.
  1. Close the Device
  • MPXClose() closes the MicroPeckerX device identified by the obtained serial number.