gpt4 book ai didi

c - 如何在嵌入式 C 中读取来自 GSM 调制解调器的消息?

转载 作者:太空宇宙 更新时间:2023-11-04 04:24:23 26 4
gpt4 key购买 nike

我目前正在“物联网”领域开展我的小型项目。我选择使用 GSM 模块设计一个无线公告板。

我将项目分为两个模块。首先是完美完成的Arduino-LED板接口(interface)。

二、GSM-Arduino接口(interface)。基本上,一条消息/SMS 将从手机发送到 GSM 模块,然后我们必须使用 Arduino 从 GSM 模块读取该消息。我在这里遇到问题。消息正在发送到 GSM 调制解调器,但我无法读取它。我尝试编写不同的代码,但它不起作用。消息未显示。

这是我试过的代码片段。

`#include SoftwareSerial.h

SoftwareSerial SIM900A(2,3);// RX | TX

// Connect the SIM900A TX to Arduino pin 2 RX

// Connect the SIM900A RX to Arduino pin 3 TX.


void setup()
{

SIM900A.begin(9600); // Setting the baud rate of GSM Module

Serial.begin(9600); // Setting the baud rate of Serial Monitor(Arduino)

Serial.println ("SIM900A Ready");

delay(100);

Serial.println (" Press s to send and r to recieve ");

}

void loop()
{

if (Serial.available()>0)

switch(Serial.read())

{

case 's': SendMessage();
break;

case 'r': RecieveMessage();
break;

}

if (SIM900A.available()>0)

Serial.write(SIM900A.read());

}


void SendMessage()
{

SIM900A.println("AT+CMGF=1"); //Sets the GSM Module in Text Mode

delay(1000); // Delay of 1000 milli seconds or 1 second

Serial.println ("Set SMS Number");

SIM900A.println("AT+CMGS=\"+91xxxxxxxxxx\"\r"); //Replace with your mobileno.

delay(1000);

Serial.println ("Set SMS Content");

SIM900A.println("Hello, I am SIM900A GSM Module");// The SMS text you want to send

delay(100);

Serial.println ("Finish");

SIM900A.println((char)26);// ASCII code of CTRL+Z

delay(1000);

Serial.println (" ->SMS Sent");
}


void RecieveMessage()
{

Serial.println ("SIM900A Receiving SMS");

delay (1000);

SIM900A.println("AT+CNMI=2,2,0,0,0"); // AT Command to receive a live SMS

delay(1000);

Serial.write (" ->Unread SMS Recieved");

}`

最佳答案

您可能必须使用以下命令将首选短信存储设置为 SIM 卡:

SIM900A.print("AT+CPMS=\"SM\"\r");

此外,将此命令移至 setup():

SIM900A.print("AT+CMGF=1\r");

最后,请注意我是如何使用 SIM900A.print() 而不是 SIM900A.println() 并在每个命令后发送“\r”或 0x0d 的。 println() 发送“\n\r”,这会导致某些调制解调器出现问题。

关于c - 如何在嵌入式 C 中读取来自 GSM 调制解调器的消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43176551/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com