在linux终端上使用system()将用户输入的命令发送到c++中的arduino



使用c++程序,我可以成功地将命令发送到arduino。代码使用命令:

system("$echo[command]>dev/tyACM0"(;

目前,我必须手动将命令输入到这个空间,我想知道用户是否可以输入命令,然后将其添加到system((中的字符串中?

这是我认为您想要的近似值:

#include <fstream>
#include <iostream>
#include <string>
int main() {
std::string command;
if(std::getline(std::cin, command)) {  // read user input
std::ofstream ard("/dev/ttyACM0"); // open the device
if(ard) {
ard << command << 'n';        // send the command
}
} // here `ard` goes out of scope and is closed automatically
}

请注意,此处根本不需要不安全的system()命令。只需打开设备并直接发送字符串。

最新更新