Results 1 to 7 of 7

Thread: Problem sending data over to serial port

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

    Default Problem sending data over to serial port

    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
    Qt Code:
    1. void MainWindow::on_pushButton_2_clicked() //testing button
    2. {
    3. QString input = ui->lineEdit->text();
    4. bool ok;
    5. char data = input.toInt(&ok, 16);
    6. char checksum;
    7. char power[] = {0x01, 0x06, 0x08, data, checksum, 0x04};
    8. char result = 0x00;
    9. for (int i=0; i<4; i++) {
    10. result += power[i];
    11. };
    12. checksum = result;
    13. power[4]=checksum;
    14. serial->write(power,sizeof(power));
    15. }
    To copy to clipboard, switch view to plain text mode 
    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.

  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: Problem sending data over to serial port

    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,
    _

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

    hovuquocan1997 (28th July 2015)

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

    Default Re: Problem sending data over to serial port

    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.

  5. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Problem sending data over to serial port

    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)
    Qt Code:
    1. 0x00 == 0
    2. 0x01 == 1
    3. ...
    4. 0x09 == 9
    5. 0x10 == 16
    6. ...
    7. 0x15 == 21
    8. 0x16 == 22 <<< out of range
    9. ...
    10. 0x21 == 33 <<< all the way
    To copy to clipboard, switch view to plain text mode 

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

    hovuquocan1997 (28th July 2015)

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

    Default Re: Problem sending data over to serial port

    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.

  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: Problem sending data over to serial port

    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.
    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 (28th July 2015)

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

    Default Re: Problem sending data over to serial port

    Thank you very much! That was the case.

Similar Threads

  1. Sending serial data over ethernet
    By awpitt13 in forum Newbie
    Replies: 2
    Last Post: 27th February 2017, 13:06
  2. How to interrupt serial port sending?
    By redkit_redkit in forum Qt Programming
    Replies: 3
    Last Post: 9th April 2014, 12:58
  3. Replies: 2
    Last Post: 15th March 2014, 11:54
  4. serial port printer sending commands, how?
    By triperzonak in forum Qt Programming
    Replies: 3
    Last Post: 6th April 2009, 14:51
  5. sending QImage over serial port
    By yagabey in forum Qt Programming
    Replies: 5
    Last Post: 16th January 2008, 21:38

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.