使用 C 程式,我可以成功地向 arduino 發送命令。代碼使用命令:
system("$echo [command] > dev/ttyACM0");
目前我必須手動將命令輸入這個空間,我想知道用戶是否可以輸入命令,然后將其添加到 system() 中的字串中?
uj5u.com熱心網友回復:
這是我認為您想要的近似值:
#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
}
請注意,這里根本不需要 unsafesystem()命令。只需打開設備并直接發送字串即可。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/387051.html
上一篇:Unix連接命令
