ネットの情報は簡単な例ばかり。結果実験してみないと分からない。シリアル通信をモニタしながら自作の治具とあれこれとやってみたら何とかなった。シリアルで、介在するCPU(Atmega16L)にコマンドとしてI2C送受を行わせる。
CPUから治具へ(MCP9600基板キット)I2C通信を行わせ温度データをPCに返信させる。
pythonはこういう簡単な確認にはいいけど、最後はc#でGUIになるね。
Pythonだけどデバッグ中に不適な構文エラーがあるとやたらCUI画面が落ちる・・・。
大丈夫かと言いたくなる。例外処理など入れないとだろうか。
●環境:Win7 python3.6
●ターゲット:MCP9600熱電対I2C基板
import serial
import time
ma = input('How many times is it? >')
m= int(ma)
ser = serial.Serial("COM1",19200,8)
# Get Firmware Version
#cmd = bytearray([0x00,0x02,0x66,0x00,0x0d,0x0a])
cmd = bytearray([0x00,0x02,0xCE,0x00,0x00,0x0d,0x0a])
#print(cmd)
#bytearray(b'\x00\x02f\x00\r\n') と表示される
#以下は当たり前な表現で、byteを指定で扱っている。
#ser.write(b'test\n')
n = 0
mcnt = 0
while mcnt < m:
print( '---(',mcnt+1,')-----')
while n<5:
if n == 0:
cmd[2] = 0xCE
if n == 1:
cmd[2] = 0xCC
if n == 2:
cmd[2] = 0xCA
if n == 3:
cmd[2] = 0xC8
if n == 4:
cmd[2] = 0xC6
ser.write(cmd)
d = ser.readline()
data = d.rstrip()
#print( data )
temp = ((d[0] * 256 ) + d[1] ) * 0.0625
print( '{:02X}'.format(cmd[2]),':','{:.1f}'.format(temp ))
#250msecの変換時間が必要だ
time.sleep(0.4)
n +=1
n = 0
mcnt +=1
ser.close()
No tags for this post.


