Domanda

we are sending some data over a serial line, and i can do pretty much everything via a bash script (instead of code), except for the crc16 calculation. if i can do it all in scripts versus code, it would make configuration a heckofalot easier (especially while in the field).

i'm alright with commands, but i lose all ability when we get to the tricky stuff.

so my question is, can someone do a rewrite of this CRC16 for me, but within bash?

here is the algorithm grabbed from wikipedia, and it is the one in our code:

uint16_t Encoder::checksum(std::string thestring)
{
    uint8_t d, e, f;
    uint16_t c, r, crccalc;
    c = 0xffff;

    for (unsigned int i = 0; i < thestring.length(); i++)
    {
        d = thestring[i];
        e = c ^ d;
        f = e ^ (e << 4);
        r = (c >> 8) ^ (f << 8) ^ (f << 3) ^ (f >> 4);
        c = r;
    }
    c ^= 0xffff;
    crccalc = c;
    return crccalc;
}

i can easily create an executable out of the C++ code, and just feed it stdin, but i think it would be really neat to be able to have this within the bash.

the other thing i don't know is how to ensure that my variable sizes are correct. how can i ensure that i am getting a 16 bit integer?

any help would be great. i found a little script online, but i didn't trust it. thought it would be really cool to have answered here.

È stato utile?

Soluzione

Bash have:

  • xor ($((5^2)) will be 7);
  • left shift ($(3<<2) will be 12);
  • right shift ($(8>>2) will be 2);
  • hexademical numbers support ($((0xFF)) will be 255).

Nothing comes to mind to convert from 32 (64) to 16 bit integer in pure Bash but you can do it with awk:

$ echo 65536 | awk '{printf("%hu\n",$1)}'
0

This should be enough to rewrite algorithm in Bash.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top