Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Call for Presentations - Qt World Summit

    Unsolved QSerialPort doesn't actually print to port

    General and Desktop
    4
    6
    884
    Loading More Posts
    • 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.
    • Excludos
      Excludos last edited by

      I'm trying to make a very simple app in Qt that prints a "G" to a comport, that is connected to a driver through rs232. After sending the G, the light should turn on, and I should get a response from the driver. These are the snippets of code that deals with the serialport:

      The setup:

      ledDriver = new QSerialPort;
      
      ledDriver->setPortName(right);
      ledDriver->open(QSerialPort::ReadWrite);
      ledDriver->setBaudRate(QSerialPort::Baud38400);
      ledDriver->setDataBits(QSerialPort::Data8);
      ledDriver->setParity(QSerialPort::NoParity);
      ledDriver->setStopBits(QSerialPort::OneStop);
      ledDriver->setFlowControl(QSerialPort::NoFlowControl);
      

      and the writing that happens when you click a button:

      ledDriver->flush();
      if(ledDriver->isWritable()){
          QString temp = "G";
          qDebug() << temp;
          QByteArray ba = temp.toLocal8Bit();
          ledDriver->write(ba);
          if (ledDriver->waitForBytesWritten(1000)) {
          } else {
          qDebug() << "Write timed out";
          }
      }else{
          qDebug() << "Serial is not writable";
      }
      if (ledDriver->waitForReadyRead(1000)) {
          QByteArray responseData = ledDriver->readAll();
          while (ledDriver->waitForReadyRead(10))
              responseData += ledDriver->readAll();
      
          QString response(responseData);
          qDebug() << "response:" + response;
      } else {
      qDebug() << "Read timed out";
      }
      

      Now the only response I get is the exact same phrase I send. So if I send G, the response is "G" instead of the number I'm suppose to be getting.

      If I open Putty afterwards, and send anything (doesn't matter what), THEN the G is sendt, the light turns on, and I get the respons I'm suppose to be getting. So it seems like Qt is writing the signal but not sending it? Am I missing something very obvious here?

      Environment is Windows 10 x64 on Qt 5.5.1 with newest MinGW32.

      1 Reply Last reply Reply Quote 0
      • Excludos
        Excludos last edited by

        I should also add that I have the same issue when using the blockingmaster example (not a large surprise since that's where I've taken much of the code from). BUT the terminal example works perfectly..

        1 Reply Last reply Reply Quote 0
        • K
          kuzulis Qt Champions 2020 last edited by kuzulis

          These same and same and same questions appears hundred thousand times per month. And nobody want to search the solution (by googling) himself, it take about 5 minutes. The terminal example sends the 'CR' character when you press to "Enter" button, but your code does not!

          1 Reply Last reply Reply Quote 0
          • Excludos
            Excludos last edited by

            I understand the frustration about people asking the same questions, I share it equally, but I have been googling this for absolutely days now (and no, my googling skills do not suck). There is ZERO documentation in the code itself, so it's difficult enough to understand what the fuck is going on let alone debug the thing. I did also look through this forum quickly to see if there was a question similar to mine but couldn't find any.

            I thank you for your answer thought. That should help me plenty :)

            1 Reply Last reply Reply Quote 0
            • Muzab
              Muzab last edited by

              Can you try!

              ledDriver ->write(command.toStdString().c_str());

              1 Reply Last reply Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                @Muzab there's absolutely no need to do all these conversions. write takes a const char * or a QByteArray so the first thing to do in simple cases is just not use QString. As for the problem at hand, as @kuzulis noted there is the CR char that is missing from the commands used by @Excludos.

                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 Reply Quote 0
                • First post
                  Last post