Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Sending single byte over QSerialPort

    General and Desktop
    4
    8
    3608
    Loading More Posts
    • 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.
    • V
      valerio.j last edited by

      Hello,

      I have the below code and I'm trying to send single bytes of data using QSerialPort, but I get the error
      error: invalid user-defined conversion from 'QByteRef' to 'const char*' [-fpermissive]
      Any help will be appreciated.

      @void MainWindow::serialTX()
      {
      //char mydata[] = {0x52, 0x53, '\0'};
      QByteArray ba;
      ba.resize(5);
      ba[0] = 0x41;
      ba[1] = 0x42;
      ba[2] = 0x43;
      ba[3] = 0x44;
      ba[4] = 0x45;
      //QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));

      serial->write(ba[1]);
      
      //qDebug() << data;
      //serial->close();
      

      }@

      1 Reply Last reply Reply Quote 0
      • I
        ion_knight last edited by

        -It sounds like write is expecting a const char *, have you tried casting it? Also it would be good to see your serial class as QSerialPort doesn't contain write-

        1 Reply Last reply Reply Quote 0
        • SGaist
          SGaist Lifetime Qt Champion last edited by

          Hi,

          Maybe a silly question but why do you create a QByteArray of size 5 if you only want to send one byte ?

          @ ion_knight, QSerialPort is a QIODevice so it has write functions.

          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 Reply Quote 0
          • A
            andreyc last edited by

            QSerialPort inherits write() functions from QIODevice and that write functions expect either "zero terminated const char*":http://qt-project.org/doc/qt-5/qiodevice.html#write-2 or "const char* and size":http://qt-project.org/doc/qt-5/qiodevice.html#write

            Try this construction
            @
            serial->write(ba.data[1], 1);
            @

            1 Reply Last reply Reply Quote 0
            • V
              valerio.j last edited by

              I get another error instead

              error: invalid types '<unresolved overloaded function type>[int]' for array subscript

              [quote author="andreyc" date="1418160521"]QSerialPort inherits write() functions from QIODevice and that write functions expect either "zero terminated const char*":http://qt-project.org/doc/qt-5/qiodevice.html#write-2 or "const char* and size":http://qt-project.org/doc/qt-5/qiodevice.html#write

              Try this construction
              @
              serial->write(ba.data[1], 1);
              @[/quote]

                                      ^
              
              1 Reply Last reply Reply Quote 0
              • I
                ion_knight last edited by

                Yea silly of me didn't see that.

                1 Reply Last reply Reply Quote 0
                • A
                  andreyc last edited by

                  [quote author="valerio.j" date="1418161384"]I get another error instead

                  error: invalid types '<unresolved overloaded function type>[int]' for array subscript
                  [/quote]

                  Right. It should be either &(ba.data()[1]) or ba.data() + 1
                  @
                  serial->write(&(ba.data()[1]), 1);
                  @

                  1 Reply Last reply Reply Quote 0
                  • V
                    valerio.j last edited by

                    Great, thanks @andreyc that did it.

                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post