Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Serial Port In windows



  • 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?



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


  • Lifetime Qt Champion

    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.


  • Lifetime Qt Champion

    @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



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


  • Lifetime Qt Champion

    @saeidparand but did the example work correctly?


Log in to reply