Created
February 11, 2016 15:13
-
-
Save TonyMooori/412322f9aaa5ce9dd8e4 to your computer and use it in GitHub Desktop.
シリアルポートからpyserialでデータを受け取る
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding:utf-8 | |
import time | |
import serial | |
import sys | |
""" | |
参考: pyserial公式ドキュメント | |
[1]サイトトップ http://pythonhosted.org/pyserial/ | |
[2]API一覧 http://pythonhosted.org/pyserial/pyserial_api.html | |
[3]イントロダクション http://pythonhosted.org/pyserial/shortintro.html | |
""" | |
if __name__ == "__main__": | |
ser = serial.Serial() | |
ser.baudrate = 9600 # ArduinoのSerial.beginで指定した値 | |
ser.timeout = 0 # タイムアウトの時間 | |
ser.port = "COM3" # Arduinoのポート番号.場合によって違うので注意!!! | |
# 開いてみる | |
try: | |
ser.open() | |
print("open " + ser.port ) | |
except: | |
print("can't open" + ser.port ) | |
sys.exit(0) | |
# ちゃんと開けていたらループに入る | |
while ser.is_open: | |
s = ser.read(100) | |
if s != "": | |
print(s) | |
else: | |
print(".") | |
time.sleep(0.1) # 0.1秒待つ | |
print("serial connection closed") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment