Sms 使用SIM 900和st nucleo发送短信息

Sms 使用SIM 900和st nucleo发送短信息,sms,at-command,stm32,Sms,At Command,Stm32,我使用SimCOM的Sim900,如果我使用sand AT命令或其他命令都能很好地工作,但是我在编写此代码时遇到了一个sand SMS问题 #include "mbed.h" #include <string> Serial pc(SERIAL_TX, SERIAL_RX); // pc comunication Serial SIM900(PA_9, PA_10); //tx, rx SIM 900 string result; void power(){ sim_p

我使用SimCOM的Sim900,如果我使用sand AT命令或其他命令都能很好地工作,但是我在编写此代码时遇到了一个sand SMS问题

    #include "mbed.h"
#include <string>
Serial pc(SERIAL_TX, SERIAL_RX); // pc comunication
Serial SIM900(PA_9, PA_10);   //tx, rx SIM 900
string result;
void power(){
 sim_power.write(1); // accension gsm
    wait(1);
    sim_power.write(0);
    wait(13);
    SIM900.printf("ATE0\r\n");  // DISABLE ECHO 
    wait(1);}
 void clearString()
 {  result.clear();   }
 void callback_rx() { 
while (SIM900.readable()) {
  x = SIM900.getc();
  result += x;
pc.putc(x);      
   }
   }
 void sendSMS()
 {
 clearString();
 SIM900.printf("AT+CMGF=1\r"); //at command for send sms
 wait_ms(100);
 clearString();
 wait_ms(100);   
 SIM900.printf("AT+CMGS=");
 SIM900.putc('"');
 SIM900.printf("+32292*******"); 
 SIM900.putc('"');
 SIM900.printf("\r");
 SIM900.printf("Example Message SMS");
 SIM900.putc(0x1A);
 wait_ms(20000);
 } 
 int main() {    
 power();
 pc.printf("\r\n GSM 900 TEST\n"); 
 SIM900.attach(&callback_rx);  
 SIM900.baud(9600); // 
 wait_ms(100);         
  while(1) {
 wait_ms(10);
 sendSMS();  // SEND SMS     
 wait_ms(100);                 
 } 
 }
我看到系统发送消息后两个循环,我不知道哪里有错误,你能帮我吗


致以最良好的祝愿。A.

我想你的时间安排有问题。您必须等待,直到收到SIMCOM在commonds之后的回复,如“OK”。得到答案后,您可以发送其他命令

试试这个

void sendSMS(string number, string message){
  SIM900.printf("AT+CMGF=1\r\n");
  wait_ms(100);
  SIM900.printf("AT+CMGS=\"%s\"\r\n",number.c_str());
  wait_ms(100);
  SIM900.printf("%s ",message.c_str());  
  SIM900.putc(0x1A);
}
并通过

发送短信(“32292*******”,“示例短信”)


+1.对于AT命令的任何处理,绝对需要从调制解调器读取并解析所有响应。当您读取响应时,您必须查看它是否确实是来自命令的响应,而不是未经请求的结果代码。
void sendSMS(string number, string message){
  SIM900.printf("AT+CMGF=1\r\n");
  wait_ms(100);
  SIM900.printf("AT+CMGS=\"%s\"\r\n",number.c_str());
  wait_ms(100);
  SIM900.printf("%s ",message.c_str());  
  SIM900.putc(0x1A);
}