Python3 で USB400J と STM431J を使う

EnOcean の USB400J受信用USBモジュール　と STM431J温度センサーモジュールを次の環境で使いました。

Raspberry Pi

>```text
Linux raspberry 4.14.34-v7+ #1110 SMP Mon Apr 16 15:18:51 BST 2018 armv7l GNU/Linux
$ python3 --version
Python 3.5.3
```

Arch Linux

>```text
Linux arch 4.17.12-arch1-1-ARCH #1 SMP PREEMPT Fri Aug 3 07:16:41 UTC 2018 x86_64 GNU/Linux
$ python --version
Python 3.7.0
```

次のサイトにあるプログラムを改造しました。
[PythonでEnOceanの電文を読み取る ](http://hanzomemo.blogspot.com/2015/12/pythonenocean.html)

```py:read_serial_port.py
#! /usr/bin/python3
# -*- coding: utf-8 -*-
#
#	read_serial_port.py
#
#						Aug/07/2018
# --------------------------------------------------------------------
from serial import *
from sys import exit
from datetime import datetime
import codecs
# --------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
port = '/dev/ttyUSB0'
 
# シリアルポートを開く
try:
	ser = Serial(port, 57600)
	print('open serial port: %s' % port)
except:
	print('cannot open serial port: %s' % port)
	exit(1)
 
# 初期化
cnt,dataLen,optLen = 0,0,0
telegraph,headList,dataList,optList = [],[],[],[]
ready = True # 電文開始のフラグ管理
 
# データの解釈とログの記録
while True:
#
	tt = ser.read()
	ss = codecs.encode(tt, 'hex_codec')
#	print(ss)
	if ss == b'55' and ready: # 電文開始
		# 変数のリセット
		cnt,dataLen,optLen = 0,0,0
		telegraph,headList,dataList,optList = [],[],[],[]
		ready = False
		print ('==========')
	cnt += 1
	telegraph.append(ss.decode('utf-8'))
	if 2 <= cnt <= 5: # header
		headList.append(ss.decode('utf-8'))
	if cnt == 5: # header終了, data length取得
		dataLen = int(headList[1],16)
		optLen  = int(headList[2],16)
	if 7 <= cnt <= (6+dataLen): # data
		dataList.append(ss.decode('utf-8'))
	if (7+dataLen) <= cnt <= (6+dataLen+optLen): # optional data
		optList.append(ss.decode('utf-8'))
	if cnt == (6+dataLen+optLen+1): # 電文終了
		ready = True
		dt = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
		# ログ出力
		print (dt)
		print (':'.join(telegraph))
		print ('head...', ':'.join(headList))
		print ('data...', ':'.join(dataList), '(length=%d)' % dataLen)
		print ('opt ...', ':'.join(optList),  '(length=%d)' % optLen)
		sensorId = ':' . join(dataList[1:5]) # センサID取得
		print ('sensorId ...', sensorId)
		# マグネットセンサ
		if sensorId == '04:00:03:df':
			if   dataList[5] == '08':
				action = 'open'
			elif dataList[5] == '09':
				action = 'close'
			print ('door...', action)
		# 温度センサ
#		elif sensorId == '04:00:7a:fc':
		elif sensorId == '00:00:04:01':
			val = int(dataList[7],16)
			temp = (255.0-val)/255.0*40.0
			print ('temp...', temp)
		# 上記以外のセンサIDは無視
		else:
			continue
# --------------------------------------------------------------------
```

使い方
>STM431J の LRN ボタンを押すとデータが送られます。

```text
# ./read_serial_port.py 
*** 開始 ***
open serial port: /dev/ttyUSB0
==========
2018-08-07 20:12:09
55:00:0c:02:0a:e6:62:00:00:04:01:2e:69:08:28:0b:80:78:01:4d:f1
head... 00:0c:02:0a
data... 62:00:00:04:01:2e:69:08:28:0b:80:78 (length=12)
opt ... 01:4d (length=2)
sensorId ... 00:00:04:01
temp... 38.745098039215684
==========
2018-08-07 20:12:10
55:00:0c:02:0a:e6:62:00:00:04:01:2e:69:08:28:0b:80:78:01:47:c7
head... 00:0c:02:0a
data... 62:00:00:04:01:2e:69:08:28:0b:80:78 (length=12)
opt ... 01:47 (length=2)
sensorId ... 00:00:04:01
temp... 38.745098039215684
==========
2018-08-07 20:15:43
55:00:0a:02:0a:9b:22:04:01:2e:69:00:00:50:08:0d:01:39:ba
head... 00:0a:02:0a
data... 22:04:01:2e:69:00:00:50:08:0d (length=10)
opt ... 01:39 (length=2)
sensorId ... 04:01:2e:69
```