Python3简单实现串口通信的方法

yipeiwu_com7年前Python基础

如下所示:

import serial
import sys
import os
import time
import re
 
def wait_for_cmd_OK():
    while True:
        line = ser.readline()
        try:
            print(line.decode('utf-8'),end='')
        except:
            pass
        if ( re.search(b'OK',line)):
            break
 
def sendAT_Cmd(serInstance,atCmdStr):
    serInstance.write(atCmdStr.encode('utf-8'))
    wait_for_cmd_OK()
 
ser = serial.Serial("/dev/ttyACM0",9600,timeout=30) #选择串口号及波特率,因为我是在ubuntu下使用,故串口号为/dev/ttyACM0
sendAT_Cmd(ser,'AT+CFUN=1\r')
ser.close() 

以上这篇Python3简单实现串口通信的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

python中__call__内置函数用法实例

本文实例讲述了python中__call__内置函数的用法。分享给大家供大家参考。具体分析如下: 对象通过提供__call__(slef, [,*args [,**kwargs]])方法...

详细解读Python中解析XML数据的方法

Python可以使用 xml.etree.ElementTree 模块从简单的XML文档中提取数据。 为了演示,假设你想解析Planet Python上的RSS源。下面是相应的代码:...

python pyheatmap包绘制热力图

利用python pyheatmap包绘制热力图,供大家参考,具体内容如下 import matplotlib.pyplot as plt from pyheatmap.heatma...

pytorch 实现张量tensor,图片,CPU,GPU,数组等的转换

1, 创建pytorch 的Tensor张量: torch.rand((3,224,224)) #创建随机值的三维张量,大小为(3,224,224) torch.Tensor([...

Python中单例模式总结

一、单例模式     a、单例模式分为四种:文件,类,基于__new__方法实现单例模式,基于metaclass方式实现    ...