Я пытаюсь написать очень простое приложение на С++ для общения с 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);
}
Я могу общаться с Arduino легко с помощью экрана GNU, поэтому я знаю, что все работает нормально с базовым сообщением:
$screen/dev/tty.usbmodem641 9600
(сломанный) код С++, который у меня выглядит следующим образом:
#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;
std::string response;
file >> response;
std::cout << "Response: " << response << std::endl;
return 0;
}
Он компилируется отлично, но при его запуске на Arduino мигают некоторые огни, и терминал просто зависает:
Открытие fstream
Где я иду не так?