2016-11-09 72 views
1

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

#!/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

[email protected]:~/Desktop $ sudo python Newpy 
GSM Down 
Resetting Module 
Turning On GSM 
GSM Is Up 



Do you want to powerdown GSM?: 

回答

0

尝试ser.write('AAAAAAAT')this。显然,GSM中断板上的SIM908会尝试自动检测波特率,并且必须为此提供一些额外的数据。

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

+0

我特别设置了波特率,但是我会试一试。 – iNoob

+0

@iNoob听起来不错!我看到你正在专门设置RPi上的波特率,但这并不意味着GSM具有相同的设置。当我读到你的症状时,我首先想到的是“波特率不匹配!”所以我检查了谷歌,看看它是否发生在其他人身上。 – cxw

+0

我仍然获得奇数输出'GSM向下 设模块 开启GSM GSM是最多 IIIIIIIIIIII 你想关机GSM:' – iNoob