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. Adding individual bytes to message for serial port
Qt 6.11 is out! See what's new in the release blog

Adding individual bytes to message for serial port

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 2 Posters 318 Views 1 Watching
  • 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.
  • A Offline
    A Offline
    agmar
    wrote on last edited by agmar
    #1

    Hi,
    i managed to simplify the code and i understand most of it, however, i am not sure how to combine the individual bytes into an array that could be sent

    
    void MainWindow::openSeriall(){
        m_serial->setPortName(portName);
        m_serial->setBaudRate(QSerialPort::Baud19200);
        m_serial->setDataBits(QSerialPort::Data8);
        m_serial->setParity(QSerialPort::NoParity);
        m_serial->setFlowControl(QSerialPort::NoFlowControl);
        m_serial->setStopBits(QSerialPort::OneStop);
    
    }
    
    void MainWindow::writeSerial()
    {
        quint8 message = 0xFF;
        message += quint8(mAddress)   ;
      //  message += QChar(0x55)       ;
      //  message += QChar(byte4)      ;
     //   message += QChar(mPSpeed)    ;
    //    message += QChar(mTSpeed)    ;
    //    message += QChar(mCheckSum)  ;
        qDebug() << message          ;
        if (m_serial->open(QIODevice::ReadWrite)) {
            QByteArray thisSend = message.toUtf8();
        m_serial->write(thisSend);
        m_serial->waitForBytesWritten(-1);
        m_serial->close();
    
    }
    }
    
    

    i can send the values as QChars, but that adds an additional byte to the output,as i understand and that is not my goal
    here is the issue:

    error: request for member ‘toUtf8’ in ‘message’, which is of non-class type ‘quint8’ {aka ‘unsigned char’}
    119 | quint8 thisSend = message.toUtf8();
    | ^~~~~~

    jsulmJ 1 Reply Last reply
    0
    • A agmar

      Hi,
      i managed to simplify the code and i understand most of it, however, i am not sure how to combine the individual bytes into an array that could be sent

      
      void MainWindow::openSeriall(){
          m_serial->setPortName(portName);
          m_serial->setBaudRate(QSerialPort::Baud19200);
          m_serial->setDataBits(QSerialPort::Data8);
          m_serial->setParity(QSerialPort::NoParity);
          m_serial->setFlowControl(QSerialPort::NoFlowControl);
          m_serial->setStopBits(QSerialPort::OneStop);
      
      }
      
      void MainWindow::writeSerial()
      {
          quint8 message = 0xFF;
          message += quint8(mAddress)   ;
        //  message += QChar(0x55)       ;
        //  message += QChar(byte4)      ;
       //   message += QChar(mPSpeed)    ;
      //    message += QChar(mTSpeed)    ;
      //    message += QChar(mCheckSum)  ;
          qDebug() << message          ;
          if (m_serial->open(QIODevice::ReadWrite)) {
              QByteArray thisSend = message.toUtf8();
          m_serial->write(thisSend);
          m_serial->waitForBytesWritten(-1);
          m_serial->close();
      
      }
      }
      
      

      i can send the values as QChars, but that adds an additional byte to the output,as i understand and that is not my goal
      here is the issue:

      error: request for member ‘toUtf8’ in ‘message’, which is of non-class type ‘quint8’ {aka ‘unsigned char’}
      119 | quint8 thisSend = message.toUtf8();
      | ^~~~~~

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @agmar said in Adding individual bytes to message for serial port:

      quint8 message = 0xFF;

      If you need an array then use one:

      QByteArray message;
      message.append(0xFF);
      ...
      m_serial->write(message);
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • A agmar has marked this topic as solved on

      • Login

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