#include <iostream>
#include <fstream>
#include <stdio.h>
using namespace std;
char j='a';
main()
{
fstream arduino;
arduino.open("/dev/ttyACM0",ios::in | ios::out);
//Opening device file
if(!arduino)
cout<<"error";
arduino<<2;
arduino.clear();
if(arduino >> j)
cout << "Value received: " << j << '\n';
else if(arduino.eof())
cerr << "Premature EOF\n";
else if(arduino.bad())
cerr << "Attempt to read from device failed.\n";
else
cerr << "Logical I/O error.\n";
arduino.close();
return 0;
}
arduino code: int p; void setup() { pinMode(13,OUTPUT); Serial.begin(9600); }
void loop()
{
if(Serial.available())
{
p=Serial.read();
if(p!=-1)
{
Serial.write(1);
digitalWrite(13,HIGH);
delay(5000);
}
}
else
{
digitalWrite(13,LOW);
delay(1000);
}
}
i have tried this code in C++ for serial communication with the arduino. i got an error "premature eof". what is the problem here??