Sending single byte over QSerialPort
-
Hello,
I have the below code and I'm trying to send single bytes of data using QSerialPort, but I get the error
error: invalid user-defined conversion from 'QByteRef' to 'const char*' [-fpermissive]
Any help will be appreciated.@void MainWindow::serialTX()
{
//char mydata[] = {0x52, 0x53, '\0'};
QByteArray ba;
ba.resize(5);
ba[0] = 0x41;
ba[1] = 0x42;
ba[2] = 0x43;
ba[3] = 0x44;
ba[4] = 0x45;
//QByteArray data = QByteArray::fromRawData(mydata, sizeof(mydata));serial->write(ba[1]); //qDebug() << data; //serial->close();
}@
-
-It sounds like write is expecting a const char *, have you tried casting it? Also it would be good to see your serial class as QSerialPort doesn't contain write-
-
Hi,
Maybe a silly question but why do you create a QByteArray of size 5 if you only want to send one byte ?
@ ion_knight, QSerialPort is a QIODevice so it has write functions.
-
QSerialPort inherits write() functions from QIODevice and that write functions expect either "zero terminated const char*":http://qt-project.org/doc/qt-5/qiodevice.html#write-2 or "const char* and size":http://qt-project.org/doc/qt-5/qiodevice.html#write
Try this construction
@
serial->write(ba.data[1], 1);
@ -
I get another error instead
error: invalid types '<unresolved overloaded function type>[int]' for array subscript
[quote author="andreyc" date="1418160521"]QSerialPort inherits write() functions from QIODevice and that write functions expect either "zero terminated const char*":http://qt-project.org/doc/qt-5/qiodevice.html#write-2 or "const char* and size":http://qt-project.org/doc/qt-5/qiodevice.html#write
Try this construction
@
serial->write(ba.data[1], 1);
@[/quote]^
-
Yea silly of me didn't see that.
-
[quote author="valerio.j" date="1418161384"]I get another error instead
error: invalid types '<unresolved overloaded function type>[int]' for array subscript
[/quote]Right. It should be either &(ba.data()[1]) or ba.data() + 1
@
serial->write(&(ba.data()[1]), 1);
@