Results 1 to 14 of 14

Thread: Reading Bytes from Serial

  1. #1
    Join Date
    Dec 2017
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Reading Bytes from Serial

    Is there a way to read a certain amount of bytes from the serial and type cast it into int or float? I've read through the QByteArray document, but I don't really get it.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Reading Bytes from Serial

    Why not simply use QByteArray::toFloat() or toInt()?

    I've read through the QByteArray document, but I don't really get it.
    What exactly didn't you get?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Dec 2017
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading Bytes from Serial

    I've tried using toFloat(), but kept on getting 0

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading Bytes from Serial

    You're going to have to show some code if you want help in finding what's wrong.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Dec 2017
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading Bytes from Serial

    i.e.

    QByteArray a("425AE78F");
    float serial = a.toFloat();
    qDebug() << serial;

    I'm getting 0 instead of 54.72613. I don't think I'm doing it correctly.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading Bytes from Serial

    This is example from QByteArray::toDouble doc :
    Qt Code:
    1. QByteArray string("1234.56");
    2. double a = string.toDouble(); // a == 1234.56
    To copy to clipboard, switch view to plain text mode 
    Do you see the difference ?

  7. #7
    Join Date
    Dec 2017
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading Bytes from Serial

    Are you saying that the string a is not a float that's why it fails to convert?

  8. #8
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading Bytes from Serial

    Of course. The number of float type and its text representation is two different things. You must go back to the basics of C ++.

  9. #9
    Join Date
    Dec 2017
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading Bytes from Serial

    As you can tell, I'm still learning c++. If you don't mind, could you give me an example of how does the float conversion works?

    "4h\xB2\xC9\xBC'\xB2\xC9<\xBC\xF9K;\xD1\xEB\x7 F?"

    The above is a chunk of my code, and I'm trying to convert it to float, but I couldn't get it to work. I really appreciate your help.

  10. #10
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading Bytes from Serial

    How is the data sent? The order of bytes is important.

  11. #11
    Join Date
    Dec 2017
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading Bytes from Serial

    Actually I'm reading the data from the serial. I have 9 values print out in arduino as double, so I want to read it 36 bytes at a time and convert it to float.

  12. #12
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading Bytes from Serial

    What does it mean "print out in arduino as double" ? You must do the opposite operation to "print out".

  13. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading Bytes from Serial

    "4h\xB2\xC9\xBC'\xB2\xC9<\xBC\xF9K;\xD1\xEB\x7 F?"
    Are these the actual bytes you get from the arduino, or have you converted them to a string? This could almost be an array of hexadecimal numbers except for all of the "punctuation / non-hex" characters in it.

    QByteArray a("425AE78F");
    And this is a hexadecimal number, represented as a string literal. You can't use the toDouble() or toFloat() conversions on such as string, because it doesn't represent the string form of a floating point number (ie. "1234.567").

    You can convert this to a long or a long long using something like this:

    Qt Code:
    1. QByteArray a("425AE78F");
    2. long b = a.toLong( 0, 16 );
    To copy to clipboard, switch view to plain text mode 
    Last edited by d_stranz; 20th December 2017 at 18:48.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  14. #14
    Join Date
    Dec 2017
    Posts
    12
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Reading Bytes from Serial

    [QUOTE=d_stranz;301620]Are these the actual bytes you get from the arduino, or have you converted them to a string? This could almost be an array of hexadecimal numbers except for all of the "punctuation / non-hex" characters in it.

    Yes, they are the actual bytes. This is kind of unrelated with the topic, but if I want to send a character 'a' to the serial, can I use serial -> write("a")?

Similar Threads

  1. Replies: 3
    Last Post: 19th November 2016, 21:06
  2. qserialport and reading bytes
    By gab74 in forum Newbie
    Replies: 3
    Last Post: 14th February 2014, 21:11
  3. Replies: 4
    Last Post: 23rd October 2012, 09:40
  4. reading bytes out of a file
    By priceey in forum Qt Programming
    Replies: 7
    Last Post: 6th October 2009, 17:55
  5. How to write bytes read from serial port to a QFile
    By shamik in forum Qt Programming
    Replies: 19
    Last Post: 25th June 2007, 15:12

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.