[英]SIM800L string concatenation
I have this code from a website that I'm using as a guide to send SMS message from a SIM800L connected to my Arduino Mega.我从一个网站上获得了此代码,我将其用作从连接到我的 Arduino Mega 的 SIM800L 发送 SMS 消息的指南。
#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
}
I added some bits of code in the middle so that it will receive a string input from a user in my Python GUI via serial connection.我在中间添加了一些代码,以便它可以通过串行连接在我的 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");
}
I am trying to concatenate the data from my serial.readString()
to the end of the text
.我正在尝试将serial.readString()
的数据连接到text
的末尾。 Conventional methods like the +
and %s
don't work.像+
和%s
这样的传统方法不起作用。
In Arduino IDE I'm getting this error:在 Arduino IDE 中,我收到此错误:
error: cannot convert ‘StringSumHelper’ to ‘char*’ in assignment
If I'm correct char*
is a pointer that points to an address.如果我是正确的char*
是一个指向地址的指针。 Is there anyway to add the string from the serial monitor to the text?无论如何将串行监视器中的字符串添加到文本中?
You have to convert the Arduino String
object to a standard C string.您必须将 Arduino String
对象转换为标准 C 字符串。 You can do this by using the c_str() method of the String
class.您可以通过使用String
类的c_str()方法来做到这一点。 It will return a char*
pointer.它将返回一个char*
指针。
Now you can concatenate the two string with the strncat function from C library, string.h
and also using strncpy as well.现在,您可以使用 C 库string.h
的strncat函数连接这两个字符串,也可以使用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);
Note it will simply chop off the remaining data if there is not enough space in the buffer.请注意,如果缓冲区中没有足够的空间,它只会截断剩余的数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.