Python读取串口

在物联网的实验中,我们经常会用一些简单的Python脚本来读取串口通过终端显示。

Python读取串口并在终端显示,界面虽然不好看,但是有时候可以用来调试程序。

import serial           
import sys
try:
  ser = serial.Serial('COM4', 9600)
except Exception, e:
  print 'open serial failed.'
  exit(1)
print 'The temprature is'
while True:
  # echo
  s = ser.read()
  if(s!='#'):
    ser.write(s)
  # write to stdout and flush it
    sys.stdout.write(s)
    sys.stdout.flush()



你可能感兴趣的:(Python)