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

yipeiwu_com6年前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自定义scrapy中间模块避免重复采集的方法

本文实例讲述了Python自定义scrapy中间模块避免重复采集的方法。分享给大家供大家参考。具体如下: from scrapy import log from scrapy.htt...

python实现比较两段文本不同之处的方法

本文实例讲述了python实现比较两段文本不同之处的方法。分享给大家供大家参考。具体实现方法如下: # find the difference between two texts #...

python创建临时文件夹的方法

本文实例讲述了python创建临时文件夹的方法。分享给大家供大家参考。具体实现方法如下: import tempfile, os tempfd, tempname = tempfi...

python算法与数据结构之单链表的实现代码

python算法与数据结构之单链表的实现代码

=一、链表 链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生...

pygame游戏之旅 按钮上添加文字的方法

pygame游戏之旅 按钮上添加文字的方法

本文为大家分享了pygame游戏之旅的第11篇,供大家参考,具体内容如下 定义一个button函数,将文字,颜色等作为参数。 def button (msg, x, y, w, h,...