Python使用线程来接收串口数据的示例

yipeiwu_com5年前Python基础

如下所示:

#!/usr/bin/env python
import serial
import time
import thread
 
class MSerialPort:
	message=''
	def __init__(self,port,buand):
		self.port=serial.Serial(port,buand)
		if not self.port.isOpen():
			self.port.open()
	def port_open(self):
		if not self.port.isOpen():
			self.port.open()
	def port_close(self):
		self.port.close()
	def send_data(self,data):
		number=self.port.write(data)
		return number
	def read_data(self):
		while True:
			data=self.port.readline()
			self.message+=data
if __name__=='__main__':
	mSerial=MSerialPort('/dev/ttyACM0',9600)
	thread.start_new_thread(mSerial.read_data,())
	while True:
		time.sleep(1)
		print mSerial.message
		print 'next line'
 

以上这篇Python使用线程来接收串口数据的示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python3实现带多张图片、附件的邮件发送

本文实例为大家分享了python3实现多张图片附件邮件发送的具体代码,供大家参考,具体内容如下 直接上代码,没有注释! from email.mime.text import MIM...

使用PyInstaller将python转成可执行文件exe笔记

1、安装PyInstaller PyInstaller的作用如标题所说,首先需要下载PyInstaller和UPX,UPX是用来压缩exe的,点击超链接下载吧,目前稳定版本是1.3,注意...

python根据出生日期获得年龄的方法

本文实例讲述了python根据出生日期获得年龄的方法。分享给大家供大家参考。具体如下: 这段代码可以根据用户的出生日期获得其年龄,born参数为date类型 def calculat...

在Debian下配置Python+Django+Nginx+uWSGI+MySQL的教程

最近尝试把项目迁移到Python环境下,特别新装了一台干净的Debian系统,准备重新配置环境,上网找了一些运行Python Web的环境方案,最后敲定Nginx+uWSGI组合,Ngi...

Python enumerate函数遍历数据对象组合过程解析

这篇文章主要介绍了Python enumerate函数遍历数据对象组合过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 介绍...