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

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

相关文章

Python线上环境使用日志的及配置文件

目录 瞎比比 与 print 相比 logging 有什么优势? 基础用法 保存到文件 多模块使用 logging 使用配置文件配置 logging 瞎...

python 获取毫秒数,计算调用时长的方法

如题:在python的函数调用中需要记录时间,下面是记录毫秒时间的方法。 import datetime import time t1 = datetime.datetime.now...

Python While循环语句实例演示及原理解析

Python While循环语句实例演示及原理解析

这篇文章主要介绍了Python While循环语句实例演示及原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 Python 编程...

Python利用matplotlib绘制约数个数统计图示例

Python利用matplotlib绘制约数个数统计图示例

本文实例讲述了Python利用matplotlib绘制约数个数统计图。分享给大家供大家参考,具体如下: 利用Python计算1000以内自然数的约数个数,然后通过matplotlib绘制...

在Python中操作字符串之replace()方法的使用

 replace()方法返回当前old换成new,可选择的替代限制到最大数量的字符串的副本。 语法 以下是replace()方法的语法: str.replace(old,...