Python版 LINマスタシミュレーションプログラム
動作環境構築
Ubuntu/Debian系では、Python 環境へのパッケージの直接インストールを拒否する仕様となっております。
そのため、サンプルプログラムの動作環境を構築する方法の一つとして、venvで仮想環境を作成し、そこにインストールする手順を紹介します。
なお、以下の手順は仮想環境を有効にした状態で行ってください。
- カレントディレクトリをソースコードと同じディレクトリにします。
Python3 setup.py buildを実行して、Pythonラッパーサンプルをビルドします。Python3 -m pip install .を実行して、手順2でビルドしたPythonラッパーサンプルをインストールします。
インストール後に mpx_lin_mstsample.py を実行すると、サンプルプログラムが動作します。
その際、「ユーザー書き込み許可設定」を行っていない場合は、仮想環境下のPythonを指定する必要があります。
動作仕様
| 項目 | 説明 |
|---|---|
| CH1 | マスタシミュレーションモードで動作します。 スケジュール送信機能でLINフレームを送信します(LINフレームの内容は送信スケジュールを参照)。 また、10msごとにMPXGetLogを呼び出してログを取得し、コンソールに表示します。 |
| CH2 | 未使用 |
送信スケジュール
送信スケジュールの内容は以下の通りです。
| No. | Dir | ID | Delay | DL | Data(16進数) | |||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| D1 | D2 | D3 | D4 | D5 | D6 | D7 | D8 | |||||
| 0 | Tx | 10H | 500ms | 2 | 40H | C8H | ||||||
| Rx | 11H | 500ms | - | |||||||||
ログ表示部詳細
| 項目 | 説明 |
|---|---|
| TIME | タイムスタンプを表示します。 |
| DIR | ログ方向を表示します。 Rx:受信ログ Tx:送信ログ |
| TYPE | ログ種別を表示します。 |
| ID | LIN ID(16進数)を表示します。 |
| SBL | SyncBreak幅を表示します。 |
| DL | データ長(0~64)を表示します。 |
| DATA | データ(D1~D64)を16進数で表示します。データの区切り文字はスペースです。 |
| CS | チェックサムを16進数で表示します。 |
| WPL | ウェイクアップパルス幅を表示します。 |
サンプルソースコード
対応元: 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()
このソースコードによる処理の流れは以下の通りです。
- デバイスオープン
- MPXOpen() でMicroPeckerXデバイスを検索し、シリアル番号取得します。
- デバイス初期化
- MPXSampleLINInit() で取得したシリアル番号のMicroPeckerXデバイスを初期化します。
- マスタシミュレーション設定
- MPXSampleLINSetMasterSchedule() でマスタシミュレーションのスケジュールを設定します。
- 実行対象マスタシミュレーション変更
- MPXSampleLINChangeMasterSchedule() で実行対象のスケジュールを設定します。
- LINステータス設定
- MPXSampleLINSetStatus() でLINステータスを設定します。
- モニタ開始
- MPXMonitorStart() でモニタを開始します。これにより、手順3で設定したスケジュールに基づくLINフレームの送信も開始します。
- モニタ用スレッド起動
- DispMonintor() でログを定期的に取得する read_exec()を実行させるスレッドを起動します。
- モニタ用スレッド停止
- stop_eventをセットして、read_exec()を実行させるスレッドを終了します。
- モニタ停止
- MPXMonitorStop() でモニタを停止します。これにより、手順3で設定したスケジュールに基づくLINフレームの送信も停止します。
- デバイスクローズ
- MPXClose() で取得したシリアル番号のMicroPeckerXデバイスをクローズします。