Using QByteArray.data() as unsigned char* in HIDAPI does not work
-
Hi there!
I want to use as much Qt-Objects as possible to make sure my code will compile the same on all platforms I may encounter.
Now I am having problems to feed the hid_read function of signal11's HIDAPI with a QByteArray.
The following works fine:
@
unsigned char buffer_in[MAX_BUFFER_IN];
// How many Bytes were read? (-1: no Device, 0: nothing to read)
num_bytes = hid_read(device, buffer_in, MAX_BUFFER_IN);
if (num_bytes > 0) {
// This correctly returns the first byte of the package
qDebug() << QString::number(buffer_in[0],16);
}
@This however does not work:
@
QByteArray buffer_in;
buffer_in.resize(MAX_BUFFER_IN+1);
buffer_in.clear();
// How many Bytes were read? (-1: no Device, 0: nothing to read)
num_bytes = hid_read(device, reinterpret_cast<unsigned char*>(buffer_in.data()), MAX_BUFFER_IN);
if (num_bytes > 0) {
// This is always 0
qDebug() << QString::number(buffer_in.size(),10);
// This is always 0
qDebug() << QString::number(buffer_in[0],16);
}
@So hid_read(hid_device *device, unsigned char *data, size_t length); seems not to be able to write into my QByteArray... but why?
Do you have an idea?
Kind regards,
schmaunz -
Hi,
The QByteArray should first have a size and must be '/0' terminated. When you create a QByteArray the array size is 1 (only a '/0'). So first set the size with resize function to MAX_BUFFER_IN and then call the hidread to fill with proper data. It might be even better (if possible) to ask the hid to how many bytes there are available.
Happy coding! -
Whow, thanks for the super fast answer :)
My fault was indeed to call "buffer_in.clear();" before hid_read.
But without clear(); the Array is filled with random values... so I guess
@
buffer_in.resize(MAX_BUFFER_IN);
buffer_in.fill(0x00);
@Would be the correct way to empty a QByteArray?
-
if you need initialized values you could use fill(0x00, MAX_BUFFER_IN), but the 0x00 will be seen as a '/0' character, so the size will be set to 0 internally for the QByteArray IYAM. Better to set to character 0?
-
So, to make it easy:
@
// Setup your QByteArray to hold more then enough databytes
BufferIn.resize(MAX_BUFFER_IN + 1); // The +1 to avoid a crash when MAX_BUFFER_IN data bytes are written and the '\0' character is overwritten.
// Fill Buffer with data
NumberOfBytes = hid_read(device, static_cast<unsigned char*>(BufferIn.data()), MAX_BUFFER_IN);
// Why did you choose to use the reinterpret_cast?if (NumberOfBytes > 0)
{
// Chop away the stuff you don't use
BufferIn.resize(NumberOfBytes);
// Now we have a '\0' terminated bytearray
qDebug() << QString::number(BufferIn));
}
@ -
Thanks for your effort!
My device is generating binary data and encodes it with a start/stop byte, a control byte and the actual payload in between.
The start byte can never be misinterpreted as a control byte, because it makes no sense in that regard.
The payload however may and will contain everything from 0x00 to 0xFF.In other words zero termination is not an option here.
I always read the entire endpoint and parse the data myself.On second thought, you are right.
I really don't need an initialized array, because I overwrite it anyways, but I would love an array that responds to .isEmpty(); if there was nothing to do... I might have to give that some more thought ;)[quote author="Jeroentje@home" date="1402492423"]
Why did you choose to use the reinterpret_cast?
[/quote]I am using a reinterpret_cast, because static_cast throws:
Error: invalid static_cast from type 'char*' to type 'unsigned char*'I haven't found another way around that.
Since my parser works with the actual bits of the data, their type doesn't matter just yet.And as far as I understand QByteArray is sort of typeless, isn't it?
If you know a better solution, please let me know!