Sending hex characters to serial port
-
hello all,
I am a newbie in Qt and also not an expert in programming but I am trying to learn. I have tried my best to search for the problem before asking but still I have confusions so I thought I should ask on forum. Below is my question
I am sending a command to a device via serial port and then the device is responding and i am using that data so far so good. When i press the button I am sending the command directly to get device information I am sending the command like this;
@ const char buff[8]={0x01,0x80,0x04,0x00,0x2A,0x81,0x00,0x00};
port->write(buff,8);@The device is responding and I am receiving the data on serial port. Now I have a lineEdit where I can write the command myself also and i've used validator also so that the user can only write "hex" values.
I am doing like this;
@port->write(ui->sendEdit->text().toStdString().c_str());@
The problem is, this way ASCII values are being sent not hex. I can not find a way so that I get the same command in hex not ASCII as it was done previously without lineedit as i mentioned in the start.
In short, how can I do this (below) using lineedit, so that i write on line edit and it will be sent this way.
@ const char buff[8]={0x01,0x80,0x04,0x00,0x2A,0x81,0x00,0x00};
port->write(buff,8);@Please help me in this regard i would be thankful.
-
welcome to devnet
It looks like to need to use "QString::toInt":http://qt-project.org/doc/qt-4.8/qstring.html#toInt with base 16. The exact solution might depend of what your actual looks.
-
Thanks for the help. I have done it finally here is what i did;
@char outbuff[40];
int l;
QString strRcv = ui->sendEdit->text();
for (l=0; l<strRcv.length()/2; l++) {
QString tmp = strRcv.mid(l*2,2);
int val = tmp.toInt(&ok, 16);
outbuff[l] = val;
}
port->write(outbuff, l);@