【问题标题】:Using pyserial to write and read AT command data from GSM module使用pyserial从GSM模块读写AT命令数据
【发布时间】:2023-04-08 12:12:01
【问题描述】:

我正在使用 Raspberry Pi、Breakout BoardGSM 模块。我遇到了串行读取数据的问题。它似乎非常分散,有很多空白行和各种奇怪的条目,看起来来自终端(ssh 登录字段提示)。我只是试图读取 AT 命令的输入响应。为我的 GSM 阅读AT Command manual,回复非常具体,所以我只想阅读我正在寻找的回复。我是这样的串行接口新手,所以任何关于最佳实践的建议也将不胜感激。

#!/usr/bin/python

import time
import serial
import traceback
import netifaces as ni
import RPi.GPIO as GPIO


def resetModule(resetModulePin):
    GPIO.output(resetModulePin, GPIO.HIGH)
    time.sleep(0.5)
    GPIO.output(resetModulePin, GPIO.LOW)
    time.sleep(0.1)


def switch(onModulePin):
    GPIO.output(onModulePin, GPIO.HIGH)
    time.sleep(2)
    GPIO.output(onModulePin, GPIO.LOW)


def writeAT():
    i = 0
    while True:
        ser.flushInput()
        ser.flush()
        out = ''
        ser.write('AT\r')
        time.sleep(1)
        if ser.inWaiting() > 0:
            out += ser.read(12)
            print 'GSM Is Up'
            return out
        else:
            i += 1
        if i > 3:
            print 'GSM Down'
            print 'Resetting Module'
            resetModule(resetModulePin)
            time.sleep(5)
            print 'Turning On GSM'
            switch(onModulePin)
            time.sleep(5)
            i = 0



try:
    resetModulePin = 15
    onModulePin = 13

    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(onModulePin , GPIO.OUT)
    GPIO.setup(resetModulePin, GPIO.OUT)

    ser = serial.Serial(port='/dev/ttyAMA0', baudrate=115200)

    ser.isOpen()
    answers = ['yes', 'y']
    while True:
        out = writeAT()
        if out != '':
            print out
            break
    question = raw_input('Do you want to powerdown GSM?:').lower()
    if question in answers:
        print 'Powering Off GSM'
        switch(onModulePin)
        ser.close()
    GPIO.cleanup()
except KeyboardInterrupt, e:
    GPIO.cleanup()
except Exception,e :
    traceback.print_exc()
    GPIO.cleanup()

OUTPUT(注意空行)#Debugging

pi@raspberrypi:~/Desktop $ sudo python Newpy
GSM Down
Resetting Module
Turning On GSM
GSM Is Up



Do you want to powerdown GSM?:

【问题讨论】:

    标签: python raspberry-pi gsm pyserial


    【解决方案1】:

    按照this 尝试ser.write('AAAAAAAT')。显然 GSM 分线板上的 SIM908 试图自动检测波特率,你必须给它一些额外的数据才能这样做。

    编辑或尝试this (post of Tue Nov 25, 2014 11:41 pm) — 检查 SIM908 上“充电”跳线的设置。

    【讨论】:

    • 我是专门设置波特率的,不过我试试看。
    • @iNoob 听起来不错!我看到您在 RPi 上专门设置了波特率,但这并不意味着 GSM 具有相同的设置。当我阅读您的症状时,我首先想到的是“波特率不匹配!”所以我在谷歌上查看了它是否发生在其他人身上。
    • 我仍然得到奇怪的输出GSM Down Resetting Module Turning On GSM GSM Is Up IIIIIIIIIIII Do you want to powerdown GSM?:
    • 我没有使用 arduino 我的 sim908 没有任何跳线
    • @iNoob 抱歉 - 需要注销。仔细检查 p。您链接的手册中的 13 个 - 我没有看到您的代码在等待 RDY。另外,请尝试AT\r\n,即使手册说您只需要<CR> (\r) --- 我不确定我是否相信手册的那部分内容。祝你好运!
    猜你喜欢
    • 1970-01-01
    • 2014-06-25
    • 1970-01-01
    • 1970-01-01
    • 2010-10-14
    • 2015-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多