3

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
MadHer
  • 91
  • 3
  • 11
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Mar 30 '18 at 17:39
  • Note that the purpose of Stack Overflow is not to ask for a project to be finished for you because you got frustrated with it. I sympathise if you have lost patience, since that happens at all skill levels, and projects are hard to commit to. However, Stack Overflow is for specific and answerable questions, and the filter we use is "is this question interesting and useful for future readers". Questions that are "finish my project plz" do not really fit into that mould. – halfer Mar 30 '18 at 17:42
  • thanks for ur answer , i didnt ask to finish my work , i asked specially about how to use this function which is serialGetchar in c++, and i think it comes to the kind of questions asked in stackoverflow , i'm working on my own work , and i asked for a help , as i founded before many answers in internet , i wanted to have some specific answers to my own case . i just explaned it to bee clear , and that others understand what i want for real , as others do and did before. Thanks – MadHer Mar 30 '18 at 18:32
  • Your original question started with _i would love that somebody help me to complete my code_, which would likely give the reader the impression that you wanted someone to pair with you to finish it off. I accept that perhaps it was just non-optimal phrasing, so I have undownvoted. It's worth being aware of this ethic on Stack Overflow all the same, though. – halfer Mar 30 '18 at 18:38
  • (Aside: please use upper case letters in your writing here as much as you can, so as not to create editing work. Stack Overflow is a technical resource, and technical writing is expected. That means using words like "your" rather than the chat equivalent - real words are not too much trouble to type here. Equally, when referring to yourself, the word is "I", in upper case, in all circumstances). – halfer Mar 30 '18 at 18:39
  • OKAY THANK YOU SIR I WILL TAKE THIS IN CONSIDERATION NEXT TIME, THANK YOU AGAIN. – MadHer Mar 30 '18 at 18:57
  • Hi Mr @Mark Setchell , actually it did work , but with some updates to my personnal code ;because my code was about either reading the data from the serial port and also blinking the led , so what I've done so far , is reading the data from the serial port , but only turning off the LED.Thank you so much for your help , and sorry for being late. – MadHer Apr 20 '18 at 21:32

1 Answers1

3

This code should work for you on the Raspberry Pi. It is extremely simple! Make sure you get the correct device special file by running:

dmesg

and looking for line like this:

[610106.464745] usb 1-1.4: new full-speed USB device number 4 using dwc_otg
[610106.608642] usb 1-1.4: New USB device found, idVendor=2341, idProduct=0043
[610106.608655] usb 1-1.4: New USB device strings: Mfr=1, Product=2, SerialNumber=220
[610106.608663] usb 1-1.4: Manufacturer: Arduino (www.arduino.cc)
[610106.608671] usb 1-1.4: SerialNumber: 55731323536351E002E1
[610106.686193] cdc_acm 1-1.4:1.0: ttyACM0: USB ACM device

#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <wiringSerial.h>

int main ()
{
  int fd ;

  if((fd=serialOpen("/dev/ttyACM0",9600))<0){
    fprintf(stderr,"Unable to open serial device: %s\n",strerror(errno));
    return 1;
  }

  for (;;){
    putchar(serialGetchar(fd));
    fflush(stdout);
  }
}

I assume you have a USB cable connecting one of the RaspberryPi USB ports to the USB port on the Arduino.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • thanks mr mark , i'm trying to use it right now in my c++ program thanks , will back to you after , thanks for ur answer ! – MadHer Mar 30 '18 at 18:34
  • I am trying to send some integers from my Arduino to my Raspberry Pi and I want to add them in my C++ code on my Qt Quick application, can you please help me how to extract the integers ? when using this code and trying to output it which `qDebug()<<` it is outputting several integers that are not supposed to be there – mina Jun 28 '21 at 15:22