Serial CRC/checksum library functions

I have two serial interfaces one requiring checksum modulo 256 and the other CRC-16 (Modbus).
Are there any plans for common CRC l
ibrary functions?

Either way it’s a few lines of code.  When I write another serial example I’ll include a CRC, something like this for a CCITT CRC-16:


local crc;

function crcUpdate(byte)
{
int b;

crc = crc ^ (byte<<8);
for(local b=0; b<8; b++)
{
if(crc & 0x8000)
crc = (crc<<1) ^ 0x1021;
else
crc = (crc<<1);
}

crc = crc & 0xFFFF;
}

Untested!

Rob
Fen Consultants, UK

Rob thanks

Should have mentioned, when you reset the CRC it should be to 0xFFFF not zero.


Rob

btw, CRC functions are in the wishlist to be implemented in the squirrel API… and also MODBUS. Stay tuned.

Hugo, thanks will Mobus CRC support both ASCII and RTU versions on the protocol?

Planning just RTU at first. Did you have a preference?


RTU please.

Hugo,

I’m brand new to electric imp, but a longtime user of Modbus TCP/IP. Two quick followup questions:

  1. Hows the Modbus RTU implementation going?
  2. Is it possible to communicate with an electric imp via a protocol such as Modbus TCP/IP over the local network (as opposed to sending it through the Planner)?

I often work in situations where we have a local network, but no Internet access, so receiving data via the cloud wouldn’t work for us.

Thanks!

Using imp standalone
Without an internet connection the imps won’t be able to interact with external services or each other. (From the last post in the thread I linked)

Still on the wishlist; other API additions are more important right now but we’ll get to it…

I’ve started a Modbus implementation, translating an arduino library into squirrel. It’s not finished yet (still waiting on a RS-485 converter so most of the electric imp API calls are not implemented) but it does compile in squirrel and I think it is pretty close. I’ve put it on github at https://github.com/deldrid1/Electric-Imp-Modbus-Master. Feel free to fork and use it - and if you do work on it please send me a Pull Request!

For CRC calculation, here are two different methods I’ve written for Modbus: https://gist.github.com/3839697. One uses a lookup table as suggested by the Modbus Spec, and the other is calculated.