Results 1 to 7 of 7

Thread: HEX addition, subtraction LRC ASCII

  1. #1
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows Android

    Default HEX addition, subtraction LRC ASCII

    Trying to do ASCII LRC calculation, so I have hex values stored in a QByteArray, something in the lines of 01050500FF00, I need to add these values together, then subtract the result from FF, and add 1 to get the LRC..

    in short :
    1- Step1 = Sum Hex bytes of given QByteArray
    2- Step2 = "FF" - Step1 (take the right part)
    3- Step3 = Step2 + 1

    Example :

    1 - "01050500FF00", LRC should be F6
    0+1+0+5+0+5+0+0+F+F+0+0 = 010A
    FF - 0A = F5
    F5 +1 = F6

    2 - "010505000000", LRC should be F5
    ie 0+1+0+5+0+5+0+0+0+0+0+0 = 0B
    FF - 0B = F4
    F4 + 1 = F5

    My question is there an easy way to do Hex addition and subtraction in QT? if so how? or do I convert to decimal... case 'A' : a[i] = 11;break;... and so on..

    tnx for any help..

    p.s. C++
    Last edited by phakorr; 28th April 2014 at 11:48. Reason: added p.s. to tell that this is for C++

  2. #2
    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: HEX addition, subtraction LRC ASCII

    Did You have in QByteArray characters ('0'..'9', 'A'..'F') or bytes (0..0xf) ? This is not the same.

  3. #3
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: HEX addition, subtraction LRC ASCII

    Supposed to have bytes..

    The QByteArray crcWord is formed as such.. where ;

    ui->lineEdit->text() is '01' (slave id)
    functioncode is '05' (modbus function code)
    ui->lineEdit_2->text() is '0500' (Register address hex)
    ui->lineEdit_3->text() is '0000' (or 'FF00' from the examples I gave)
    (data)


    Qt Code:
    1. QByteArray crcWord = QString(ui->lineEdit->text() + functionCode + ui->lineEdit_2->text() + ui->lineEdit_3->text()).toLatin1();
    To copy to clipboard, switch view to plain text mode 

    I later write to the port, it all works when I manually add the LRC manually (i.e. replace the crc.toUpper() with "F5" (or whatever the LRC value of the word is) )

    Qt Code:
    1. ..
    2.  
    3. QString sendWord = ":" + crcWord + crc.toUpper() + "\r\n";
    4.  
    5. QByteArray sndwrd = sendWord.toLatin1();
    6. const char *cstr = sndwrd.data();
    7.  
    8. serial->write(cstr);
    9. serial->close();
    To copy to clipboard, switch view to plain text mode 
    Last edited by phakorr; 28th April 2014 at 17:54. Reason: added info on values..

  4. #4
    Join Date
    Oct 2013
    Posts
    41
    Thanks
    1
    Thanked 8 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: HEX addition, subtraction LRC ASCII

    Is this what you are trying to implement?

    http://en.wikipedia.org/wiki/Longitu...dundancy_check

    If so, you'll need to take your byte array which contains a string, and pull 2 characters out of it at a time to convert them to bytes. Then do your calculations on the bytes.

  5. #5
    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: HEX addition, subtraction LRC ASCII

    So in QByteArray You have ASCII characters (string) not binary data. You can convert them to binary data with QByteArray::fromHex method and then simply add byte by byte.

  6. #6
    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: HEX addition, subtraction LRC ASCII

    Your description of the process is wrong but you have given the correct result in both cases.
    Qt Code:
    1. 1 - "01050500FF00", LRC should be F6
    2. 0+1+0+5+0+5+0+0+F+F+0+0 = 010A // wrong working, right answer
    3. 0x01 + 0x05 + 0x05 + 0x00 + 0xFF + 0x00 = 0x010A // this is the correct working
    4. FF - 0A = F5
    5. F5 +1 = F6
    To copy to clipboard, switch view to plain text mode 

    Steps 2 and 3 are a description of negating a number in Two's-complement. Your computer will do this for you. Here is your example with the LRC appended to the input:
    Qt Code:
    1. QByteArray input("01050500FF00");
    2.  
    3. // Convert to binary
    4. QByteArray work = QByteArray::fromHex(input);
    5. // Step 1, sum bytes (relies on char losing any overflow)
    6. char sum = 0;
    7. foreach (char c, work)
    8. sum += c;
    9. // steps 2 and 3: negate result
    10. sum = -sum;
    11.  
    12. qDebug() << "Input =" << work.toHex().toUpper();
    13. work.append(sum);
    14. qDebug() << "With LRC =" << work.toHex().toUpper();
    To copy to clipboard, switch view to plain text mode 

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

    phakorr (29th April 2014)

  8. #7
    Join Date
    Apr 2014
    Posts
    3
    Thanks
    1
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: HEX addition, subtraction LRC ASCII

    Quote Originally Posted by ChrisW67 View Post
    Your description of the process is wrong but you have given the correct result in both cases.
    Qt Code:
    1. ..
    2. 0+1+0+5+0+5+0+0+F+F+0+0 = 010A // wrong working, right answer
    3. 0x01 + 0x05 + 0x05 + 0x00 + 0xFF + 0x00 = 0x010A // this is the correct working
    4. ...
    To copy to clipboard, switch view to plain text mode 

    Steps 2 and 3 are a description of negating a number in Two's-complement. Your computer will do this for you. Here is your example with the LRC appended to the input:
    ....
    Thanks for the reply, all is working now. I wrote the calculation wrong partially on purpose as oddly enough it still calculates the LRC correctly for all the samples that I tried it on..

Similar Threads

  1. socketnotifier addition to qapplication.
    By vijay523201 in forum Qt for Embedded and Mobile
    Replies: 1
    Last Post: 21st July 2013, 12:17
  2. Hex to Ascii and Ascii to Hex Problem
    By havoc in forum Newbie
    Replies: 6
    Last Post: 21st July 2013, 00:24
  3. QGraphicsScene and dynamic addition of QGraphicsItems
    By El Bazza in forum Qt Programming
    Replies: 7
    Last Post: 15th September 2012, 08:57
  4. vertical scrollbar subtraction from the screen width
    By sujan.dasmahapatra in forum Qt Programming
    Replies: 0
    Last Post: 16th February 2011, 10:16
  5. Addition of images
    By merry in forum Qt Programming
    Replies: 1
    Last Post: 6th February 2007, 13:38

Tags for this Thread

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.