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设计】。

相关文章

Django文件存储 默认存储系统解析

Django默认使用的文件存储系统'django.core.files.storage.FileSystemStorage'是一个本地存储系统,由settings中的DEFAULT_FI...

Python之pandas读写文件乱码的解决方法

python读写文件有时候会出现   ‘XXX'编码不能打开XXX什么的,用记事本打开要读取的文件,另存为UTF-8编码,然后再用py去读应该可以了。如果还不行,那...

Python序列类型的打包和解包实例

打包 如给出一系列由逗号分隔的表达式,他们将被视为一个单独元组,即使没有提供封闭的圆括号 如: numbers = 1, 2, 3, 4 使numbers被赋值元组(1, 2, 3...

Python 内置函数globals()和locals()对比详解

这篇文章主要介绍了Python globals()和locals()对比详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Pytho...

Python with的用法

在Python中,with关键字是一个替你管理实现上下文协议对象的好东西。例如:file等。示例如下:    from __future__ import wi...