2017-04-03 34 views
1

我目前在“物联网”领域下研究我的迷你项目。我选择使用GSM模块设计无线通知板。如何从嵌入式C中的GSM调制解调器读取消息?

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

二,GSM-Arduino接口。基本上,信息/短信将从手机发送到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"); 

}` 
+0

我们不知道你的硬件是否工作。我们不能帮助硬件调试。你在范围上看到什么? – ThingyWotsit

+0

硬件工作正常!当我们在GSM模块中插入SIM卡并测试它是否从移动电话发送时接收到任何消息时,它将完全正常工作。如何阅读接收消息是我面临问题的地方。我无法阅读GSM的消息。 –

+0

'测试它是否接收到任何消息' - 如果您无法接收Arduino接口上的任何数据,如何测试它? – ThingyWotsit

回答

0

您可能必须使用命令来设置首选短信存储到SIM卡:

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

此外,移动这个命令设置():

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

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

+0

您可能还想验证调制解调器的波特率是9600还是别的。大多数调制解调器让你配置这个,所以它可能已经改变为其他值 –