PDA

View Full Version : Problem sending data over to serial port



hovuquocan1997
27th July 2015, 18:58
Hello everyone, I'm having some problems sending my commands to the device through a serial port. So first, I create a Qlineedit that only takes integer, from 0 to 21, and then I create a pushButton with this code


void MainWindow::on_pushButton_2_clicked() //testing button
{
QString input = ui->lineEdit->text();
bool ok;
char data = input.toInt(&ok, 16);
char checksum;
char power[] = {0x01, 0x06, 0x08, data, checksum, 0x04};
char result = 0x00;
for (int i=0; i<4; i++) {
result += power[i];
};
checksum = result;
power[4]=checksum;
serial->write(power,sizeof(power));
}

so for example, if I put in "1" to the lineedit, it will generate a char array of 01, 06, 08, 01, 10, 04 and send it to the serial port. If I input "2", it's going to be 01, 06, 08, 02, 11, 04, and etc. The way I check if the data is sent correctly is that the device will send back an acknowledge line. I've tried this and successfully done it up to 15, then I don't know why but from 16 to 21, the data can't be sent correctly because the device doesn't send back anything. If it doesn't work at all then I know what to fix, but in this case, it works but only for 0-15, so I'm really confused. I will appreciate any help, thank you.

anda_skoa
27th July 2015, 21:30
You use 16 as the base for toInt().
That makes it parse the number as a hex value.
16 in hex is 1 * 16^1 + 6 * 16^0 = 1 * 16 + 6 * 1 = 16 + 6 = 22

Maybe 22 is out of range?

Cheers,
_

hovuquocan1997
27th July 2015, 21:49
Thanks for your reply, but the problem is likely not about the range, since QIntValidator only restraints the input and not the output. As you advised, I changed the range to 22 just in case, but still don't get it to work beyond 15, so I don't think that's the problem.

ChrisW67
27th July 2015, 22:45
What is the allowable range of values, in decimal, that your device will accept as fourth byte in the set of six? If the device only accepts values 0 through 21 decimal then your hexadecimal input is out of range. (It is also missing allowable values if you only allow the chars '0' to '9' in the input box)


0x00 == 0
0x01 == 1
...
0x09 == 9
0x10 == 16
...
0x15 == 21
0x16 == 22 <<< out of range
...
0x21 == 33 <<< all the way

hovuquocan1997
27th July 2015, 22:51
My device accepts everything from 0 to 255, which is 0xFF, what I mean is that I use QIntValidator to limit the input of users to 0-21 only, but the device is not limited.

jefftee
27th July 2015, 22:59
Are you entering integer values into the textedit? i.e. 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, 20,21? If so, you should be converting to integer using base 10, which is the default for QString::toInt. Your code above is treating your input as hex values, not integer.

For your code to work as shown, you'd need to be entering hex values: 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,10,11,12,13,14,15.

hovuquocan1997
28th July 2015, 18:49
Thank you very much! That was the case.