S
S
Slavka2016-01-21 08:58:28
Qt
Slavka, 2016-01-21 08:58:28

How to use QtSerialPort?

QByteArray message;
    message.resize(7);
    message[0] = (char)':';
    message[1] = 0x1A;
    message[2] = 0x03;
    message[3] = 0x00;
    message[4] = 0x00;
    message[5] = 0x00;
    message[6] = 0x04;

  serialport->write(message);

here is the code and it always sends only ":" even if the number of bytes is specified as the second parameter, I tried it on the qt4 library assembled by myself and in the original one on qt5 .
Here is the port config
serialport->setPortName("COM6");

    if(!serialport->open(QIODevice::ReadWrite) )
    {
        QMessageBox  *msg = new QMessageBox();
        msg->setText("fail");
        msg->show();
    }


    serialport->setBaudRate(QSerialPort::Baud9600);
    serialport->setDataBits(QSerialPort::Data8);
    serialport->setStopBits(QSerialPort::OneStop);
    serialport->setFlowControl(QSerialPort::NoFlowControl);
    serialport->setParity(QSerialPort::NoParity);


}

In general, you just need to send a ModBus ASCII request

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2016-01-22
@Slavka_online

As Armenian Radio noted - you need to check the return code first.
Second, perhaps calling bool QSerialPort::waitForBytesWritten(int msecs) might help.

A
Armenian Radio, 2016-01-21
@gbg

The semantics of all such calls is the same - you specify how much you want to transfer, write gives you how much you actually transferred, or -1. Check the return code (and read the documentation ), of course you didn't bother

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question