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. QSerialPort Qchar + QByteArray
Forum Update on Monday, May 27th 2025

QSerialPort Qchar + QByteArray

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.5k 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.
  • C Offline
    C Offline
    chiffaCff
    wrote on last edited by
    #1

    Hello every one! I develop some application to write/read data from COM-port. I get data from lineEdit and send it to port like:

    SerialPort->WriteToPort(ui->lineEdit_2->text().toLocal8Bit());
    

    all work fine, but i need to add some char to start and and line like:

    SerialPort->WriteToPort(QChar(253)+ui->lineEdit_2->text().toLocal8Bit()+QChar(254))
    

    but i get and error... Please help, how i can convert Qchar to QByteArray. Thanks!

    K 1 Reply Last reply
    0
    • C chiffaCff

      Hello every one! I develop some application to write/read data from COM-port. I get data from lineEdit and send it to port like:

      SerialPort->WriteToPort(ui->lineEdit_2->text().toLocal8Bit());
      

      all work fine, but i need to add some char to start and and line like:

      SerialPort->WriteToPort(QChar(253)+ui->lineEdit_2->text().toLocal8Bit()+QChar(254))
      

      but i get and error... Please help, how i can convert Qchar to QByteArray. Thanks!

      K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      @chiffaCff

      Hi and welcome to devnet

      This has nothing to do with QSerialPort. WriteToPort is not a routine of QSerialPort. It boils down to a pure QByteArray issue.

      If I am not mistaken this may work:

      SerialPort->WriteToPort(QByteArray(QChar(253)+ui->lineEdit_2->text().toLocal8Bit()+QChar(254)));
      

      My guess is that you do not have a routine fitting the outcome. You should see in the error protocol of your compile run.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • C Offline
        C Offline
        chiffaCff
        wrote on last edited by
        #3

        get the error:
        mainwindow.cpp:35: error: ambiguous overload for 'operator+' (operand types are 'QChar' and 'QByteArray')
        SerialPort->WriteToPort(QByteArray(QChar(253)+ui->lineEdit_2->text().toLocal8Bit()+QChar(254)));
        ^

        K 1 Reply Last reply
        0
        • C chiffaCff

          get the error:
          mainwindow.cpp:35: error: ambiguous overload for 'operator+' (operand types are 'QChar' and 'QByteArray')
          SerialPort->WriteToPort(QByteArray(QChar(253)+ui->lineEdit_2->text().toLocal8Bit()+QChar(254)));
          ^

          K Offline
          K Offline
          koahnig
          wrote on last edited by
          #4

          @chiffaCff

          What is the prototyp for WriteToPort look like?

          QString tmp (QChar(253)+ui->lineEdit_2->text()+QChar(254));
          SerialPort->WriteToPort(tmp.toLocal8Bit());
          

          Not sure why you are using toLocal8bit. With standard string the statement above shall work, so it should be with QString, I guess.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply
          0
          • M Offline
            M Offline
            mateczek
            wrote on last edited by mateczek
            #5
              QString s="string text";
              QByteArray b= s.toUtf8()+QByteArray("tekst")+QByteArray::fromHex("1310");
              SerialPort->WriteToPort(b);
            
            1 Reply Last reply
            0
            • C Offline
              C Offline
              chiffaCff
              wrote on last edited by
              #6

              Thanks for everyone . Create like:

              QByteArray data;
                 data.append(char(253));
                 data.append(ui->lineEdit_3->text().toLocal8Bit());
                 data.append(char(254));
              
              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