I've written two pieces of code, to create a serial communication between Arduino and a Raspberry Pi using C++. The codes are:
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 baud
}
void loop() {
Serial.println("Hello from arduino");
delay(500);
}
And the C++ code in Raspberry is :
#include <iostream>
#include <wiringPi.h>
#include <wiringSerial.h>
using namespace std ;
int serialDeviceId=0;
int main(void)
{
int pin=7;
serialDeviceId= serialOpen("/dev/ttyACM1",9600);
if(serialDeviceId==-1)
{
cout<<"Unable to open serial device"<<endl;
return 1;
}
if(wiringPiSetup()==-1)
{
return 0 ;
}
pinMode(pin,OUTPUT); // designing pin as an output
while(1)
{
digitalWrite(pin,0);
delay(500);
digitalWrite(pin,1);
delay(500);
}
return 0;
}
So now I would like to read the data from the serial port using always that wiringpi and I've found that I can use SerialGetchar, but I don't know exactly how to use it. I just need to need this part in my code so that I can receive that "Hello from from arduino" written in the serial, from my Arduino code.