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. Serial Port In windows

Serial Port In windows

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

    Hi dear friends .
    I have a problem to open a serial port for the first time after plugged in to the Computer. when I plugged in to my system my codes doesn't able to send or receive data into Dongle but if I check this port by putty software it is OK and it is very interest because after check by putty all my codes will worked and I can send or receive data to my Dongle.
    I don't know how I can fix this problem.

    serial->setPortName("COM3");
    serial->setBaudRate(QSerialPort::Baud9600);
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::NoFlowControl);
    serial->open(QIODevice::ReadWrite);
    serial->write("permit");
    connect(serial,SIGNAL(readyRead()),this,SLOT(serialRecived()));

    void Login::serialRecived()
    {
    QByteArray ba;
    ba="name:Dual";
    serial->write(ba);
    out=serial->readAll();
    }

    Is there any one here to help me for fixing this problem?

    JonBJ 1 Reply Last reply
    0
    • S saeidparand

      Hi dear friends .
      I have a problem to open a serial port for the first time after plugged in to the Computer. when I plugged in to my system my codes doesn't able to send or receive data into Dongle but if I check this port by putty software it is OK and it is very interest because after check by putty all my codes will worked and I can send or receive data to my Dongle.
      I don't know how I can fix this problem.

      serial->setPortName("COM3");
      serial->setBaudRate(QSerialPort::Baud9600);
      serial->setDataBits(QSerialPort::Data8);
      serial->setParity(QSerialPort::NoParity);
      serial->setStopBits(QSerialPort::OneStop);
      serial->setFlowControl(QSerialPort::NoFlowControl);
      serial->open(QIODevice::ReadWrite);
      serial->write("permit");
      connect(serial,SIGNAL(readyRead()),this,SLOT(serialRecived()));

      void Login::serialRecived()
      {
      QByteArray ba;
      ba="name:Dual";
      serial->write(ba);
      out=serial->readAll();
      }

      Is there any one here to help me for fixing this problem?

      JonBJ Online
      JonBJ Online
      JonB
      wrote on last edited by JonB
      #2

      @saeidparand
      I do not know if the following will help your issue, because that might be something like putty sets something on the port which you do not, enabling it to work. However, there are two things which could be problematic in your code:

      serial->write("permit");
      connect(serial,SIGNAL(readyRead()),this,SLOT(serialRecived()));
      

      Assuming send that can result in a reply (looks like it), always connect signals/slots before the first time the other end can raise a signal. Your code could miss the reply. Swap the order of these two lines.

      serial->write(ba);
      out=serial->readAll();
      

      You cannot assume the write() will be sent and complete immediately, so the readAll() could be too early to see the reply, and furthermore it will only read whatever happens to have been received at the instant it is called, so it might get nothing or only part of the reply. Change to using signal/slot for receiving all data.

      OIC, you are using signal/slot. Then, it seems to me, it gets worse. Every time you get any readyRead signal, OK you are calling readAll() at that point, but you are also sending "name:Dual" to the device. Is that really correct? On every read, and at least theoretically if you receive the reply back in multiple chunks you send the message for each chunk received??

      Also you should slot onto the errorOccurred signal, you don't know if that's being raised.

      1 Reply Last reply
      3
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

        Hi
        You can also use the Terminal sample.
        Its a serial program allowing setting up the serial and send and receive.
        https://doc.qt.io/qt-5/qtserialport-terminal-example.html
        Its available directly in Creator.

        aha_1980A 1 Reply Last reply
        1
        • mrjjM mrjj

          Hi
          You can also use the Terminal sample.
          Its a serial program allowing setting up the serial and send and receive.
          https://doc.qt.io/qt-5/qtserialport-terminal-example.html
          Its available directly in Creator.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @mrjj That's what I already told him last month in https://forum.qt.io/topic/107185/send-and-receive-data-from-serial-port

          Regard

          Qt has to stay free or it will die.

          S 1 Reply Last reply
          4
          • aha_1980A aha_1980

            @mrjj That's what I already told him last month in https://forum.qt.io/topic/107185/send-and-receive-data-from-serial-port

            Regard

            S Offline
            S Offline
            saeidparand
            wrote on last edited by
            #5

            @aha_1980 Dear friend , you told me visit the example last month , I read sample code , and write my own code.but my code has a bug.
            thanks for your help.

            aha_1980A 1 Reply Last reply
            0
            • S saeidparand

              @aha_1980 Dear friend , you told me visit the example last month , I read sample code , and write my own code.but my code has a bug.
              thanks for your help.

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @saeidparand but did the example work correctly?

              Qt has to stay free or it will die.

              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