-1

我正在尝试将收到的短信保存到字符串中,但它不起作用。这是我的错误信息:

recive_0_1:50:28: 错误:从 'int' 到 'char ' [-fpermissive] 的无效转换退出状态 1 从 'int' 到 'char ' [-fpermissive] 的无效转换此报告将包含“显示详细输出”的更多信息在文件 -> 首选项中启用“编译期间”选项。**

#include <SoftwareSerial.h>
#include <string.h>

char message[160];  // max size of an SMS
char* text;
String data;


//Create software serial object to communicate with SIM800L
SoftwareSerial mySerial(4, 5); //SIM800L Tx & Rx is connected to Arduino #3 & #2
void setup()
{
  //Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
  Serial.begin(9600);
  
  //Begin serial communication with Arduino and SIM800L
  mySerial.begin(9600);

  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+CNMI=1,2,0,0,0"); // Decides how newly arrived SMS messages should be handled
  updateSerial();
}

void loop()
{
  updateSerial();
  
}

void updateSerial()
{
 // char c;
  delay(500);
  while (Serial.available()) 
  {
    mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
    //char data=Serial.read();
    //Serial.println(data,DEC);
  }
  while(mySerial.available()) 
  {
    Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
        text=mySerial.read();
      Serial.print(text);
    //}
  }

}
4

1 回答 1

0

看起来您在尝试将整数转换为字符时遇到了问题。我使用以下代码从我的 SIM 卡读取到我的 SIM900 设备:

第一:我的软件序列被定义并且我的安装脚本运行:

SoftwareSerial gprsSerial(7, 8);

void setup()
{
  pinMode(13, OUTPUT);
  gprsSerial.begin(19200);
  Serial.begin(19200);
  delay(200);
  // set up your AT COMMANDS HERE - I HAVE NOT INCLUDED THESE
}

然后我按如下方式运行我的循环函数,调用其后指定的附加函数:

void loop() {
readSMS();
delay(2000);

}

void toSerial(){
  delay(200);
  if(gprsSerial.available()>0){
    textMessage = gprsSerial.readString();
    delay(100);
    Serial.print(textMessage);    
  }
}

void readSMS() {
  textMessage = ""; 
  gprsSerial.print("AT+CSQ\r");
  toSerial();
  gprsSerial.print("AT+CMGF=1\r");
  toSerial();
  gprsSerial.println("AT+CNMI=2,2,0,0,0\r");  
  delay(2000);
  toSerial();
}

有了上述内容,您应该不会遇到任何数据类型转换的麻烦。

于 2020-06-30T13:24:25.497 回答