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中实现字符串类型与字典类型相互转换的方法

本文以实例形式简述了Python中字符串类型与字典类型相互转换的方法,是比较实用的功能。具体方法如下: 一、字典(dict)转为字符串(string) 我们可以比较容易的将字典(dict...

使用python实现链表操作

使用python实现链表操作

一、概念梳理 链表是计算机科学里面应用应用最广泛的数据结构之一。它是最简单的数据结构之一,同时也是比较高阶的数据结构(例如棧、环形缓冲和队列) 简单的说,一个列表就是单数据通过索引集合在...

python requests更换代理适用于IP频率限制的方法

python requests更换代理适用于IP频率限制的方法

有些网址具有IP限制,比如同一个IP一天只能点赞一次。 解决方法就是更换代理IP。 从哪里获得成千上万的IP呢? 百度“http代理” 可获得一大堆网站。 比如某代理网站,1天...

centos6.5安装python3.7.1之后无法使用pip的解决方案

编译安装全是坑…… 第一遍装完无法使用pip,报错找不到ssl模块。各种报错: pip is configured with locations that require TLS/SS...

python使用Pandas库提升项目的运行速度过程详解

python使用Pandas库提升项目的运行速度过程详解

前言 如果你从事大数据工作,用Python的Pandas库时会发现很多惊喜。Pandas在数据科学和分析领域扮演越来越重要的角色,尤其是对于从Excel和VBA转向Python的用户。...