Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Serial Communication between Arduino & pi
Forum Update on Monday, May 27th 2025

Serial Communication between Arduino & pi

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
4 Posts 3 Posters 3.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jerwinprabu
    wrote on 22 Apr 2017, 05:57 last edited by Jerwinprabu
    #1

    Hello all,

    I've been trying for more than a week to communicate from raspberry pi (QT C++) to Arduino (Arduino IDE c++) through a serial port but i keep failing.

    I did some searching on google, read the example... and still i didn't succeeded. Ok so the basic thing is that i need to communicate continuously the serial port sent command from Raspberry pi to Arduino. I tried to keep the code as simple as possible.

    Initially I'm sending "J" char from raspberry pi (QT C++) to Arduino (Arduino IDE c++) and waiting on that J, to make the LED blink on Arduino. But it doesn't work.. Even I didn't get any sample for interfacing & communicating & sending data raspberry pi (QT C++) to Arduino (Arduino IDE c++). I don't know what is the problem exactly. Kindly help me to solve the issue.

    In monitor, 9600 baudrate

    I have attached program what I have tried on both side.

    main.cpp

    #include <iostream>
    #include <QIODevice>
    
    #include <QtSerialPort/QSerialPort>
    #include <QtSerialPort/QSerialPortInfo>
    #include <QString>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        QApplication a(argc, argv);
        Dialog w;
        w.show();
    
        QSerialPort serialPort;
        serialPort.setBaudRate(QSerialPort::Baud9600);
    
        QSerialPortInfo info("ttyUSB0");
            qDebug() << "Name        : "     << info.portName();
            qDebug() << "Description : "     << info.description();
            qDebug() << "Busy:"              << info.isBusy();
    
            QSerialPort serial;
            serial.setPortName("ttyUSB0");
    
            serial.open(QIODevice::ReadWrite);
            serial.setBaudRate(QSerialPort::Baud9600);
            serial.setDataBits(QSerialPort::Data8);
            serial.setParity(QSerialPort::NoParity);
            serial.setStopBits(QSerialPort::OneStop);
            serial.setFlowControl(QSerialPort::NoFlowControl);
    
            serial.open(QSerialPort::ReadWrite);
            cout<<"Readable    :"<<serial.isReadable()<<endl;
            cout<<"Writable    :"<<serial.isWritable()<<endl<<endl;
    
            if (serial.isOpen() && serial.isWritable())
            {
                qDebug() << "Is open : " << serial.isOpen() << endl;
                qDebug() << "Is writable : " << serial.isWritable() << endl;
    
                qDebug() << "Ready..." << endl;
                serial.write("J");
    
                QByteArray ba("J\n");
    
                serial.write(ba);
                {
                    QByteArray ba("J");
                    serial.write(ba);
                    serial.flush();
                    qDebug() << "data has been send" << endl;
                    serial.close();
                }
                if (serial.bytesToWrite() > 0)
                {
                    serial.flush();
                    if(serial.waitForBytesWritten(1000))
                    {
                        qDebug() << "data has been send" << endl;
                    }
                }
                if(serial.flush())
                    {
                        qDebug() << "ok" << endl;
                    }
                    qDebug() <<"value sent "<< endl;
                    serial.close();
            }
            else
            {
                qDebug() << "An error occured" << endl;
            }
        return a.exec();
    }
    

    Arduino code:

    int led = 13, avlb = 0;
    
    void setup() 
    { 
    Serial.begin(9600); 
    pinMode(led, OUTPUT); 
    Serial.println("started");
    }
    
    void loop() 
    { 
    
     if (Serial.available() > 0)
    {
        Serial.println("available");
        Serial.println(Serial.available());  
        delay(2000); 
        digitalWrite(led, HIGH);  
        delay(5000);           
    
      if(Serial.read() == 'J')
     {
       Serial.println("read");
       Serial.println(Serial.read());
       delay(2000);
       digitalWrite(led, LOW);   
       delay(1000);  
     }  
    }
    
    else
     {
       Serial.println("not available");
       delay(1000);
     }
    }
    

    Output Displayed:

    Raspberry qt creator ide o/p:

    Name        :  "ttyUSB0"
    Description :  "FT232R USB UART"
    Busy: false
    
    Readable    :1
    Writable    :1
    
    Is open :  true 
    
    Is writable :  true 
    
    Ready... 
    
    data has been send 
    
    bool QSerialPort::flush(): device not open
    value sent
    

    Arduino Ide Output displayed:

    started
    
    not available
    not available
    not available
    not available
    not available
    not available
    not available
    not available
    not available
    
    C 1 Reply Last reply 22 Apr 2017, 19:48
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 22 Apr 2017, 17:12 last edited by
      #2

      Hi,

      I'd recommend starting by using the terminal example to test the connection and communication.

      Then, in your code, you are closing the port on several occasions, why ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      J 1 Reply Last reply 29 Apr 2017, 05:17
      1
      • J Jerwinprabu
        22 Apr 2017, 05:57

        Hello all,

        I've been trying for more than a week to communicate from raspberry pi (QT C++) to Arduino (Arduino IDE c++) through a serial port but i keep failing.

        I did some searching on google, read the example... and still i didn't succeeded. Ok so the basic thing is that i need to communicate continuously the serial port sent command from Raspberry pi to Arduino. I tried to keep the code as simple as possible.

        Initially I'm sending "J" char from raspberry pi (QT C++) to Arduino (Arduino IDE c++) and waiting on that J, to make the LED blink on Arduino. But it doesn't work.. Even I didn't get any sample for interfacing & communicating & sending data raspberry pi (QT C++) to Arduino (Arduino IDE c++). I don't know what is the problem exactly. Kindly help me to solve the issue.

        In monitor, 9600 baudrate

        I have attached program what I have tried on both side.

        main.cpp

        #include <iostream>
        #include <QIODevice>
        
        #include <QtSerialPort/QSerialPort>
        #include <QtSerialPort/QSerialPortInfo>
        #include <QString>
        
        using namespace std;
        
        int main(int argc, char *argv[])
        {
            QApplication a(argc, argv);
            Dialog w;
            w.show();
        
            QSerialPort serialPort;
            serialPort.setBaudRate(QSerialPort::Baud9600);
        
            QSerialPortInfo info("ttyUSB0");
                qDebug() << "Name        : "     << info.portName();
                qDebug() << "Description : "     << info.description();
                qDebug() << "Busy:"              << info.isBusy();
        
                QSerialPort serial;
                serial.setPortName("ttyUSB0");
        
                serial.open(QIODevice::ReadWrite);
                serial.setBaudRate(QSerialPort::Baud9600);
                serial.setDataBits(QSerialPort::Data8);
                serial.setParity(QSerialPort::NoParity);
                serial.setStopBits(QSerialPort::OneStop);
                serial.setFlowControl(QSerialPort::NoFlowControl);
        
                serial.open(QSerialPort::ReadWrite);
                cout<<"Readable    :"<<serial.isReadable()<<endl;
                cout<<"Writable    :"<<serial.isWritable()<<endl<<endl;
        
                if (serial.isOpen() && serial.isWritable())
                {
                    qDebug() << "Is open : " << serial.isOpen() << endl;
                    qDebug() << "Is writable : " << serial.isWritable() << endl;
        
                    qDebug() << "Ready..." << endl;
                    serial.write("J");
        
                    QByteArray ba("J\n");
        
                    serial.write(ba);
                    {
                        QByteArray ba("J");
                        serial.write(ba);
                        serial.flush();
                        qDebug() << "data has been send" << endl;
                        serial.close();
                    }
                    if (serial.bytesToWrite() > 0)
                    {
                        serial.flush();
                        if(serial.waitForBytesWritten(1000))
                        {
                            qDebug() << "data has been send" << endl;
                        }
                    }
                    if(serial.flush())
                        {
                            qDebug() << "ok" << endl;
                        }
                        qDebug() <<"value sent "<< endl;
                        serial.close();
                }
                else
                {
                    qDebug() << "An error occured" << endl;
                }
            return a.exec();
        }
        

        Arduino code:

        int led = 13, avlb = 0;
        
        void setup() 
        { 
        Serial.begin(9600); 
        pinMode(led, OUTPUT); 
        Serial.println("started");
        }
        
        void loop() 
        { 
        
         if (Serial.available() > 0)
        {
            Serial.println("available");
            Serial.println(Serial.available());  
            delay(2000); 
            digitalWrite(led, HIGH);  
            delay(5000);           
        
          if(Serial.read() == 'J')
         {
           Serial.println("read");
           Serial.println(Serial.read());
           delay(2000);
           digitalWrite(led, LOW);   
           delay(1000);  
         }  
        }
        
        else
         {
           Serial.println("not available");
           delay(1000);
         }
        }
        

        Output Displayed:

        Raspberry qt creator ide o/p:

        Name        :  "ttyUSB0"
        Description :  "FT232R USB UART"
        Busy: false
        
        Readable    :1
        Writable    :1
        
        Is open :  true 
        
        Is writable :  true 
        
        Ready... 
        
        data has been send 
        
        bool QSerialPort::flush(): device not open
        value sent
        

        Arduino Ide Output displayed:

        started
        
        not available
        not available
        not available
        not available
        not available
        not available
        not available
        not available
        not available
        
        C Offline
        C Offline
        Celal
        wrote on 22 Apr 2017, 19:48 last edited by
        #3

        @Jerwinprabu said in Communication between Raspberry pi (QT C++) and Arduino (Arduino IDE):

        int led = 13, avlb = 0;

        void setup()
        {
        Serial.begin(9600);
        pinMode(led, OUTPUT);
        Serial.println("started");
        }

        void loop()
        {

        if (Serial.available() > 0)
        {
        Serial.println("available");
        Serial.println(Serial.available());
        delay(2000);
        digitalWrite(led, HIGH);
        delay(5000);

        if(Serial.read() == 'J')
        {
        Serial.println("read");
        Serial.println(Serial.read());
        delay(2000);
        digitalWrite(led, LOW);
        delay(1000);
        }
        }

        else
        {
        Serial.println("not available");
        delay(1000);
        }
        }

        I do not know, what are you trying to do but your code does send data. You need the check ASCII code for letter 'J' which is 74.

        1 Reply Last reply
        0
        • S SGaist
          22 Apr 2017, 17:12

          Hi,

          I'd recommend starting by using the terminal example to test the connection and communication.

          Then, in your code, you are closing the port on several occasions, why ?

          J Offline
          J Offline
          Jerwinprabu
          wrote on 29 Apr 2017, 05:17 last edited by
          #4

          @SGaist Thanks for your reply. I will remove that. For checking I have created.

          1 Reply Last reply
          0

          1/4

          22 Apr 2017, 05:57

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved