我的目的是使用GSM SIM800L coreboard 和Arduino UNO发送SMS。这是代码
#include <SoftwareSerial.h>
//Create a software serial object to communicate with SIM800L
SoftwareSerial mySerial(3, 2); //SIM800L Tx & Rx is connected to Arduino #3 & #2
void setup()
{
//Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
Serial.begin(115200);
//Begin serial communication with Arduino and SIM800L
mySerial.begin(115200);
Serial.println("Initializing...");
delay(1000);
mySerial.println("AT"); //Once the handshake test is successful, it will back to OK
updateSerial();
mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
updateSerial();
mySerial.println("AT+CMGS=\"+ZZxxxxxxxxx\"");//change ZZ with country code and xxxxxxxxxxx with phone number to sms
updateSerial();
mySerial.print("TEST"); //text content
updateSerial();
mySerial.write(26);
}
void loop()
{
}
void updateSerial()
{
delay(500);
while (Serial.available())
{
mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while(mySerial.available())
{
Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
}
}
这是串行监视器的输出
22:31:19.430 -> Initializing...
但是,当我运行代码时,我收到了一条短信到我的手机,但是是I can't see any AT commands in the serial monitor. It only outputs "Initializing..." 。所有连接和波特率都可以,检查一千次。已将2A,4.4v电源连接到GSM核心板上,并缩短了电线,并且sho没有不良的焊接接头。 GSM模块红色指示灯每3秒闪烁一次。再一次,我将短信发送到手机。因此,这意味着问题出在Arduino串行监视器或代码上,而不是硬件上。我需要查看AT命令,因为我需要通过串行监视器放置更多命令,我尝试键入并单击发送,但是它什么也没显示。您可以提供的任何帮助将不胜感激。
您的逻辑在updateSerial()
功能中被颠倒。
实际上,您是通过AT
功能通过mySerial
发送setup
命令,然后您需要等待该对象mySerial
中出现答案。
因此,您应该执行while (!mySerial.available()) ;
以能够从中读取某些内容。该循环结束后,您可以从mySerial
中读取。
但是,您要将其转发到串行监视器,因此,您还需要检查Serial
是否可用于写入,这就是为什么您也要等待它,从而导致while (!mySerial.available() || !Serial.available()) ;
的原因。
一旦确定两个序列号都可用,就可以从一个序列中读取并将刚刚读取的内容写入另一个序列中:Serial.Write(mySerial.read())
。
而且,由于不需要使用mySerial.write(Serial.read())
,因为Serial
仅用于转发您从SIM800L接收到的内容,因此,您可以简单地删除该部分。
因此,对功能的更正将导致以下结果:
void updateSerial()
{
delay(500);
while (!mySerial.available() || !Serial.available())
;
Serial.write(mySerial.read());
}
因此,您将从SIM800L接收到的所有信息都转发到了串行监视器。