Results 1 to 13 of 13

Thread: Get hex from Qlineedit and send to serial port

  1. #1
    Join Date
    Jul 2015
    Posts
    18
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Get hex from Qlineedit and send to serial port

    Hello everyone, my question is how to get hex pairs from Qlineedit and send them to serial port
    I know how to use Qvalidator to restrain users from inputting anything else besides, but I don't know how to get those values and set them as chars to send to serial port. For example, the user will input 01 02 03 04 05, and through a pushButton, I want my program to get those values in pair, then create a char array that's basically like this
    Qt Code:
    1. {0x01, 0x02, 0x03, 0x04, 0x05}
    To copy to clipboard, switch view to plain text mode 
    so that I can send it to the serial port. I will appreciate any help, thank you

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get hex from Qlineedit and send to serial port

    QString::toInt() using 16 as the base.
    Then taking the lowest byte of the int.

    Cheers,
    _

  3. The following user says thank you to anda_skoa for this useful post:

    hovuquocan1997 (29th July 2015)

  4. #3
    Join Date
    Jul 2015
    Posts
    18
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get hex from Qlineedit and send to serial port

    Thank you for the reply, here is what I have so far and it's still not working,
    Qt Code:
    1. void MainWindow::on_pushButton_3_clicked() //testing button
    2. {
    3. ui->lineEdit_2->setInputMask("hhhhhhhhhhhhhh");
    4. QString input = ui->lineEdit_2->text();
    5. bool ok;
    6. char command=input.toInt(&ok,16);
    7. serial->write(command,sizeof(command));
    8. }
    To copy to clipboard, switch view to plain text mode 

    and also is there a method to input as many as hex pairs as I want? Since setinputmask restricts my input to only that many that I set. Thank you.

  5. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Get hex from Qlineedit and send to serial port

    Well, if you line edit has multiple tuples, you need to first separate them before calling toInt().
    See QString::mid().

    Instead of the mask you could try a validator, maybe a QRegularExpressionValidator or a custom one.

    Cheers,
    _

  6. The following user says thank you to anda_skoa for this useful post:

    hovuquocan1997 (29th July 2015)

  7. #5
    Join Date
    Jul 2015
    Posts
    18
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get hex from Qlineedit and send to serial port

    Can you please provide an example of QRegularExpressionValidator? I don't know how to use it even after reading the class page.

  8. #6
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Get hex from Qlineedit and send to serial port

    There are a ton of examples on the class page itself. You use it like any of the other validator classes but in the case of QRegularExpressionValidator, it uses a QRegularExpression to validate the input.

    Here's a quick example of a QRegularExpression that accepts a list of one or two digit hex values (0-9 or A-F) separated by a comma:

    Qt Code:
    1. QRegularExpression regex("^(?:(?:[0-9A-F]{1,2})(?:,[0-9A-F]{1,2})*)$");
    To copy to clipboard, switch view to plain text mode 

    Use that to validate your QTextInput, then split into individual values using QString::split(","), etc. The regex explanation is:


    ^ assert position at start of a line
    (?:(?:[0-9A-F]{1,2})(?:,[0-9A-F]{1,2})*) Non-capturing group
    (?:[0-9A-F]{1,2}) Non-capturing group
    [0-9A-F]{1,2} match a single character present in the list below
    Quantifier: {1,2} Between 1 and 2 times, as many times as possible, giving back as needed [greedy]
    0-9 a single character in the range between 0 and 9
    A-F a single character in the range between A and F (case sensitive)
    (?:,[0-9A-F]{1,2})* Non-capturing group
    Quantifier: * Between zero and unlimited times, as many times as possible, giving back as needed [greedy]
    , matches the character , literally
    [0-9A-F]{1,2} match a single character present in the list below
    Quantifier: {1,2} Between 1 and 2 times, as many times as possible, giving back as needed [greedy]
    0-9 a single character in the range between 0 and 9
    A-F a single character in the range between A and F (case sensitive)
    $ assert position at end of a line
    g modifier: global. All matches (don't return on first match)
    m modifier: multi-line. Causes ^ and $ to match the begin/end of each line (not only begin/end of string)
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  9. The following user says thank you to jefftee for this useful post:

    hovuquocan1997 (29th July 2015)

  10. #7
    Join Date
    Jul 2015
    Posts
    18
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get hex from Qlineedit and send to serial port

    Thank you for your help, but after I split them into a list, how do I access each element in the list and convert them to char values?

  11. #8
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Get hex from Qlineedit and send to serial port

    You need to read the doc for QStringList, which is the type returned from QString::split.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  12. The following user says thank you to jefftee for this useful post:

    hovuquocan1997 (29th July 2015)

  13. #9
    Join Date
    Jul 2015
    Posts
    18
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get hex from Qlineedit and send to serial port

    So my lineedit is named "data", and the pushButton is named "sendData", here is the code I have:
    Qt Code:
    1. void MainWindow::on_sendData_clicked()
    2. {
    3. QString input = ui->data->text();
    4. QStringList list1 = input.split(",");
    5. int size = list1.size();
    6. char senddata[size];
    7. for (int i=0; i <= size; i++){
    8. QString ran = list1 [i];
    9. bool ok;
    10. senddata[i]=ran.toInt(&ok,16);
    11. }
    12. serial->write(senddata,sizeof(senddata));
    13. }
    To copy to clipboard, switch view to plain text mode 
    I thought it would work okay, but somehow I can't initialize the char array "senddata" without an actual size, do you have any fix for that? Thank you.

  14. #10
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Get hex from Qlineedit and send to serial port

    Quote Originally Posted by hovuquocan1997 View Post
    I thought it would work okay, but somehow I can't initialize the char array "senddata" without an actual size, do you have any fix for that? Thank you.
    Your for loop should test i < size, not <=. As written, you're looping one too many times, exceeding the size of both the QStringList list1 and the char array senddata.

    Once you fix the for loop, you should make it easy on yourself and use a QByteArray for senddata. It allows a variable length array of bytes, access to individual bytes using array syntax, and you can access the data itself using QByteArray::constData and the size of the data with QByteArray::length.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  15. The following user says thank you to jefftee for this useful post:

    hovuquocan1997 (29th July 2015)

  16. #11
    Join Date
    Jul 2015
    Posts
    18
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get hex from Qlineedit and send to serial port

    I've tried QByteArray also, but the same problem persists, the problem is not about the type but about the size of the array. Two of the errors are "senddata: unknown size" and "cannot allocate an array of constant size 0" on this line
    Qt Code:
    1. char senddata[size];
    To copy to clipboard, switch view to plain text mode 
    and even when I switched to QByteArray, the same errors pop up.

  17. #12
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: Get hex from Qlineedit and send to serial port

    You don't need to specify the size of the byte array if you use QByteArray, that was my whole point. Here is your code using QByteArray and proper for loop bounds:

    Qt Code:
    1. QString input = ui->data->text();
    2. QStringList list1 = input.split(",");
    3. int size = list1.size();
    4. QByteArray senddata;
    5. for (int i=0; i < size; i++){
    6. QString ran = list1 [i];
    7. bool ok;
    8. senddata[i]=ran.toInt(&ok,16);
    9. }
    10.  
    11. serial->write(senddata.constData(), senddata.length());
    To copy to clipboard, switch view to plain text mode 
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  18. The following user says thank you to jefftee for this useful post:

    hovuquocan1997 (29th July 2015)

  19. #13
    Join Date
    Jul 2015
    Posts
    18
    Thanks
    11
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Get hex from Qlineedit and send to serial port

    Thank you very much, that solved everything! Thanks for helping me so much, I really appreciate it!

Similar Threads

  1. Convert int to hex and add to char array to send to serial port
    By hovuquocan1997 in forum Qt Programming
    Replies: 2
    Last Post: 15th July 2015, 17:13
  2. Serial read misses to read data from the serial port
    By mania in forum Qt for Embedded and Mobile
    Replies: 11
    Last Post: 18th August 2014, 08:49
  3. Replies: 1
    Last Post: 13th March 2013, 08:44
  4. Replies: 5
    Last Post: 27th May 2011, 09:38
  5. send binary file over serial port with QSerialDevice
    By ilpaso in forum Qt Programming
    Replies: 30
    Last Post: 13th December 2010, 12:08

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.