简体   繁体   English

如何在Arduino中读取AT命令的输出?

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

How do I capture the output from an AT command on an Arduino? 如何在Arduino上捕获AT命令的输出?

I'm using the Arduino Uno R3 with a GSM shield. 我正在使用带有GSM屏蔽的Arduino Uno R3。 I have all the AT commands ( they can be seen here ) and I can enter them just fine if I use the terminal and get output. 我有所有的AT命令( 它们可以在这里看到 ),如果我使用终端并获得输出,我可以输入它们。 However how can I capture the resulting output via code? 但是,如何通过代码捕获结果输出? The code below shows what I've tried but it does not work. 下面的代码显示了我尝试过的但它不起作用。 In particular where I attempt to get the analog input and then print out the result. 特别是在我尝试获取模拟输入然后打印出结果的地方。

#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());  
    */
}

I get the outputs: 我得到了输出:

AT+CMGF=1 AT + CMGF = 1

AT+CADC? AT + CADC? 21 13 21 13

or 要么

AT+CMGF=1 AT + CMGF = 1

AT+CADC? AT + CADC? 18 65 18 65

Regardless of changes in my analog source 无论我的模拟源发生什么变化

Take a look at the documentation of the SoftwareSerial read function here . 请在此处查看 SoftwareSerial read功能的文档。

When you read from the GSM device serial interface, you cannot take for granted that there are bytes to be read on the buffer. 当您从GSM设备串行接口读取时,您不能理所当然地认为缓冲区中有要读取的字节。

It's very likely that mySerial.read() returns -1 (no bytes available), as Arduino runs that code before the GSM device can provide something on the serial port. mySerial.read()很可能返回-1 (没有可用的字节),因为Arduino在GSM设备可以在串行端口上提供某些内容之前运行该代码。

You should use the available function (documentation here ) to test the serial interface for incoming bytes. 您应该使用available功能( 此处的文档)来测试串行接口的传入字节。 You could use it with a timeout to avoid infinite waiting. 您可以在超时时使用它以避免无限等待。

The best thing you could try is to write a separate class to handle serial operations (read, write, timeouts, delays, etc). 你可以尝试的最好的事情是编写一个单独的class来处理串行操作(读,写,超时,延迟等)。

Also, I wrote a GPRS driver for Arduino once. 另外,我曾为Arduino写过一次GPRS驱动程序。 I had a problem with the power supply that required me to install an extra capacitor on the GPRS device and use a power supply with more than 2A of output current. 我的电源问题要求我在GPRS设备上安装一个额外的电容,并使用输出电流超过2A的电源。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM