Python操作串口的方法

yipeiwu_com6年前Python基础

本文实例讲述了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程序设计有所帮助。

相关文章

详解Python3中的Sequence type的使用

其实本来是要reverse一下list的,就去查了一下list[::-1]是什么意思,发现还有很多要注意的地方,所以就记一下。 主要是参照https://docs.python.org/...

python端口扫描系统实现方法

本文实例讲述了python端口扫描系统实现方法。分享给大家供大家参考。具体实现方法如下: 该程序的主要功能如下: 1. 从自有API接口获取所有的外网IP段; 2. 用Nmap 遍历扫描...

Python 多线程搜索txt文件的内容,并写入搜到的内容(Lock)方法

废话不多说,直接上代码吧! import threading import os class Find(threading.Thread): #搜索数据的线程类 def __i...

python解析json串与正则匹配对比方法

现在有如下格式的json串: “detail_time”:”2016-03-30 16:00:00”,”device_id”:”123456”,”os”:”Html5Wap”,”s...

opencv3/C++实现视频背景去除建模(BSM)

opencv3/C++实现视频背景去除建模(BSM)

视频背景建模主要使用到: 高斯混合模型(Mixture Of Gauss,MOG) createBackgroundSubtractorMOG2(int history=500, d...