QSerialPort Writing issue



  • Hi,
    I have written a simple serial port writing code as follows :```
    //your code here

    **#include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtSerialPort/QSerialPort>
    QSerialPort serial;
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        serial.setPortName("com1");
        serial.open(QIODevice::ReadWrite);
        serial.setBaudRate(QSerialPort::Baud9600);
        serial.setDataBits(QSerialPort::Data8);
        serial.setParity(QSerialPort::NoParity);
        serial.setStopBits(QSerialPort::OneStop);
        serial.setFlowControl(QSerialPort::NoFlowControl);
    
        serial.write("OK");
    
    }
    

    my COM1 is connected to a hardware.
    Now the issue is i am not able to recieve this data at hardware end, but if i am sending same data with different app like docklite , i am getting correct data at hardware end.
    Also i am able to see this data in serial port monitor,but not at hardware end.
    What can be the issue:

    0_1542000483733_1111.JPG

    [Edit: fixed coding tags SGaist]



  • @Vishu1236
    two points.

    first, set the settings of the serial port before you call open().
    second open returns a bool if opening was successful or not. You should check that.



  • Hi,
    thanx for your reply
    i have changed my code to :

    //your code here
    

    #include "mainwindow.h"
    #include "ui_mainwindow.h"
    #include <QtSerialPort/QSerialPort>
    QSerialPort *serial;

    MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    serial = new QSerialPort(this);
    serial->setPortName("com1");
    connect(serial,SIGNAL(readyRead()),this,SLOT(serialReceived()));
    serial->setBaudRate(QSerialPort::Baud9600);
    serial->setDataBits(QSerialPort::Data8);
    serial->setParity(QSerialPort::NoParity);
    serial->setStopBits(QSerialPort::OneStop);
    serial->setFlowControl(QSerialPort::NoFlowControl);
    serial->open(QIODevice::WriteOnly);

    serial->setDataTerminalReady(false);
    if (serial->isOpen() && serial->isWritable())
    {
        serial->write("OK");
    }
    

    }

    still able to see data at serial port monitor but not at my harware.
    serial port monitor is showing :

    Port opened by process "serialport.exe" (PID: 8120)

    Request: 11/12/2018 11:30:41 AM.24564 (+314.5910 seconds)

    4F 4B OK

    Port closed



  • @J.Hilk
    Hi
    i tried printing errors and found "unknown error" errorno. 22 in setStopBits() ,setFlowControl.

    any sugesstions??


  • Lifetime Qt Champion

    Hi,

    22 ? It's not part of the QSerialPort:: SerialPortError enum values.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.