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. [SOLVED]Serial port not sending bytes in my application
Forum Updated to NodeBB v4.3 + New Features

[SOLVED]Serial port not sending bytes in my application

Scheduled Pinned Locked Moved General and Desktop
12 Posts 3 Posters 12.6k 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.
  • J Offline
    J Offline
    JustGreg
    wrote on 12 Mar 2013, 14:25 last edited by
    #1

    Hi!

    I created a test app to experiment with QtSerialPort. To verify the behaviour of my app I made a simple embedded device which displays the received bytes on its LCD display. The device is using an FTDI USB-serial converter. The device has been tested carefully in Windows.

    In Linux, I checked with dmesg that the device is listed as ttyUSB0. I gave full permission to everybody to use this device with

    @sudo chmod 0666 /dev/ttyUSB0@

    I tested it with Cutecom and it worked. I also tried with an example project ('Terminal') which came with QtSerialPort package. The 'Terminal' application worked too. However, there is no luck with my app which is very simple and there is not much to go wrong. Here is my code:

    mainwindow.h

    @private:
    Ui::MainWindow *ui;
    QSerialPort *serial;@

    mainwindow.cpp

    @MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    bool status = false;

    QString pname = "ttyUSB0";
    
    serial = new QSerialPort(this);
    serial->setPortName(pname);
    
    status = serial->open(QIODevice::ReadWrite);
    
    if (status){
        serial->setBaudRate(QSerialPort::Baud115200,QSerialPort::AllDirections);
        serial->setDataBits(QSerialPort::Data8);
        serial->setStopBits(QSerialPort::OneStop);
        serial->setFlowControl(QSerialPort::NoFlowControl);
        serial->setParity(QSerialPort::NoParity);
    
        serial->putChar('W');
    }
    else
        serial->close();
    
    serial->close();
    

    }@

    The status variable gave me 'true' which means the port had been opened. But the character 'W' didn't show up on my LCD display nonetheless. I've been eyeballing it for a few days now in vain so any advice would be great. Thank you.

    Edit: the display is OLED not LCD, typo

    1 Reply Last reply
    0
    • T Offline
      T Offline
      toptan
      wrote on 12 Mar 2013, 14:36 last edited by
      #2

      You probably need to read docs for your LCD more thoroughly. I doubt that sending single letter via serial will cause the display to display that letter.

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JustGreg
        wrote on 12 Mar 2013, 14:40 last edited by
        #3

        [quote author="toptan" date="1363098990"]You probably need to read docs for your LCD more thoroughly. I doubt that sending single letter via serial will cause the display to display that letter.[/quote]

        The external device is working correctly. Trust me, I'm not sending single letters to an OLED display :-)

        1 Reply Last reply
        0
        • T Offline
          T Offline
          toptan
          wrote on 12 Mar 2013, 14:54 last edited by
          #4

          Have you checked whether putChar returns true. After all it is QIODevice of some king. Also you could try write and waitForBytesWritten combo. Something like this:
          @
          QByteArray data("W");
          serial.write(data);
          if (!serial.waitForBytesWritten(waitTimeout)) {
          /* timeout handling */
          }
          @

          I am using array of six 7 segment led displays in my project and QSerialPort works no problem on linux.

          1 Reply Last reply
          0
          • K Offline
            K Offline
            kuzulis
            Qt Champions 2020
            wrote on 12 Mar 2013, 15:16 last edited by
            #5

            2 JustGreg

            Remove serial->close() in end of MainWindow constructor.

            1 Reply Last reply
            0
            • J Offline
              J Offline
              JustGreg
              wrote on 12 Mar 2013, 15:31 last edited by
              #6

              [quote author="kuzulis" date="1363101362"]2 JustGreg

              Remove serial->close() in end of MainWindow constructor.[/quote]

              Thank you kuzulis. That was the problem, however my app is still not sending the bytes when I'm stepping over the code in debug mode. Only in 'Run' mode. Do you know what can cause this behaviour? It'd be nice to know.

              @toptan: the putChar method returned true, even in debug mode where there were no bytes sent to my device...strange. Thank you for your help, anyway.

              1 Reply Last reply
              0
              • T Offline
                T Offline
                toptan
                wrote on 12 Mar 2013, 15:35 last edited by
                #7

                No problem. I also have the problem in debug mode. I overcame that with a lot of qDebug(), if you find solution for step-by-step debugging please share.

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  kuzulis
                  Qt Champions 2020
                  wrote on 12 Mar 2013, 15:37 last edited by
                  #8

                  bq. That was the problem, however my app is still not sending the bytes when I’m stepping over the code in debug mode. Only in ‘Run’ mode. Do you know what can cause this behaviour? It’d be nice to know.

                  Because all I/O operations processing asynchronously in Qt event-loop, so in debug step-by-step event-loop is stopped by step-by-step. :)

                  So, nothing strange in this...

                  UPD:
                  Also, port->write() always returns true, because it write data to internal ring buffer.
                  So, for catch errors you can connect to SerialPortError signal.

                  1 Reply Last reply
                  0
                  • J Offline
                    J Offline
                    JustGreg
                    wrote on 12 Mar 2013, 15:44 last edited by
                    #9

                    [quote author="toptan" date="1363102507"]No problem. I also have the problem in debug mode. I overcame that with a lot of qDebug(), if you find solution for step-by-step debugging please share.[/quote]

                    Right, I'll check qDebug then. I'm new to Qt & Linux so do not expect too much brainpower from me :-)

                    [quote author="kuzulis" date="1363102625"]
                    Because all I/O operations processing asynchronously in Qt event-loop, so in debug step-by-step event-loop is stopped by step-by-step. :)

                    So, nothing strange in this...[/quote]

                    Good to know. Thank you and I'll be back soon with my receive event signal-slot questions :-)

                    1 Reply Last reply
                    0
                    • J Offline
                      J Offline
                      JustGreg
                      wrote on 12 Mar 2013, 15:52 last edited by
                      #10

                      [quote author="kuzulis" date="1363101362"]2 JustGreg

                      Remove serial->close() in end of MainWindow constructor.[/quote]

                      One more question: what could cause the putChar process to fail? The close method was too fast and it had already closed the port before the internal buffer was emptied?

                      1 Reply Last reply
                      0
                      • K Offline
                        K Offline
                        kuzulis
                        Qt Champions 2020
                        wrote on 12 Mar 2013, 15:55 last edited by
                        #11

                        Yes. At the moment to the call close() does not force flush data.

                        So, you can do port->flush() before port->close()

                        UPD: On future maybe behavior can change... and flush() will called automatically on close().

                        1 Reply Last reply
                        0
                        • J Offline
                          J Offline
                          JustGreg
                          wrote on 12 Mar 2013, 15:58 last edited by
                          #12

                          Ok and thanks!

                          1 Reply Last reply
                          0

                          1/12

                          12 Mar 2013, 14:25

                          • Login

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