Reputation: 301
I am using
QMap < int, QByteArray> RegTable; (Seprate Class Variable)
to storing Modbus Register Address and data received from Serial Port. Everything working fine, data read from the serial port and fill to the QByteArray correctly.
qDebug() << Modbus->RegTable[RegAddr].toHex(' ');
print the correct data. But if want to write variable using it then always the same value/Garbage stored in this variable.
unsigned int unixTimeStamp = 0;
qDebug() << Modbus->RegTable[RegAddr].toHex(' ');
unixTimeStamp = static_cast <unsigned int>Modbus->RegTable[RegAddr].at(4);
unixTimeStamp |= static_cast <unsigned int>Modbus->RegTable[RegAddr].at(5)<<8;
unixTimeStamp |= static_cast <unsigned int>Modbus->RegTable[RegAddr].at(6)<<16;
unixTimeStamp |= static_cast <unsigned int>Modbus->RegTable[RegAddr].at(7)<<24;
QDateTime timestamp;
timestamp.setTime_t(unixTimeStamp );
qDebug() << timestamp.toString(Qt::SystemLocaleShortDate); //wrong time
RegTable is Modbus Class variable, And I used it to the main class but always I get the wrong data or values.
Upvotes: 1
Views: 317
Reputation: 409442
With
unixTimeStamp |= ...;
you write to the same byte over and over again. You need to shift the values:
unixTimeStamp = static_cast <unsigned int>Modbus->RegTable[RegAddr].at(4) << 24;
unixTimeStamp |= static_cast <unsigned int>Modbus->RegTable[RegAddr].at(5) << 16;
unixTimeStamp |= static_cast <unsigned int>Modbus->RegTable[RegAddr].at(6) << 8;
unixTimeStamp |= static_cast <unsigned int>Modbus->RegTable[RegAddr].at(7);
Of course, the shifts might have to be done in the opposite order depending on byte order.
Upvotes: 1