mbed 从电脑接收字符串
mbed receive String from pc
我正在学习 mbed,并正在做一些例子。我的代码是从我的电脑发送一个随机字符串 "ccc" 到微控制器 LPCXpresso。
enter code here
#include "mbed.h"
DigitalOut myled(LED1);
Serial pc(USBTX, USBRX);
int main() {
char c;
char buffer[128];
myled = 1;
pc.gets(buffer, 4);
pc.printf("I got '%s'\n", buffer);
if(buffer == 'ccc'){
myled = 0;
}
}
我输入 'ccc',printf 执行 return 消息:我得到 'ccc'。但是,在 if 语句中,如果我使用 (buffer == 'ccc'),我会看到错误消息:Error: Operand types are incompatible ("char *" and "int") in "main.cpp".如果我使用 (buffer == "ccc"),我的 LED 灯不会按预期点亮,因为我确定缓冲区是 ccc。
我怎样才能让它发挥作用?
您不能比较 C/C++ 中的字符串。使用 strcmp。例如:
if (strcmp(buffer, "ccc") == 0) {
我正在学习 mbed,并正在做一些例子。我的代码是从我的电脑发送一个随机字符串 "ccc" 到微控制器 LPCXpresso。
enter code here
#include "mbed.h"
DigitalOut myled(LED1);
Serial pc(USBTX, USBRX);
int main() {
char c;
char buffer[128];
myled = 1;
pc.gets(buffer, 4);
pc.printf("I got '%s'\n", buffer);
if(buffer == 'ccc'){
myled = 0;
}
}
我输入 'ccc',printf 执行 return 消息:我得到 'ccc'。但是,在 if 语句中,如果我使用 (buffer == 'ccc'),我会看到错误消息:Error: Operand types are incompatible ("char *" and "int") in "main.cpp".如果我使用 (buffer == "ccc"),我的 LED 灯不会按预期点亮,因为我确定缓冲区是 ccc。
我怎样才能让它发挥作用?
您不能比较 C/C++ 中的字符串。使用 strcmp。例如:
if (strcmp(buffer, "ccc") == 0) {