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

相关文章

python聚类算法解决方案(rest接口/mpp数据库/json数据/下载图片及数据)

python聚类算法解决方案(rest接口/mpp数据库/json数据/下载图片及数据)

1. 场景描述 一直做java,因项目原因,需要封装一些经典的算法到平台上去,就一边学习python,一边网上寻找经典算法代码,今天介绍下经典的K-means聚类算法,算法原理就不介绍...

python sqlobject(mysql)中文乱码解决方法

UnicodeEncodeError: 'latin-1' codec can't encode characters in position; 找了一天终于搞明白了,默认情况下,mys...

Django框架创建项目的方法入门教程

Django框架创建项目的方法入门教程

本文实例讲述了Django框架创建项目的方法。分享给大家供大家参考,具体如下: Django 管理工具 安装 Django 之后,就有了可用的管理工具 django-admin.py。我...

Python3之不使用第三方变量,实现交换两个变量的值

method 1: a,b = b,a method 2: a = a+b b = a-b a = a-b 以上这篇Python3之不使用第三方变量,实现交换两个变量的...

python时间序列按频率生成日期的方法

有时候我们的数据是按某个频率收集的,比如每日、每月、每15分钟,那么我们怎么产生对应频率的索引呢?pandas中的date_range可用于生成指定长度的DatetimeIndex。 我...