Skip to content

Instantly share code, notes, and snippets.

@TonyMooori
Last active February 26, 2021 18:20
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TonyMooori/bd276abc8d149ef33225 to your computer and use it in GitHub Desktop.
Save TonyMooori/bd276abc8d149ef33225 to your computer and use it in GitHub Desktop.
pyserialでシリアルポート一覧を表示し選択させて,読み込むプログラム
import serial
from serial.tools import list_ports
import time
def select_port():
ser = serial.Serial()
ser.baudrate = 19200 # ArduinoのSerial.beginで指定した値
ser.timeout = 0.1 # タイムアウトの時間
ports = list_ports.comports() # ポートデータを取得
devices = [info.device for info in ports]
if len(devices) == 0:
# シリアル通信できるデバイスが見つからなかった場合
print("error: device not found")
return None
elif len(devices) == 1:
print("only found %s" % devices[0])
ser.port = devices[0]
else:
# ポートが複数見つかった場合それらを表示し選択させる
for i in range(len(devices)):
print("input %3d: open %s" % (i,devices[i]))
print("input number of target port >> ",end="")
num = int(input())
ser.port = devices[num]
# 開いてみる
try:
ser.open()
return ser
except:
print("error when opening serial")
return None
def main():
ser = select_port()
if ser is None:
return
while ser.is_open:
data = ser.read(256)
if data != b'':
print(data.decode(),end="")
time.sleep(1)
ser.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment