MODBUS ASCII LRC Computation in C++

In C++, IoT by timfanelliLeave a Comment

I’m currently working on a project interfacing a BeagleBone Black to a drive control unit, which communicates using MODBUS ASCII over RS-232. The biggest challenge with the communication protocol is that MODBUS requires a Longitudinal Redundancy Check word as the last two characters of the command. Since MODBUS ASCII is, as the name would imply, an ASCII protocol, it encodes hexadecimal command codes as human-readable strings, rather than as binary values. This posed an initially-mindbending problem for calculating the LRC.

I cooked up the following routine, which does the job quite nicely… wanted to post incase others found it useful. I don’t claim it’s the most efficient, but it is certainly clean, and relatively easy to understand.

#include <sstream>
#include <iostream>

// command is hexadecimal number represented as a 
// string in the form: 0x__________. The number may 
// be of arbitrary length, but must contain an even-
// number of ascii characters (i.e., two characters 
// per byte)
std::string calculateLRC( std::string command ) {
        std::stringstream ss;
        int sum = 0;

        // Sum each byte
        for ( int i = 2; i < command.size(); i += 2 )
                sum += std::stoul( command.substr(i,2), 0, 16 );

        // Convert 2's compliment inversion to a string
        ss << std::hex << (~sum+0x01);

        return ss.str().substr( ss.str().size()-2, 2 );

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.