Adding individual bytes to message for serial port
-
wrote on 9 Feb 2023, 14:36 last edited by agmar 2 Sept 2023, 14:37
Hi,
i managed to simplify the code and i understand most of it, however, i am not sure how to combine the individual bytes into an array that could be sentvoid MainWindow::openSeriall(){ m_serial->setPortName(portName); m_serial->setBaudRate(QSerialPort::Baud19200); m_serial->setDataBits(QSerialPort::Data8); m_serial->setParity(QSerialPort::NoParity); m_serial->setFlowControl(QSerialPort::NoFlowControl); m_serial->setStopBits(QSerialPort::OneStop); } void MainWindow::writeSerial() { quint8 message = 0xFF; message += quint8(mAddress) ; // message += QChar(0x55) ; // message += QChar(byte4) ; // message += QChar(mPSpeed) ; // message += QChar(mTSpeed) ; // message += QChar(mCheckSum) ; qDebug() << message ; if (m_serial->open(QIODevice::ReadWrite)) { QByteArray thisSend = message.toUtf8(); m_serial->write(thisSend); m_serial->waitForBytesWritten(-1); m_serial->close(); } }
i can send the values as QChars, but that adds an additional byte to the output,as i understand and that is not my goal
here is the issue:error: request for member ‘toUtf8’ in ‘message’, which is of non-class type ‘quint8’ {aka ‘unsigned char’}
119 | quint8 thisSend = message.toUtf8();
| ^~~~~~ -
Hi,
i managed to simplify the code and i understand most of it, however, i am not sure how to combine the individual bytes into an array that could be sentvoid MainWindow::openSeriall(){ m_serial->setPortName(portName); m_serial->setBaudRate(QSerialPort::Baud19200); m_serial->setDataBits(QSerialPort::Data8); m_serial->setParity(QSerialPort::NoParity); m_serial->setFlowControl(QSerialPort::NoFlowControl); m_serial->setStopBits(QSerialPort::OneStop); } void MainWindow::writeSerial() { quint8 message = 0xFF; message += quint8(mAddress) ; // message += QChar(0x55) ; // message += QChar(byte4) ; // message += QChar(mPSpeed) ; // message += QChar(mTSpeed) ; // message += QChar(mCheckSum) ; qDebug() << message ; if (m_serial->open(QIODevice::ReadWrite)) { QByteArray thisSend = message.toUtf8(); m_serial->write(thisSend); m_serial->waitForBytesWritten(-1); m_serial->close(); } }
i can send the values as QChars, but that adds an additional byte to the output,as i understand and that is not my goal
here is the issue:error: request for member ‘toUtf8’ in ‘message’, which is of non-class type ‘quint8’ {aka ‘unsigned char’}
119 | quint8 thisSend = message.toUtf8();
| ^~~~~~@agmar said in Adding individual bytes to message for serial port:
quint8 message = 0xFF;
If you need an array then use one:
QByteArray message; message.append(0xFF); ... m_serial->write(message);
-
1/2