Skip to content

Instantly share code, notes, and snippets.

@pythonsuezo
Last active June 7, 2021 03:40
Show Gist options
  • Save pythonsuezo/5e6810905ceb6b78d5559006a7c769db to your computer and use it in GitHub Desktop.
Save pythonsuezo/5e6810905ceb6b78d5559006a7c769db to your computer and use it in GitHub Desktop.
pythonでシリアル通信をするコード
import serial
# COMポート20でボーレート9600、パリティビットなしで設定
ser = serial.Serial("COM20", baudrate=9600 ,parity=serial.PARITY_NONE)
# ノイズデータがある場合があるのでバッファをクリアする
ser.reset_input_buffer()
# シリアル通信もバイナリ形式で送る
ser.write(b"hello world")
print("send: hello world")
# 受け取ったデータもバイナリ形式
# 引数は受け取る文字数
recv_data = ser.read(1)
print(recv_data)
ser.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment