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脚本如何简化jar操作命令

前言 本篇和大家分享的是使用python简化对jar包操作命令,封装成简短关键字或词,达到操作简便的目的。最近在回顾和构思shell脚本工具,后面一些文章应该会分享shell内容,希望大...

Python使用MyQR制作专属动态彩色二维码功能

Python使用MyQR制作专属动态彩色二维码功能

Python中有一个非常有趣好玩的库MyQR,不仅可以制作各种漂亮的二维码,还可以生成动态彩色二维码。 MyQR是一个能够生成自定义二维码的第三方库,你可以根据需要生成普通二维码、带图片...

详解Python中内置的NotImplemented类型的用法

它是什么?   >>> type(NotImplemented) <type 'NotImplementedType'> NotImpl...

浅谈python的dataframe与series的创建方法

如下所示: # -*- coding: utf-8 -*- import numpy as np import pandas as pd def main(): s = pd.S...

Python标准库内置函数complex介绍

本函数可以使用参数real + imag*j方式创建一个复数。也可以转换一个字符串的数字为复数;或者转换一个数字为复数。如果第一个参数是字符串,第二个参数不用填写,会解释这个字符串且返回...