バックナンバーはこちら。
https://www.simulationroom999.com/blog/diagnostic-communication-backnumber/
はじめに
ISO-TPのシミュレーションをしよう。のシリーズ。
Pythonの話がしばらく続く。
登場人物
博識フクロウのフクさん

イラストACにて公開の「kino_k」さんのイラストを使用しています。
https://www.ac-illust.com/main/profile.php?id=iKciwKA9&area=1
エンジニア歴8年の太郎くん

イラストACにて公開の「しのみ」さんのイラストを使用しています。
https://www.ac-illust.com/main/profile.php?id=uCKphAW2&area=1
python-canで頑張ればISO15765-2はできる?

PythonでCANの制御が出来たということは、
これを駆使すればトランスポート層とネットワーク層に相当するISO15765-2が実現できるってことで良いんだよね?

まぁ頑張れはできるんじゃん?

なに?
その投げやりな感じは?

いや、もっと楽な方法があるんで。

だったら、それを教えてよ!!
Pythonのcan-isotp

実はPythonのパッケージとしてISO15765-2ことISO-TPに相当するものが存在する。
その名もずばり、can-isotp。

おー!
それを使えばOKってことか!!

あれ?
だったら、前回までのpytohn-canの話はなんだったの?
ISO-TP相当のパッケージがあるなら、
それでよかったのでは?

can-isotpの依存関係にpython-canがあるんだよ。
だからpython-canのインストール自体はどっちみち必要。

使い方は省略しても良かったんじゃない?

まぁそれもアリな気はするけど、
私も現状としては手探りなんだよね。
それに、can-isotpの実験をする段階で結局はpython-canを使う予定なんで、
やはり、省略するべきではないかな?

まぁ、その実験ってのをやっていこうよ。
can-isotpのインストール

これは説明要らないかもしれないけど、pip使うだけ。
> pip install can-isotp
※プロキシ経由の場合、
> pip install --proxy=[プロキシ名]:[ポート番号] can-isotp

うん。
インストールできた。
can-isotpの送信テスト

じゃ、とりあえず一発送信テストをしてみるか。
スクリプトコードは以下になるよ。
ファイル名はisotpsndtest.pyとでもしておこうか。
import isotp
import time
from can.interfaces.vector import VectorBus
bus = VectorBus(channel=0, bitrate=500000)
addr = isotp.Address(isotp.AddressingMode.NormalFixed_29bits, source_address=0xF1, target_address=0x10)
stack = isotp.CanStack(bus, address=addr)
stack.send(b'\x01\x02\x03\x04\x05\x06\x07')
while stack.transmitting():
stack.process()
time.sleep(0.0001)
bus.shutdown()

can.loggerも起動しておいて、
上記スクリプトを実行
can.logger起動
> python -m can.logger -c 0 -i vector -f isotpsnd.asc
送信スクリプト実行
> python isotpsndtest.py

特にエラーなく終わったかな。

そしてcan.loggerで計測したcan回線の情報。
Begin Triggerblock
0.000000 Start of measurement
0.000000 1 18DA10F1x Rx d 8 07 01 02 03 04 05 06 07
End TriggerBlock

7byteのメッセージだったから、
シングルフレーム送信になってるね。

そうだね。
問題無く送信出来ていると言える。
そして、先ほどのスクリプトコードのsendメソッドの引数を変更してメッセージ長を伸ばしてみる。
stack.send(b'\x01\x02\x03\x04\x05\x06\x07')
↓
stack.send(b'\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10')

あ、これだとマルチフレームになるやつだね。
実行してみるよ。

あれ?
なんかエラーになった。
> python isotpsndtest.py
Reception of FlowControl timed out. Stopping transmission

ログを見てみよう。
Begin Triggerblock Sat
0.000000 Start of measurement
0.000000 1 18DA10F1x Rx d 8 10 1E 01 02 03 04 05 06
End TriggerBlock

これは・・・
FF(FirstFrame)しか出てないね・・・。

だろうね。

そういえば、
FFを送信した後に送信先からFC(FlowControl)が一回送られてくるんだっけか・・・。

そうそう。正解。

次回はこのFCをうまく対応してみよう。
まとめ

まとめだよ。
- Pythonパッケージのcan-isotpでISO15765-2ことISO-TPの通信ができる。
- ただし、python-canも依存関係の都合上インストールされている必要がある。
- とりあえずSF(SingleFrame)の送信はできた。
- マルチフレーム送信はFF(FirstFrame)で止まってしまってエラー終了となった。
- FFのあとにFCを受信しないと次のシーケンスに進めないプロトコル上の仕様。
バックナンバーはこちら。
コメント