我有一个网站的代码,我正在使用它作为从连接到我的Arduino Mega的SIM800L发送短信的指南。
#include <Sim800l.h>
#include <SoftwareSerial.h>
Sim800l Sim800l; //declare the library
char* text;
char* number;
bool error;
void setup(){
Sim800l.begin();
text="Testing Sms";
number="+542926556644";
error=Sim800l.sendSms(number,text);
// OR
//error=Sim800l.sendSms("+540111111111","the text go here");
}
void loop(){
//do nothing
}我在中间添加了一些代码,这样它就可以通过串行连接在Python GUI中接收用户输入的字符串。
#include <Sim800l.h>
#include <SoftwareSerial.h>
Sim800l Sim800l; //declare the library
char* text;
char* number;
bool error;
String data;
void setup(){
Serial.begin(9600);
}
void loop(){
if (Serial.available() > 0)
{
data = Serial.readString();
Serial.print(data);
sendmess();
}
}
void sendmess()
{
Sim800l.begin();
text="Power Outage occured in area of account #: ";
number="+639164384650";
error=Sim800l.sendSms(number,text);
// OR
//error=Sim800l.sendSms("+540111111111","the text go here");
}我正在尝试将serial.readString()中的数据连接到text的末尾。像+和%s这样的传统方法不起作用。
在Arduino IDE中,我得到这个错误:
error: cannot convert ‘StringSumHelper’ to ‘char*’ in assignment如果我没记错,char*是一个指向地址的指针。有没有办法将串行监视器中的字符串添加到文本中?
发布于 2017-02-10 22:57:34
您必须将Arduino String对象转换为标准的C字符串。您可以使用String类的c_str()方法来完成此操作。它将返回一个char*指针。
现在,您可以使用C库中的strncat函数、string.h以及strncpy来连接这两个字符串。
#include <string.h>
char message[160]; // max size of an SMS
char* text = "Power Outage occured in area of account #: ";
String data;
/*
* populate <String data> with data from serial port
*/
/* Copy <text> to message buffer */
strncpy(message, text, strlen(text));
/* Calculating remaining space in the message buffer */
int num = sizeof(message) - strlen(message) - 1;
/* Concatenate the data from serial port */
strncat(message, data.c_str(), num);
/* ... */
error=Sim800l.sendSms(number, message);注意,如果缓冲区中没有足够的空间,它将简单地砍掉剩余的数据。
https://stackoverflow.com/questions/42158230
复制相似问题