PDA

View Full Version : QLCDNumber strange behavior!



saman_artorious
27th October 2013, 09:48
Well, the value of QLcdNumber is set using the display() property. For the first time dealing with QLcdNumber shows a strange behavior.
Consider this example:


void MainWindow::dataFromUdp(QByteArray pack)
{
if(pack.at(0) == '!')
{
unsigned long x;

x = (unsigned char)pack[pack.length() -1] +
(unsigned char)pack[pack.length() -2] * 256 +
(unsigned char)pack[pack.length() -3] * 256 * 256 +
(unsigned char)pack[pack.length() -4] * 256 * 256 * 256;

//extract packet

if(x == crcCalculate(0, pack.length() - 4, pack))
{
//CRC is correct

if(pack[1] == 0x0d)
{
number = 0; // class member variable
char value[6];
sprintf(value,"%c%c%c%c%c", (char)pack[2],(char)pack[3],(char)pack[4],
(char)pack[5],(char)pack[6]);

//Now value = 00125

number = atoi(value)/1000.0;

//Now value is 0.125

ui->lcd_range->display(number);

qDebug() << number;

//Here number and qlcd are set to -3.6xx


it's strange!

saman_artorious
27th October 2013, 13:08
Well, the value of QLcdNumber is set using the display() property. For the first time dealing with QLcdNumber shows a strange behavior.
Consider this example:


void MainWindow::dataFromUdp(QByteArray pack)
{
if(pack.at(0) == '!')
{
unsigned long x;

x = (unsigned char)pack[pack.length() -1] +
(unsigned char)pack[pack.length() -2] * 256 +
(unsigned char)pack[pack.length() -3] * 256 * 256 +
(unsigned char)pack[pack.length() -4] * 256 * 256 * 256;

//extract packet

if(x == crcCalculate(0, pack.length() - 4, pack))
{
//CRC is correct

if(pack[1] == 0x0d)
{
number = 0; // class member variable
char value[6];
sprintf(value,"%c%c%c%c%c", (char)pack[2],(char)pack[3],(char)pack[4],
(char)pack[5],(char)pack[6]);

//Now value = 00125

number = atoi(value)/1000.0;

//Now value is 0.125

ui->lcd_range->display(number);

qDebug() << number;

//Here number and qlcd are set to -3.6xx


it's strange!

Found the bug, the server was unwillingly sending a second packet right after the first one. So, the second one overwrote the first without eye noticing it.

Cheers,