PDA

View Full Version : Sending data over QSerialPort



jvalerio
12th February 2016, 16:01
Hello,

I'm trying to send data packets of 11 bytes using QSerialPort. It starts working fine, but it doesn't transmit all the bytes over, only sends about 8.
Not sure why it's behaving like this, any insight will be helpfull.


#include <QCoreApplication>
#include <QTextStream>
#include <QSerialPort>
#include <QDebug>
#include <QFile>
#include <QTimer>
#include <QtSerialPort/QSerialPortInfo>
#include <QStringList>

QSerialPort serial;

int sendCmd(const QByteArray &myCmd);

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

serial.setPortName("COM7");
serial.setBaudRate(QSerialPort::Baud9600);
serial.setDataBits(QSerialPort::Data8);
serial.setParity(QSerialPort::NoParity);
serial.setStopBits(QSerialPort::OneStop);
serial.setFlowControl(QSerialPort::NoFlowControl);

if (serial.open(QIODevice::ReadWrite))
{

qDebug() << "Connected";
}
else
{
qDebug() << "Error";

}

QByteArray testOuts;

testOuts[0] = '$';
testOuts[1] = 'D';
testOuts[2] = 'W';
testOuts[3] = '!';
testOuts[4] = 'S';
testOuts[5] = 'H';
testOuts[6] = 'P';
testOuts[7] = 'X';
testOuts[8] = 'X';
testOuts[9] = 'O';
testOuts[10] = '#';

serial.write (testOuts);
serial.flush();
serial.waitForBytesWritten(1000);

//return a.exec();
//serial.close();
}

anda_skoa
12th February 2016, 16:40
Have you checked bytesToWrite() after waitForBytesWritten()? Is the value 0?

Cheers,
_