トップ 差分 一覧 ソース 検索 ヘルプ RSS ログイン

PRG-PY-serial

 pyserial

インストール

pip3 install pyserial

読み出し

#!/bin/env python3
import serial

def main():

   # '/dev/ttyUSB0'は適宜変更
   # 10秒まってもデータが来ない場合にはタイムアウト
   # ser = serial.Serial('/dev/ttyUSB0',9600,timeout=10)   
   
   # with serial.Serial('/dev/ttyUSB0',9600,timeout=10) as ser :
   with serial.Serial('/dev/ttyUSB0',115200,timeout=10) as ser :
       while True:
         line = ser.readline()
         # print(line.decode(encoding='utf-8',errors='replace'))
         print(str(line,encoding='utf-8',errors='replace'))

       ser.close()
 
 if __name__ == "__main__":
     main()

SAMPLE

## PC 側
import serial
# pip3 install parse
from parse import *

def main():
 with serial.Serial('/dev/ttyUSB0',115200,timeout=2) as ser :
     while True:
         line = ser.readline()
         lined = line.decode(encoding='utf-8',errors='replace')
         r = parse ( "{name:w}: ({datas})\r", lined )
         r.fixed
         print (r['name'])
     ser.close()

if __name__ == "__main__":
   main()
#MicroPython側
import utime
from random import randint
from machine import I2C, Pin, sleep
from mpu9250 import MPU9250

i2c = I2C(scl=Pin(22), sda=Pin(21), freq=200000)
sensor = MPU9250(i2c)
print("MPU9250 id: " + hex(sensor.whoami))

while True:
   print('acceleration:', sensor.acceleration)
   print('gyro:', sensor.gyro)
   print('magnetic:', sensor.magnetic)
   sleep(300)
   #print('\n')

madgwick filter