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
Forum Updated to NodeBB v4.3 + New Features

Serial Port In windows

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 4 Posters 2.1k 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.
  • S Offline
    S Offline
    saeidparand
    wrote on 21 Oct 2019, 09:56 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?

    J 1 Reply Last reply 21 Oct 2019, 11:20
    0
    • S saeidparand
      21 Oct 2019, 09:56

      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?

      J Offline
      J Offline
      JonB
      wrote on 21 Oct 2019, 11:20 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
      • M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 21 Oct 2019, 15:05 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.

        A 1 Reply Last reply 21 Oct 2019, 15:16
        1
        • M mrjj
          21 Oct 2019, 15:05

          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.

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 21 Oct 2019, 15:16 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 22 Oct 2019, 07:12
          4
          • A aha_1980
            21 Oct 2019, 15:16

            @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 22 Oct 2019, 07:12 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.

            A 1 Reply Last reply 22 Oct 2019, 15:55
            0
            • S saeidparand
              22 Oct 2019, 07:12

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

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 22 Oct 2019, 15:55 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

              3/6

              21 Oct 2019, 15:05

              • Login

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