在 linux 终端上使用 system() 将用户输入的命令发送到 c++ 中的 arduino
Sending user inputted commands to an arduino in c++ using system() on a linux terminal
使用 c++ 程序我可以成功地向 arduino 发送命令。代码使用命令:
system("$echo [command] > dev/ttyACM0");
目前我必须手动将命令输入到此 space,我想知道是否可以让用户输入命令,然后将其添加到 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()
命令。只需打开设备,直接发送字符串即可。
使用 c++ 程序我可以成功地向 arduino 发送命令。代码使用命令:
system("$echo [command] > dev/ttyACM0");
目前我必须手动将命令输入到此 space,我想知道是否可以让用户输入命令,然后将其添加到 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()
命令。只需打开设备,直接发送字符串即可。