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;
crc = (crc<<1);

crc = crc & 0xFFFF;


Fen Consultants, UK

Rob thanks

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


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.


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.


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 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: One uses a lookup table as suggested by the Modbus Spec, and the other is calculated.