Momergil
3rd July 2014, 14:15
Hello!
I'm having some problem with correctly creating and interpreting a struct through a QDataStream "operation". The goal here is the convert a struct into a QByteArray, send by socket to another application which will convert the raw data to the struct again. For now I'm using a Qt-based emulator which converts the raw data into a QByteArray before converting it to the struct.
The struct is the following:
struct re8k_ics_settings_inout
{
unsigned char header;
struct re8k_ics_settings_inout_nominalvoltages
{
unsigned int A;
unsigned int B;
unsigned int C;
} nominalVoltages;
enum re8k_ice_nominal_frequency nominalFrequency;
};
The enum have only two optional numbers (0 or 1). Here is the code containing both the serialiation and the deserialization:
//
QByteArray serializedData;
QDataStream dataStream(&serializedData,QIODevice::WriteOnly);
dataStream.setVersion(QDataStream::Qt_4_8);
re8k_ics_settings_inout structTemp;
structTemp.header = RE8K_ICDEF_SETTINGS_INOUT;
structTemp.nominalVoltages.A = 400;
structTemp.nominalVoltages.B = 9999;
structTemp.nominalVoltages.C = 1;
structTemp.nominalFrequency = Frequency50Hz; //0
dataStream << structTemp.header
<< quint32(structTemp.nominalVoltages.A) << quint32(structTemp.nominalVoltages.B) << quint32(structTemp.nominalVoltages.C)
<< structTemp.nominalFrequency;
emit signalSendToSystem(serializedData);
qDebug() << "Sended:" << serializedData.toHex() << serializedData.size() << (serializedData.size() == RE8K_ICDEF_SETTINGS_INOUT_SIZE) << RE8K_ICDEF_SETTINGS_INOUT_SIZE
<< structTemp.nominalVoltages.A << structTemp.nominalVoltages.B << structTemp.nominalVoltages.C;
//
const QByteArray tempBA = serializedData.mid(0,RE8K_ICDEF_SETTINGS_INOUT_SIZ E);
const void* tempPointer = tempBA.constData();
const re8k_ics_settings_inout* const newStruct = static_cast< const re8k_ics_settings_inout* >(tempPointer);
qDebug() << "Received:" << tempBA.toHex() << tempBA.size() << (tempBA.toHex() == serializedData.toHex()) << newStruct->nominalVoltages.A << newStruct->nominalVoltages.B << newStruct->nominalVoltages.C;
//
re8k_ics_settings_inout newStruct2;
QDataStream deserialize(tempBA);
deserialize.setVersion(QDataStream::Qt_4_8);
deserialize >> newStruct2.header
>> newStruct2.nominalVoltages.A
>> newStruct2.nominalVoltages.B
>> newStruct2.nominalVoltages.C;
qDebug() << "Received 2:" << newStruct2.nominalVoltages.A << newStruct2.nominalVoltages.B << newStruct2.nominalVoltages.C;
And here is the result:
Sended: "0e000001900000270f0000000100000000" 17 false 20 400 9999 1
Received: "0e000001900000270f0000000100000000" 17 true 654311568 15 1
Received 2: 400 9999 1
So as you can see, convert the "received" QByteArray into the struct with QDataStream is actually working (400 9999 1), but if I try the way I'll have to use (using a reinterpret_cast or, in this case, some static_cast for more safety), I get some bizarre errors. Not only this, but the RE8K_ICDEF_SETTINGS_INOUT_SIZE (which is sizeof(re8k_ics_settings_inout)), returns 20 while the QByteArray produced by the QDataStream has a size of 17. So my interpretation here is that is the QDataStream that is doing the mess. And to add a extra comment, the system works fine if the biggest number I put in nominalVoltages substruct is 255 (but still with the different size problem).
How may I solve this problem?
Thanks,
Momergil
I'm having some problem with correctly creating and interpreting a struct through a QDataStream "operation". The goal here is the convert a struct into a QByteArray, send by socket to another application which will convert the raw data to the struct again. For now I'm using a Qt-based emulator which converts the raw data into a QByteArray before converting it to the struct.
The struct is the following:
struct re8k_ics_settings_inout
{
unsigned char header;
struct re8k_ics_settings_inout_nominalvoltages
{
unsigned int A;
unsigned int B;
unsigned int C;
} nominalVoltages;
enum re8k_ice_nominal_frequency nominalFrequency;
};
The enum have only two optional numbers (0 or 1). Here is the code containing both the serialiation and the deserialization:
//
QByteArray serializedData;
QDataStream dataStream(&serializedData,QIODevice::WriteOnly);
dataStream.setVersion(QDataStream::Qt_4_8);
re8k_ics_settings_inout structTemp;
structTemp.header = RE8K_ICDEF_SETTINGS_INOUT;
structTemp.nominalVoltages.A = 400;
structTemp.nominalVoltages.B = 9999;
structTemp.nominalVoltages.C = 1;
structTemp.nominalFrequency = Frequency50Hz; //0
dataStream << structTemp.header
<< quint32(structTemp.nominalVoltages.A) << quint32(structTemp.nominalVoltages.B) << quint32(structTemp.nominalVoltages.C)
<< structTemp.nominalFrequency;
emit signalSendToSystem(serializedData);
qDebug() << "Sended:" << serializedData.toHex() << serializedData.size() << (serializedData.size() == RE8K_ICDEF_SETTINGS_INOUT_SIZE) << RE8K_ICDEF_SETTINGS_INOUT_SIZE
<< structTemp.nominalVoltages.A << structTemp.nominalVoltages.B << structTemp.nominalVoltages.C;
//
const QByteArray tempBA = serializedData.mid(0,RE8K_ICDEF_SETTINGS_INOUT_SIZ E);
const void* tempPointer = tempBA.constData();
const re8k_ics_settings_inout* const newStruct = static_cast< const re8k_ics_settings_inout* >(tempPointer);
qDebug() << "Received:" << tempBA.toHex() << tempBA.size() << (tempBA.toHex() == serializedData.toHex()) << newStruct->nominalVoltages.A << newStruct->nominalVoltages.B << newStruct->nominalVoltages.C;
//
re8k_ics_settings_inout newStruct2;
QDataStream deserialize(tempBA);
deserialize.setVersion(QDataStream::Qt_4_8);
deserialize >> newStruct2.header
>> newStruct2.nominalVoltages.A
>> newStruct2.nominalVoltages.B
>> newStruct2.nominalVoltages.C;
qDebug() << "Received 2:" << newStruct2.nominalVoltages.A << newStruct2.nominalVoltages.B << newStruct2.nominalVoltages.C;
And here is the result:
Sended: "0e000001900000270f0000000100000000" 17 false 20 400 9999 1
Received: "0e000001900000270f0000000100000000" 17 true 654311568 15 1
Received 2: 400 9999 1
So as you can see, convert the "received" QByteArray into the struct with QDataStream is actually working (400 9999 1), but if I try the way I'll have to use (using a reinterpret_cast or, in this case, some static_cast for more safety), I get some bizarre errors. Not only this, but the RE8K_ICDEF_SETTINGS_INOUT_SIZE (which is sizeof(re8k_ics_settings_inout)), returns 20 while the QByteArray produced by the QDataStream has a size of 17. So my interpretation here is that is the QDataStream that is doing the mess. And to add a extra comment, the system works fine if the biggest number I put in nominalVoltages substruct is 255 (but still with the different size problem).
How may I solve this problem?
Thanks,
Momergil