Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. why i cant write to serial ? | Arduino Uno , QT 5.9.9 , C++ Question

why i cant write to serial ? | Arduino Uno , QT 5.9.9 , C++ Question

Scheduled Pinned Locked Moved Unsolved General and Desktop
serialarduinoqt5.5.9
4 Posts 3 Posters 558 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.
  • L Offline
    L Offline
    Lime1
    wrote on 28 Nov 2023, 13:34 last edited by Lime1
    #1

    My code can read from arduino uno fine, but it cannot write to it. I thought about it because I was trying to write while reading data in the same time , so i commented the part that read data, but it still doesn't work. Here is the call for the code

    uno.write_to_arduino("1"); //! testing
    

    here is the arduino init (it work fine beside the writing part)

    #include <QDebug>
    
    #include "arduino.h"
    
    Arduino::Arduino()
    {
        data = "";
        arduino_port_name = "";
        arduino_is_available = false;
        serial = new QSerialPort;
    }
    
    QString Arduino::getarduino_port_name()
    {
        return arduino_port_name;
    }
    
    QSerialPort *Arduino::getserial()
    {
        return serial;
    }
    
    int Arduino::connect_arduino()
    { // recherche du port sur lequel la carte arduino identifée par  arduino_uno_vendor_id
        // est connectée
        foreach (const QSerialPortInfo &serial_port_info, QSerialPortInfo::availablePorts())
        {
            if (serial_port_info.hasVendorIdentifier() && serial_port_info.hasProductIdentifier())
            {
                if (serial_port_info.vendorIdentifier() == arduino_uno_vendor_id && serial_port_info.productIdentifier() == arduino_uno_producy_id)
                {
                    arduino_is_available = true;
                    arduino_port_name = serial_port_info.portName();
                }
            }
        }
        qDebug() << "arduino_port_name is :" << arduino_port_name;
        if (arduino_is_available)
        { // configuration de la communication ( débit...)
            serial->setPortName(arduino_port_name);
            if (serial->open(QSerialPort::ReadWrite))
            {
                serial->setBaudRate(QSerialPort::Baud9600); // débit : 9600 bits/s
                serial->setDataBits(QSerialPort::Data8);    // Longueur des données : 8 bits,
                serial->setParity(QSerialPort::NoParity);   // 1 bit de parité optionnel
                serial->setStopBits(QSerialPort::OneStop);  // Nombre de bits de stop : 1
                serial->setFlowControl(QSerialPort::NoFlowControl);
                return 0;
            }
            return 1;
        }
        return -1;
    }
    
    int Arduino::close_arduino()
    {
        if (serial->isOpen())
        {
            serial->close();
            return 0;
        }
        return 1;
    }
    
    QByteArray Arduino::read_from_arduino()
    {
        if (serial->isReadable())
        {
            data = serial->readAll(); // récupérer les données reçues
    
            return data;
        }
    }
    
    int Arduino::write_to_arduino(QByteArray d)
    {
        if (serial->isWritable())
        {
            serial->write(d); // envoyer des donnés vers Arduino
            qDebug() << "Data sent to Arduino: " << d;
        }
        else
        {
            qDebug() << "Couldn't write to serial!";
        }
    }
    

    i get "Couldn't write to serial!" is there at least a way to debug the issue more ?

    L 1 Reply Last reply 28 Nov 2023, 13:54
    0
    • L Lime1
      28 Nov 2023, 13:34

      My code can read from arduino uno fine, but it cannot write to it. I thought about it because I was trying to write while reading data in the same time , so i commented the part that read data, but it still doesn't work. Here is the call for the code

      uno.write_to_arduino("1"); //! testing
      

      here is the arduino init (it work fine beside the writing part)

      #include <QDebug>
      
      #include "arduino.h"
      
      Arduino::Arduino()
      {
          data = "";
          arduino_port_name = "";
          arduino_is_available = false;
          serial = new QSerialPort;
      }
      
      QString Arduino::getarduino_port_name()
      {
          return arduino_port_name;
      }
      
      QSerialPort *Arduino::getserial()
      {
          return serial;
      }
      
      int Arduino::connect_arduino()
      { // recherche du port sur lequel la carte arduino identifée par  arduino_uno_vendor_id
          // est connectée
          foreach (const QSerialPortInfo &serial_port_info, QSerialPortInfo::availablePorts())
          {
              if (serial_port_info.hasVendorIdentifier() && serial_port_info.hasProductIdentifier())
              {
                  if (serial_port_info.vendorIdentifier() == arduino_uno_vendor_id && serial_port_info.productIdentifier() == arduino_uno_producy_id)
                  {
                      arduino_is_available = true;
                      arduino_port_name = serial_port_info.portName();
                  }
              }
          }
          qDebug() << "arduino_port_name is :" << arduino_port_name;
          if (arduino_is_available)
          { // configuration de la communication ( débit...)
              serial->setPortName(arduino_port_name);
              if (serial->open(QSerialPort::ReadWrite))
              {
                  serial->setBaudRate(QSerialPort::Baud9600); // débit : 9600 bits/s
                  serial->setDataBits(QSerialPort::Data8);    // Longueur des données : 8 bits,
                  serial->setParity(QSerialPort::NoParity);   // 1 bit de parité optionnel
                  serial->setStopBits(QSerialPort::OneStop);  // Nombre de bits de stop : 1
                  serial->setFlowControl(QSerialPort::NoFlowControl);
                  return 0;
              }
              return 1;
          }
          return -1;
      }
      
      int Arduino::close_arduino()
      {
          if (serial->isOpen())
          {
              serial->close();
              return 0;
          }
          return 1;
      }
      
      QByteArray Arduino::read_from_arduino()
      {
          if (serial->isReadable())
          {
              data = serial->readAll(); // récupérer les données reçues
      
              return data;
          }
      }
      
      int Arduino::write_to_arduino(QByteArray d)
      {
          if (serial->isWritable())
          {
              serial->write(d); // envoyer des donnés vers Arduino
              qDebug() << "Data sent to Arduino: " << d;
          }
          else
          {
              qDebug() << "Couldn't write to serial!";
          }
      }
      

      i get "Couldn't write to serial!" is there at least a way to debug the issue more ?

      L Offline
      L Offline
      Lime1
      wrote on 28 Nov 2023, 13:54 last edited by
      #2

      Also, switching the USB port did not work.
      and typing the command in the arduino IDE consol works fine

      P 1 Reply Last reply 28 Nov 2023, 14:35
      0
      • L Lime1
        28 Nov 2023, 13:54

        Also, switching the USB port did not work.
        and typing the command in the arduino IDE consol works fine

        P Offline
        P Offline
        Pl45m4
        wrote on 28 Nov 2023, 14:35 last edited by Pl45m4
        #3

        @Lime1 said in why i cant write to serial ? | Arduino Uno , QT 5.9.9 , C++ Question:

        Also, switching the USB port did not work.

        What should this do? :)

        Maybe you need some terminating \r\n (CR LF)
        Try to send it at the end of your data.

        What does QSerialPort::error() return?


        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

        ~E. W. Dijkstra

        S 1 Reply Last reply 28 Nov 2023, 23:07
        0
        • P Pl45m4
          28 Nov 2023, 14:35

          @Lime1 said in why i cant write to serial ? | Arduino Uno , QT 5.9.9 , C++ Question:

          Also, switching the USB port did not work.

          What should this do? :)

          Maybe you need some terminating \r\n (CR LF)
          Try to send it at the end of your data.

          What does QSerialPort::error() return?

          S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 28 Nov 2023, 23:07 last edited by
          #4

          Hi,

          In addition to @Pl45m4, you don't print any information if the opening of the serial port failed. Doing that will help track the issue.

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

          1 Reply Last reply
          0

          4/4

          28 Nov 2023, 23:07

          • Login

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