Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. QSerialPort bytesAvailable() and read() delayed, QextSerialPort works
Forum Updated to NodeBB v4.3 + New Features

QSerialPort bytesAvailable() and read() delayed, QextSerialPort works

Scheduled Pinned Locked Moved Mobile and Embedded
21 Posts 7 Posters 14.5k 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.
  • B Offline
    B Offline
    Brembo Breidscheid
    wrote on last edited by
    #1

    Hey guys,
    I communicate with a device using a simple protocol and was using QextSerialDevice. The protocol requires to send some data and then process the answer. The messages are quite small (a few bytes).
    I send the message to the device and then try to receive the answer (no signal / slot stuff).

    It seems that QSerialPort::bytesAvailable() does not immediately report when bytes where received. Even after some bytes are received, bytesAvailable() still reports 0 and read() would not return any data. When more data have been received, bytesAvailable() suddenly reports all of them and read() would return (appaerently) all data. Naturally, this behaviour kills my use case.

    I switch back to QextSerialPort for now but want to make people aware of this.

    The system runs under Windows (32bit min-gw) and I am using Qt 5.3.1.

    Is this a known / wanted behaviour? Does anyone know a fix?

    Cheers,
    Brembo

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

      Hey,
      Just for clarification: I am facing with the similar problem on Linux with QSerialPort shipped with Qt 5.3.1.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        mr_wallyit
        wrote on last edited by
        #3

        Hello,

        Have you tried the simple project ("terminal") in Qt5?
        In Qt 5.3.1 we have ready serial port, I tried with Android, Windows XP, 7, 8 correctly.

        PS
        With Android I have done a workarround.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          martonmiklos
          wrote on last edited by
          #4

          I do not know why are you come up with the terminal example, because it is clearly using the event driven approach.

          There is a message here from one of the developers of the QSerialPort mentioning the root cause of the problems:
          https://www.mail-archive.com/interest@qt-project.org/msg08712.html

          But there is not any word in the documentation about that the read, write, bytesAvailabe methods working differently than what a developer should expect from a QIODevice...

          Even the example code in the documentation is incorrect: the waitforReadyRead cannot be called without argument.

          @int numRead = 0, numReadTotal = 0;
          char buffer[50];

          forever {
          numRead = serial.read(buffer, 50);

          // Do whatever with the array
          
          numReadTotal += numRead;
          if (numRead == 0 && !serial.waitForReadyRead())
              break;
          

          }@

          1 Reply Last reply
          0
          • McLionM Offline
            McLionM Offline
            McLion
            wrote on last edited by
            #5

            Already posted the same issue here: http://qt-project.org/forums/viewthread/44873/
            I also switched back to QextSerialPort.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mr_wallyit
              wrote on last edited by
              #6

              Hello,

              I use QSerialport but I prefer event driver because if you use waitForReadyRead() the gui is blocked.

              Have you checked the project terminal?

              http://qt-project.org/doc/qt-5/qtserialport-terminal-example.html

              This is very important because when bytes are on buffer you receive a signal
              connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));

              void MainWindow::readData()
              {
              QByteArray data = serial->readAll();
              console->putData(data);
              }

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jack249
                wrote on last edited by
                #7

                my solution:
                call QCoreApplication::processEvents(); after write() then bytesAvailable() will be updated

                1 Reply Last reply
                0
                • K Offline
                  K Offline
                  KeepGoing
                  wrote on last edited by
                  #8

                  [quote author="mr_wallyit" date="1410691263"]Hello,

                  Have you tried the simple project ("terminal") in Qt5?
                  In Qt 5.3.1 we have ready serial port, I tried with Android, Windows XP, 7, 8 correctly.

                  PS
                  With Android I have done a workarround.[/quote]

                  Hello mr_wallyit
                  You said that you were able to use the Serial port with android in Qt5, could you please share with me, how this was done? I would appreciate your response. Thank you in advance.

                  1 Reply Last reply
                  0
                  • K Offline
                    K Offline
                    KeepGoing
                    wrote on last edited by
                    #9

                    [quote author="mr_wallyit" date="1410691263"]Hello,

                    Have you tried the simple project ("terminal") in Qt5?
                    In Qt 5.3.1 we have ready serial port, I tried with Android, Windows XP, 7, 8 correctly.

                    PS
                    With Android I have done a workarround.[/quote]

                    Hello mr_wallyit
                    You said that you were able to use the Serial port with android in Qt5, could you please share with me, how this was done? I would appreciate your response. Thank you in advance.

                    1 Reply Last reply
                    0
                    • M Offline
                      M Offline
                      mr_wallyit
                      wrote on last edited by
                      #10

                      Hello,
                      First, serial Port on Android works if the Kernel is builded with this module.

                      Second,file System of Android is not standard,there are not standard folders for the
                      serial port, so I changed the driver with a few line of code.

                      I changed the method serialPortLockFilePath in the fileqserialport_unix_p.cpp

                      @

                      QString serialPortLockFilePath(const QString &portName)
                      {

                      qDebug() << __LINE__ << __FILE__;
                      static const QStringList lockDirectoryPaths = QStringList()
                      

                      //#ifdef Q_OS_ANDROID
                      #ifdef ANDROID

                          << QStringLiteral("/sdcard/data/lock")
                          << QStringLiteral("/sdcard/data/locks")
                          << QStringLiteral("/sdcard/data/spool/locks")
                          << QStringLiteral("/sdcard/data/spool/uucp")
                          << QStringLiteral("/sdcard/data/tmp")
                          << QStringLiteral("/data/local/tmp");
                          qDebug() << __LINE__ << __FILE__;
                      

                      #else

                          << QStringLiteral("/var/lock")
                          << QStringLiteral("/etc/locks")
                          << QStringLiteral("/var/spool/locks")
                          << QStringLiteral("/var/spool/uucp")
                          << QStringLiteral("/tmp");
                          qDebug() << __LINE__ << __FILE__;
                      

                      #endif

                      @

                      Third,

                      You need to create these directories:

                        << QStringLiteral("/sdcard/data/lock")
                          << QStringLiteral("/sdcard/data/locks")
                          << QStringLiteral("/sdcard/data/spool/locks")
                          << QStringLiteral("/sdcard/data/spool/uucp")
                          << QStringLiteral("/sdcard/data/tmp")
                      
                      1 Reply Last reply
                      0
                      • M Offline
                        M Offline
                        mr_wallyit
                        wrote on last edited by
                        #11

                        Hello,
                        First, serial Port on Android works if the Kernel is builded with this module.

                        Second,file System of Android is not standard,there are not standard folders for the
                        serial port, so I changed the driver with a few line of code.

                        I changed the method serialPortLockFilePath in the fileqserialport_unix_p.cpp

                        @

                        QString serialPortLockFilePath(const QString &portName)
                        {

                        qDebug() << __LINE__ << __FILE__;
                        static const QStringList lockDirectoryPaths = QStringList()
                        

                        //#ifdef Q_OS_ANDROID
                        #ifdef ANDROID

                            << QStringLiteral("/sdcard/data/lock")
                            << QStringLiteral("/sdcard/data/locks")
                            << QStringLiteral("/sdcard/data/spool/locks")
                            << QStringLiteral("/sdcard/data/spool/uucp")
                            << QStringLiteral("/sdcard/data/tmp")
                            << QStringLiteral("/data/local/tmp");
                            qDebug() << __LINE__ << __FILE__;
                        

                        #else

                            << QStringLiteral("/var/lock")
                            << QStringLiteral("/etc/locks")
                            << QStringLiteral("/var/spool/locks")
                            << QStringLiteral("/var/spool/uucp")
                            << QStringLiteral("/tmp");
                            qDebug() << __LINE__ << __FILE__;
                        

                        #endif

                        @

                        Third,

                        You need to create these directories:

                          << QStringLiteral("/sdcard/data/lock")
                            << QStringLiteral("/sdcard/data/locks")
                            << QStringLiteral("/sdcard/data/spool/locks")
                            << QStringLiteral("/sdcard/data/spool/uucp")
                            << QStringLiteral("/sdcard/data/tmp")
                        
                        1 Reply Last reply
                        0
                        • M Offline
                          M Offline
                          mr_wallyit
                          wrote on last edited by
                          #12

                          Sorry I have deleted a duplicate message

                          1 Reply Last reply
                          0
                          • M Offline
                            M Offline
                            mr_wallyit
                            wrote on last edited by
                            #13

                            Sorry I have deleted a duplicate message

                            1 Reply Last reply
                            0
                            • K Offline
                              K Offline
                              KeepGoing
                              wrote on last edited by
                              #14

                              Hello Mr_Wallyit
                              Thank you for your reply.
                              My target is android. Is it the same with Android as well?

                              Thank you in advance.

                              1 Reply Last reply
                              0
                              • K Offline
                                K Offline
                                KeepGoing
                                wrote on last edited by
                                #15

                                Hello Mr_Wallyit
                                Thank you for your reply.
                                My target is android. Is it the same with Android as well?

                                Thank you in advance.

                                1 Reply Last reply
                                0
                                • M Offline
                                  M Offline
                                  mr_wallyit
                                  wrote on last edited by
                                  #16

                                  Hello,

                                  yes of course, I use Point of sale Industrial with Android, with workarround we can use serial port.

                                  Best Regards

                                  1 Reply Last reply
                                  0
                                  • M Offline
                                    M Offline
                                    mr_wallyit
                                    wrote on last edited by
                                    #17

                                    Hello,

                                    yes of course, I use Point of sale Industrial with Android, with workarround we can use serial port.

                                    Best Regards

                                    1 Reply Last reply
                                    0
                                    • A Offline
                                      A Offline
                                      Abin
                                      wrote on last edited by
                                      #18

                                      Hi Mr_Wallyit,

                                      Thanks for sharing the details.

                                      I have an android device which has serial port attached to it. The manufacturer has a sample application with it which uses the serial port. The port name is "/dev/ttyS6".

                                      As you mentioned I tried to create folder in my device. My device file explorer shows files structure as "/mnt/sdcard". So I created folders as below:
                                      "/mnt/sdcard/data/lock"
                                      "/mnt/sdcard/data/locks"
                                      "/mnt/sdcard/data/spool/locks"
                                      "/mnt/sdcard/data/spool/uucp"
                                      "/mnt/sdcard/data/tmp"

                                      But, the folder "/mnt/data/local/tmp" is unable to create as my file explorer doesn't allow to create folder.

                                      I kept my code as @QString serialPortLockFilePath(const QString &portName)
                                      {
                                      static const QStringList lockDirectoryPaths = QStringList()

                                      #ifdef Q_OS_ANDROID
                                      << QStringLiteral("/mnt/sdcard/data/lock")
                                      << QStringLiteral("/mnt/sdcard/data/locks")
                                      << QStringLiteral("/mnt/sdcard/data/spool/locks")
                                      << QStringLiteral("/mnt/sdcard/data/spool/uucp")
                                      << QStringLiteral("/mnt/sdcard/data/tmp");
                                      #else
                                      << QStringLiteral("/var/lock")
                                      << QStringLiteral("/etc/locks")
                                      << QStringLiteral("/var/spool/locks")
                                      << QStringLiteral("/var/spool/uucp")
                                      << QStringLiteral("/tmp");
                                      #endif@

                                      And I have a second device whose files structure as "/sdcard". So I created folders as below:
                                      "/sdcard/data/lock"
                                      "/sdcard/data/locks"
                                      "/sdcard/data/spool/locks"
                                      "/sdcard/data/spool/uucp"
                                      "/sdcard/data/tmp"

                                      But, the folder "/data/local/tmp" is unable to create as my file explorer crashes on creating folder "/data/locaal" (need to retry with different file explorer application).

                                      @QString serialPortLockFilePath(const QString &portName)
                                      {
                                      static const QStringList lockDirectoryPaths = QStringList()

                                      #ifdef Q_OS_ANDROID
                                      << QStringLiteral("/sdcard/data/lock")
                                      << QStringLiteral("/sdcard/data/locks")
                                      << QStringLiteral("/sdcard/data/spool/locks")
                                      << QStringLiteral("/sdcard/data/spool/uucp")
                                      << QStringLiteral("/sdcard/data/tmp");
                                      #else
                                      << QStringLiteral("/var/lock")
                                      << QStringLiteral("/etc/locks")
                                      << QStringLiteral("/var/spool/locks")
                                      << QStringLiteral("/var/spool/uucp")
                                      << QStringLiteral("/tmp");
                                      #endif@

                                      and created a sample application which tries to access the serial port. I set the port name as @setPortName("/dev/ttyS6");@
                                      But when running the application I get the error "Permission denied".

                                      Could you help me to figure out where I am going wrong.

                                      Thanks in advance.

                                      1 Reply Last reply
                                      0
                                      • A Offline
                                        A Offline
                                        Abin
                                        wrote on last edited by
                                        #19

                                        Hi Mr_Wallyit,

                                        Thanks for sharing the details.

                                        I have an android device which has serial port attached to it. The manufacturer has a sample application with it which uses the serial port. The port name is "/dev/ttyS6".

                                        As you mentioned I tried to create folder in my device. My device file explorer shows files structure as "/mnt/sdcard". So I created folders as below:
                                        "/mnt/sdcard/data/lock"
                                        "/mnt/sdcard/data/locks"
                                        "/mnt/sdcard/data/spool/locks"
                                        "/mnt/sdcard/data/spool/uucp"
                                        "/mnt/sdcard/data/tmp"

                                        But, the folder "/mnt/data/local/tmp" is unable to create as my file explorer doesn't allow to create folder.

                                        I kept my code as @QString serialPortLockFilePath(const QString &portName)
                                        {
                                        static const QStringList lockDirectoryPaths = QStringList()

                                        #ifdef Q_OS_ANDROID
                                        << QStringLiteral("/mnt/sdcard/data/lock")
                                        << QStringLiteral("/mnt/sdcard/data/locks")
                                        << QStringLiteral("/mnt/sdcard/data/spool/locks")
                                        << QStringLiteral("/mnt/sdcard/data/spool/uucp")
                                        << QStringLiteral("/mnt/sdcard/data/tmp");
                                        #else
                                        << QStringLiteral("/var/lock")
                                        << QStringLiteral("/etc/locks")
                                        << QStringLiteral("/var/spool/locks")
                                        << QStringLiteral("/var/spool/uucp")
                                        << QStringLiteral("/tmp");
                                        #endif@

                                        And I have a second device whose files structure as "/sdcard". So I created folders as below:
                                        "/sdcard/data/lock"
                                        "/sdcard/data/locks"
                                        "/sdcard/data/spool/locks"
                                        "/sdcard/data/spool/uucp"
                                        "/sdcard/data/tmp"

                                        But, the folder "/data/local/tmp" is unable to create as my file explorer crashes on creating folder "/data/locaal" (need to retry with different file explorer application).

                                        @QString serialPortLockFilePath(const QString &portName)
                                        {
                                        static const QStringList lockDirectoryPaths = QStringList()

                                        #ifdef Q_OS_ANDROID
                                        << QStringLiteral("/sdcard/data/lock")
                                        << QStringLiteral("/sdcard/data/locks")
                                        << QStringLiteral("/sdcard/data/spool/locks")
                                        << QStringLiteral("/sdcard/data/spool/uucp")
                                        << QStringLiteral("/sdcard/data/tmp");
                                        #else
                                        << QStringLiteral("/var/lock")
                                        << QStringLiteral("/etc/locks")
                                        << QStringLiteral("/var/spool/locks")
                                        << QStringLiteral("/var/spool/uucp")
                                        << QStringLiteral("/tmp");
                                        #endif@

                                        and created a sample application which tries to access the serial port. I set the port name as @setPortName("/dev/ttyS6");@
                                        But when running the application I get the error "Permission denied".

                                        Could you help me to figure out where I am going wrong.

                                        Thanks in advance.

                                        1 Reply Last reply
                                        0
                                        • M Offline
                                          M Offline
                                          mr_wallyit
                                          wrote on last edited by
                                          #20

                                          Hello,

                                          this is my source code, I think who is wrong “/mnt/sdcard/data/lock”.

                                          @

                                          bool s = true;
                                          QStringList pathList;
                                          

                                          #ifdef ANDROID
                                          pathList << "/sdcard/data";
                                          pathList << "/sdcard/data/lock";
                                          pathList << "/sdcard/data/locks";
                                          pathList << "/sdcard/data/spool";
                                          pathList << "/sdcard/data/spool/locks";
                                          pathList << "/sdcard/data/spool/uucp";
                                          pathList << "/sdcard/data/tmp";
                                          pathList << "/sdcard/data/local";
                                          pathList << "/sdcard/data/local/tmp";
                                          #endif

                                          QDir d;
                                          foreach (const QString s, pathList) {
                                              d.setPath(s);
                                              if (!d.exists()){
                                                  if(d.mkpath(d.absolutePath()))
                                                      qCCritical() << "error create directory " << d.absolutePath();
                                              }
                                          }
                                          

                                          @

                                          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