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. Sending single byte over QSerialPort

Sending single byte over QSerialPort

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 3.9k 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.
  • V Offline
    V Offline
    valerio.j
    wrote on last edited by
    #1

    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
    0
    • I Offline
      I Offline
      ion_knight
      wrote on last edited by
      #2

      -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
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #3

        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
        0
        • A Offline
          A Offline
          andreyc
          wrote on last edited by
          #4

          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
          0
          • V Offline
            V Offline
            valerio.j
            wrote on last edited by
            #5

            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
            0
            • I Offline
              I Offline
              ion_knight
              wrote on last edited by
              #6

              Yea silly of me didn't see that.

              1 Reply Last reply
              0
              • A Offline
                A Offline
                andreyc
                wrote on last edited by
                #7

                [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
                0
                • V Offline
                  V Offline
                  valerio.j
                  wrote on last edited by
                  #8

                  Great, thanks @andreyc that did it.

                  1 Reply Last reply
                  0

                  • Login

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