簡體   English   中英

如何在Arduino中讀取AT命令的輸出?

[英]How do I read the output of an AT command in Arduino?

如何在Arduino上捕獲AT命令的輸出?

我正在使用帶有GSM屏蔽的Arduino Uno R3。 我有所有的AT命令( 它們可以在這里看到 ),如果我使用終端並獲得輸出,我可以輸入它們。 但是,如何通過代碼捕獲結果輸出? 下面的代碼顯示了我嘗試過的但它不起作用。 特別是在我嘗試獲取模擬輸入然后打印出結果的地方。

#include <SoftwareSerial.h>

SoftwareSerial mySerial(7, 8);

void setup()
{
  char sensorValue[32] ="";
  Serial.begin(9600); 
  mySerial.begin(9600); 
  Serial.println("\r");

  //Wait for a second while the modem sends an "OK"
  delay(1000);                    

  //Because we want to send the SMS in text mode
  Serial.println("AT+CMGF=1\r");    
  delay(1000);

  mySerial.println("AT+CADC?");     //Query the analog input for data
  Serial.println(Serial.available());    
  Serial.println(Serial.read());    //Print out result???

  //Start accepting the text for the message
  //to be sent to the number specified.
  //Replace this number with the target mobile number.
  Serial.println("AT+CMGS=\"+MSISDN\"\r");    


  delay(1000);
  Serial.println("!");   //The text for the message
  delay(1000);
  Serial.write(26);  //Equivalent to sending Ctrl+Z 
}

void loop()
{
  /*
    if (mySerial.available())
    Serial.write(mySerial.read());
  if (Serial.available())
    mySerial.write(Serial.read());  
    */
}

我得到了輸出:

AT + CMGF = 1

AT + CADC? 21 13

要么

AT + CMGF = 1

AT + CADC? 18 65

無論我的模擬源發生什么變化

請在此處查看 SoftwareSerial read功能的文檔。

當您從GSM設備串行接口讀取時,您不能理所當然地認為緩沖區中有要讀取的字節。

mySerial.read()很可能返回-1 (沒有可用的字節),因為Arduino在GSM設備可以在串行端口上提供某些內容之前運行該代碼。

您應該使用available功能( 此處的文檔)來測試串行接口的傳入字節。 您可以在超時時使用它以避免無限等待。

你可以嘗試的最好的事情是編寫一個單獨的class來處理串行操作(讀,寫,超時,延遲等)。

另外,我曾為Arduino寫過一次GPRS驅動程序。 我的電源問題要求我在GPRS設備上安裝一個額外的電容,並使用輸出電流超過2A的電源。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM