2つのシリアルポートから別々のデータを取り込み、2つのグラフにリアル表示したい
質問を失礼します。
2つのシリアルポートから別々のデータを取り込みたいのですが、2つ上手くいかないことがあります。
とあるところから拾ったコードを修正しながら使用しているのですが、、、
・問題点1
2つのシリアルポートから出力されるデータの間隔がバラバラですが、
p=str(now)+';'+str(valueInInt)+';'+str(twiliteInInt) print(p)
という記述で、1行にまとめようとしています。しかし、twiliteInIntのデータが抜けることがあります。。
それぞれを一定間隔で取り出す方法は無いでしょうか・・。
・問題点2
この記載だと、ax1グラフとax2グラフが交互に表示されてしまいます。。
すみません、初心者でトンチンカンなことを言っているかもしれません。
もし可能でしたら、コードを教えていただけたらありがたいです。
よろしくお願いいたします。
python3.6
1# -*- coding: utf-8 -*- 2import serial 3import matplotlib.pyplot as plt 4import datetime 5import csv 6from drawnow import drawnow 7 8values = [] 9angles=[] 10 11plt.ion() 12cnt=0 13 14serArduino=serial.Serial( 15 port ='COM7', 16 baudrate=57600, 17 parity='N', 18 bytesize=8, 19 #stopbits=1, 20 #timeout=None, 21 #xonxoff=0, 22 ) 23serTwilite=serial.Serial( 24 port='com1', 25 baudrate=115200, 26 parity='N' 27 ) 28 29 30fig=plt.figure() 31def plotValues(): 32 ax1=fig.add_subplot(2,1,1) 33 ax1.plot(values, 'r-',label='values1') 34 ax1.grid(which="both") 35 ax1.set_title("angle") 36 ax1.set_ylabel("deg") 37def plotangles(): 38 ax2=fig.add_subplot(2,1,2) 39 ax2.plot(angles,'b-',label='angles') 40 ax2.grid(which="both") 41 ax2.set_title("torque") 42 ax2.set_ylabel("cNm") 43 fig.tight_layout() 44 45for i in range(0,100): 46 values.append(0) 47 angles.append(0) 48while True: 49 while (serArduino.inWaiting()==0): 50 pass 51 valueRead = serArduino.readline() 52 while (serTwilite.inWaiting()==0): 53 pass 54 twiliteRead = serTwilite.read(2) 55 #check if valid value can be casted 56 try: 57 valueInInt =int(valueRead) 58 twiliteInInt=int(twiliteRead) 59 now=datetime.datetime.now() 60 p=str(now)+';'+str(valueInInt)+';'+str(twiliteInInt) 61 print(p) 62 63 if valueInInt <= 1024: 64 if valueInInt >= 0: 65 angles.append(twiliteInInt) 66 values.append(valueInInt) 67 angles.pop(0) 68 values.pop(0) 69 drawnow(plotangles) 70 drawnow(plotValues) 71 else: 72 print("Invalid! negative number") 73 else: 74 print("Invalid! too large") 75 except ValueError: 76 print("Invalid! cannot cast")
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/07/31 18:04