Python LIN Master 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_lin_mstsample.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 master simulation mode. Sends LIN frames by using the schedule transmission function (for the LIN frame contents, see Transmission Schedule). Also calls MPXGetLog every 10 ms to obtain logs and displays them in the console. |
| CH2 | Not used |
Transmission Schedule
The transmission schedule contents are as follows.
| No. | Dir | ID | Delay | DL | Data (Hex) | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| D1 | D2 | D3 | D4 | D5 | D6 | D7 | D8 | |||||
| 0 | Tx | 10H | 500ms | 2 | 40H | C8H | ||||||
| Rx | 11H | 500ms | - | |||||||||
Log Display Details
| Item | Description |
|---|---|
| TIME | Displays the timestamp. |
| DIR | Displays the log direction. Rx: receive log / Tx: transmit log |
| TYPE | Displays the log type. |
| ID | Displays the LIN ID (hexadecimal). |
| SBL | Displays the SyncBreak width. |
| DL | Displays the data length (0 to 64). |
| DATA | Displays data (D1 to D64) in hexadecimal. Data bytes are separated by spaces. |
| CS | Displays the checksum in hexadecimal. |
| WPL | Displays the wakeup pulse width. |
Sample Source Code
Source: MPXCtrl_LIN_MstSample/mpx_lin_mstsample.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]):
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()
This source code performs the following processing flow.
- Open Device
MPXOpen()searches for MicroPeckerX devices and obtains the serial number.
- Initialize Device
MPXSampleLINInit()initializes the MicroPeckerX device with the obtained serial number.
- Configure Master Simulation
MPXSampleLINSetMasterSchedule()configures the master simulation schedule.
- Change Target Master Simulation
MPXSampleLINChangeMasterSchedule()sets the schedule to execute.
- Set LIN Status
MPXSampleLINSetStatus()sets the LIN status.
- Start Monitoring
MPXMonitorStart()starts monitoring. This also starts LIN frame transmission based on the schedule configured in step 3.
- Start Monitor Thread
- Starts the thread that runs
read_exec(), which periodically obtains logs withDispMonintor().
- Stop Monitor Thread
- Sets
stop_eventand terminates the thread that runsread_exec().
- Stop Monitoring
MPXMonitorStop()stops monitoring. This also stops LIN frame transmission based on the schedule configured in step 3.
- Close Device
MPXClose()closes the MicroPeckerX device with the obtained serial number.