Home  >  Article  >  Backend Development  >  Python操作串口的方法

Python操作串口的方法

WBOY
WBOYOriginal
2016-06-10 15:10:281680browse

本文实例讲述了Python操作串口的方法。分享给大家供大家参考。具体如下:

首先需确保安装了serial模块,如果没安装的话就安装一下python-pyserial。

一个Python实现的串口Echo

import serial 
import sys 
try: 
  ser = serial.Serial('/dev/ttyUSB0', 9600) 
except Exception, e: 
  print 'open serial failed.' 
  exit(1) 
print 'A Serial Echo Is Running...' 
while True: 
  # echo 
  s = ser.read() 
  ser.write(s) 
  # write to stdout and flush it 
  sys.stdout.write(s) 
  sys.stdout.flush()

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn