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 doesn't actually print to port
Forum Updated to NodeBB v4.3 + New Features

QSerialPort doesn't actually print to port

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 1.2k Views 2 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.
  • ExcludosE Offline
    ExcludosE Offline
    Excludos
    wrote on last edited by
    #1

    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
    0
    • ExcludosE Offline
      ExcludosE Offline
      Excludos
      wrote on last edited by
      #2

      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
      0
      • K Offline
        K Offline
        kuzulis
        Qt Champions 2020
        wrote on last edited by kuzulis
        #3

        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
        0
        • ExcludosE Offline
          ExcludosE Offline
          Excludos
          wrote on last edited by
          #4

          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
          0
          • MuzabM Offline
            MuzabM Offline
            Muzab
            wrote on last edited by
            #5

            Can you try!

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

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

              @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
              0

              • Login

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