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 string by serial port
Forum Updated to NodeBB v4.3 + New Features

Sending string by serial port

Scheduled Pinned Locked Moved Unsolved General and Desktop
qtserialport st
4 Posts 3 Posters 8.0k Views 3 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.
  • ToPoT Offline
    ToPoT Offline
    ToPo
    wrote on last edited by ToPo
    #1

    Hi Pros,
    i have a problem with sending a string by the serial port.
    It´s just a little "hello-World" program...

    The port opens correctly, i´m sending a constant string after opening:

    serial->write("Hello World!\n");

    In the UI i´ve created a slider and a label. For the slider i´ve created a slot:

    connect(ui->slider_1, SIGNAL(valueChanged(int)), this, SLOT(slider_changed(int)));

    And this is the subroutine called by the slot:

    void MainWindow::slider_changed(int value)
    {
    QString buffer = QString("%1\n").arg(QString::number(value));
    serial->write((char*)buffer.data(), buffer.length());
    serial->QSerialPort::waitForBytesWritten(-1);
    ui->test_label->setText(buffer);
    }

    So far, so good. The program compiles fine, the "Hello World" is send. I´ve connected two FTDI on different USB-Ports and control the output via a terminal.

    As soon as i slide the slider the label is changed to the integer value of the slider (0-255).
    BUT: On the serial port i just receive something like "4.14.0". The \n isn´t send at all.
    I tried already to fill the buffer via sprintf, exactly the same problem.

    What is the problem???
    Hope you can help me,
    thanks

    Tobias

    1 Reply Last reply
    0
    • M Offline
      M Offline
      michelson
      wrote on last edited by
      #2

      Mayby try:
      serial->write( buffer.toStdString().c_str(), buffer.size() );

      1 Reply Last reply
      1
      • ToPoT Offline
        ToPoT Offline
        ToPo
        wrote on last edited by
        #3

        @michelson said:

        serial->write( buffer.toStdString().c_str(), buffer.size() );

        Tanks a lot! Now it works fine :)

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          Hi and welcome to devnet,

          There's no need to use QString and all these conversions. You should use QByteArray directly. Something like:

          QByteArray buffer = QByteArray::number(value) + "\n";
          serial->write(buffer);
          

          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
          6

          • Login

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