我正在尝试编写一个非常简单的C++应用程序来与Arduino进行通信.我想向Arduino发送一个它立即发回的角色.我从教程中获取的Arduino代码如下所示:
void setup()
{
Serial.begin(9600);
}
void loop()
{
//Have the Arduino wait to receive input
while (Serial.available()==0);
//Read the input
char val = Serial.read();
//Echo
Serial.println(val);
}
Run Code Online (Sandbox Code Playgroud)
我可以使用GNU屏幕轻松地与Arduino进行通信,因此我知道基本通信一切正常:
$ screen /dev/tty.usbmodem641 9600
我看到的(破碎的)C++代码如下所示:
#include <fstream>
#include <iostream>
int main()
{
std::cout << "Opening fstream" << std::endl;
std::fstream file("/dev/tty.usbmodem641");
std::cout << "Sending integer" << std::endl;
file << 5 << std::endl; // endl does flush, which may be important
std::cout << "Data Sent" << std::endl;
std::cout << "Awaiting response" << std::endl; …Run Code Online (Sandbox Code Playgroud)