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应用文件读取与登录注册功能

python应用文件读取与登录注册功能,具体实现代码如下所示: #!/usr/bin/python3 # -*- coding: utf-8 -*- # Author: zhw #读...

python根据list重命名文件夹里的所有文件实例

如下所示: # coding = utf-8 import os path = "D:\\chunyu"#想要重命名所有文件存放的文件夹 filelist = os.listdir(...

利用Python中的pandas库对cdn日志进行分析详解

前言 最近工作工作中遇到一个需求,是要根据CDN日志过滤一些数据,例如流量、状态码统计,TOP IP、URL、UA、Referer等。以前都是用 bash shell 实现的,但是当日志...

Windows下用py2exe将Python程序打包成exe程序的教程

py2exe在sourceforge 的下载只支持到2.7。 针对python3.0+的版本,需要自己编译。 1.下载源码 svn checkout svn://svn.code.sf....

python 多进程并行编程 ProcessPoolExecutor的实现

使用 ProcessPoolExecutor from concurrent.futures import ProcessPoolExecutor, as_completed im...