我用 st nucleo 和 Shiled GSM (SIM900) 编写了这个简单的程序。我想将答案保存在字符串变量中。 我将数据保存在变量中。一切正常,但是当我使用 printf 命令时, 我有这个错误:
cannot pass object of non-pod type 'string' (aka 'basic_string<char,char_traits<char>,allocator<char>') through variadic method; call will abort at runtime (Wnon-pod-varargs)
#include "mbed.h"
#include <string>
Serial pc(SERIAL_TX, SERIAL_RX); // PC comunication
Serial SIM900(PA_9, PA_10); // serial comunication
DigitalOut myled(LED1);
DigitalOut sim_power(D9); //power gsm900
string result;
char x;
int i;
void callback_rx() {
while (SIM900.readable()) {
x = SIM900.getc();
result += x;
pc.putc(x); }
pc.printf("%s",result); //her i have error
}
void controlAT(){
result = "";
SIM900.printf("AT\r");
wait_ms(1000); }
int main()
{
power();
pc.printf("\r\n GSM 900 TEST\n");
SIM900.attach(&callback_rx); //call interrupt
SIM900.baud(9600);
while(1) {
controlAT();
wait_ms(300);
}
}
不知道哪里出了问题;你能帮帮我吗?
最佳答案
使用:
pc.printf("%s", result.c_str());
s
转换规范需要一个 C 字符串参数(即,一个以 null 结尾的 char
数组)。
关于c++ - printf 的奇怪情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26237517/