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

相关文章

在matplotlib的图中设置中文标签的方法

在matplotlib的图中设置中文标签的方法

其实就是通过 FontProperties来设置的,请参考以下代码: import matplotlib.pyplot as plt from matplotlib.font_man...

python版学生管理系统

python版学生管理系统

写一个学生管理系统,最好用python。 我都没学过python呢,只好开始临时抱佛脚,再到网上找找有没有例子看看,下面是我参照另一个博主写的,中间有一些和我不能融合的错误,我已经解决了...

跟老齐学Python之再深点,更懂list

list解析 先看下面的例子,这个例子是想得到1到9的每个整数的平方,并且将结果放在list中打印出来 >>> power2 = [] >>> f...

python3 实现验证码图片切割的方法

python3 实现验证码图片切割的方法

切割前图片 切割后四个图片 代码 #coding:utf8 import os from PIL import Image,ImageDraw,ImageFile import...

python开发之IDEL(Python GUI)的使用方法图文详解

python开发之IDEL(Python GUI)的使用方法图文详解

本文讲述了python开发之IDEL(Python GUI)的使用方法。分享给大家供大家参考,具体如下: 在安装完Python后,我们希望能够运用python GUI来运行一些我们编写的...