Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Sending hex characters to serial port

Sending hex characters to serial port

Scheduled Pinned Locked Moved General and Desktop
3 Posts 2 Posters 7.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abbassi
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • K Offline
      K Offline
      koahnig
      wrote on last edited by
      #2

      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.

      Vote the answer(s) that helped you to solve your issue(s)

      1 Reply Last reply
      0
      • A Offline
        A Offline
        abbassi
        wrote on last edited by
        #3

        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);@

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved