简体   繁体   English

如何在Arduino串口中打印对AT命令的响应?

[英]How to print response to AT commands in Arduino serial port?

I want to print the response to AT command. 我想打印对AT命令的响应。 I'm sending AT command but I'm not getting any response in the Arduino serial port. 我正在发送AT命令,但在Arduino串行端口中没有任何响应。 It's giving -1 instead of OK. 它给出-1而不是OK。

#include "SoftwareSerial.h"
String ssid = "connectify-krish";
String password = "12345678";

String myword= "";
SoftwareSerial esp(10, 11);
void setup() {
  Serial.begin(9600);
  esp.begin(9600);
  esp.write("AT");
  Serial.println(esp.read());
}

void loop() {}

As already pointed out in the comments you are not terminating the AT command line, so the modem will never respond to this. 正如注释中已经指出的那样,您不会终止AT命令行,因此调制解调器将永远不会对此做出响应。

Make sure you read V.250 and learn the difference between an AT command and an AT command line. 确保您阅读了V.250,并了解了AT命令和AT命令行之间的区别。 ATI is an AT command, and "ATI II\\r" is a command line invoking this command three times in a row. ATI是一个AT命令,而"ATI II\\r"是一个连续三次调用此命令的命令行。 Notice by the way in this example that you will get just one single Final result code for all three of them, ie the Final result code is a response to a complete command line and not to individual command invocations. 顺便提一下,在此示例中,您将只为全部三个获得一个最终结果代码,即,最终结果代码是对完整命令行的响应,而不是对单个命令调用的响应。

Then after fixing the sending of the command you must implement proper handling of the response. 然后,在更正命令的发送之后,您必须对响应进行正确的处理。 This includes reading and parsing what the modem sends back as a response to the sent command line. 这包括读取和解析调制解调器作为响应发送的命令行而发送回的内容。 See this answer for the code structure and implementation hints. 有关代码结构和实现提示,请参见此答案

As you've been told, terminate your AT commands with a carriage return character \\r . 如您所知,请使用回车符\\r终止AT命令。 Also you current code will read only a byte of the response, and thats if the response has even arrived since you included no delay at all. 同样,您当前的代码将只读取响应的一个字节,那就是响应是否到达,因为您根本没有延迟。 To communicate with the ESP interactively with the Serial monitor, I'd recommend using this: 要与串行监视器以交互方式与ESP通信,建议使用以下命令:

#include <SoftwareSerial.h>

SoftwareSerial esp(10, 11);
void setup(){
  Serial.begin(9600);
  esp.begin(9600);
}

void loop()
{
  while (Serial.available())  // forward data from monitor to esp
    esp.write(Serial.read());
  while (esp.available())  // forward data from esp to monitor
    Serial.write(esp.read());
}

This basically makes your Arduino a conduit for communication between your PC and the ESP. 基本上,这使Arduino成为PC与ESP之间通信的通道。 You can send commands to the ESP with the Serial monitor and get their results immediately. 您可以使用串行监视器将命令发送到ESP,并立即获得其结果。 Its great for testing commands. 非常适合测试命令。 Just remember to set the serial monitor to BOTH NL & CR ; 只记得将串行监视器设置为BOTH NL & CR this will serve you well for commands as well as any HTTP requests you send, as it appends \\r\\n to everything you send. 这将对命令以及您发送的所有HTTP请求都非常有用,因为它将\\r\\n附加到您发送的所有内容中。

If you do want to write a sketch to talk to the ESP, you must provide some delay after sending a command to wait for the module to process the command and respond. 如果确实要编写草图与ESP对话,则在发送命令后必须提供一些延迟,以等待模块处理该命令并做出响应。 The delay varies depending on the command, at least 500ms. 延迟取决于命令,至少500ms。 The usual procedure is to define a timeout period for each command, depending on how long its expected to take, after which you 'give up' if there's no response yet. 通常的过程是为每个命令定义一个超时时间,具体取决于期望的时间,如果没有响应,您可以“放弃”。 There are lots of libraries on GitHub that involve talking to some device using AT commands; GitHub上有很多库,涉及使用AT命令与某些设备对话; study them to learn their techniques. 学习他们以学习他们的技术。

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

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