I'm writing a qt bridge to access serial thermal printers by web apps. I'm having an issue settings serial port settings, here is my code:
bool Bridge::printToSerial(QByteArray arr, QString serialPortName, quint16 baud) {
/*
QProcess proc;
QStringList args = QStringList() << QString("mode COM1 BAUD=%1 PARITY=%2 DATA=%3" ).arg(9600).arg( "n" ).arg(8);
proc.start( "cmd.exe", args );
proc.waitForStarted();
proc.close();
*/
QSerialPort* m_port = new QSerialPort(this);
m_port->setPortName(serialPortName); // \\.\COM1
if(!m_port->open(QSerialPort::ReadWrite)) {
qDebug() << "Error serial open";
} else {
if (arr.isEmpty()) {
qDebug() << QObject::tr("Either no data was currently available on the standard input for reading, or an error occurred for port %1, error: %2").arg(serialPortName).arg(m_port->errorString()) << endl;
delete m_port;
return 1;
}
m_port->setBaudRate(baud);
m_port->setDataBits(QSerialPort::Data8);
m_port->setParity(QSerialPort::NoParity);
m_port->setStopBits(QSerialPort::OneStop);
//m_port->setBreakEnabled(true);
qint64 bytesWritten = m_port->write(arr);
if (bytesWritten == -1) {
qDebug() << QObject::tr("Failed to write the data to port %1, error: %2").arg(serialPortName).arg(m_port->errorString()) << endl;
delete m_port;
return 1;
} else if (bytesWritten != arr.size()) {
qDebug() << QObject::tr("Failed to write all the data to port %1, error: %2").arg(serialPortName).arg(m_port->errorString()) << endl;
delete m_port;
return 1;
} else if (!m_port->waitForBytesWritten(500)) {
qDebug() << QObject::tr("Operation timed out or an error occurred for port %1, error: %2").arg(serialPortName).arg(m_port->errorString()) << endl;
delete m_port;
return 1;
}
m_port->close();
delete m_port;
qDebug() << QObject::tr("Data successfully sent to port %1").arg(serialPortName) << endl;
return false;
}
delete m_port;
return true;
}
the problem is that the data prints but doesn't finish, which made me thing that data bits is not set to 8 bits.
I executed the command: mode COM1 and it shows that the port isn't configured properly ( baud rate, bits, parity all wrong ). btw the device manager is showing the default settings of the port and a different result than the cmd line: "mode COM1"
To make it work, I either have to execute the cmd line: mode COM1 BAUD=9600 PARITY=n DATA=8 before I run the program. or call m_port->setBreakEnabled(true); after I open the port. This call hangs the program, and I have to recompile the exe without to work. When the system restarts, command line mode show the old results ( baud rate 1200, data bits 7 etc )
Any clue why I'm having this behavior ? It seems QSerialPort is unable to effectively change all the settings I require.