CRC "Cyclic Redundancy Check"

Is it possible to generate a CRC (cyclic redundancy check) in ignition ?

If it's possible in Python 2.7 then it's possible in Ignition.
A web search shows that it is possible in Python 2.7. Try this (but I haven't):

1 Like

It's built into Java: java.util.zip.CRC32

2 Likes

Hi, Phil.
Could you spare a few lines on how to set this up? I've never done it.

I have recipes in a table and I'd like to provide a CRC of the recipe parameters (for each recipe) so that the recipe can be checked by a single number rather than a parameter by parameter cross-check. The recipes are returned from the database in a (typically) 8 x 14 dataset.

Thanks.

So, simple usage would look like this:

from java.util.zip import CRC32

def crcOfBytes(bytes):
	crc = CRC32()
	crc.update(bytes)
	return crc.value

More complex cases just need to call .update() more, and perform conversions of your actual object types to individual bytes, byte arrays, or byte buffers. Whatever method you choose, your consistency in ordering of operations will be critical to repeatable CRC values.

That was beautifully simple, thanks, Phil, and saved me a lot of time.

For anyone else, I want the CRC to be easily human readable so I've converted the output to hex with a space between the first four and second four characters.

def crc(data):
	from java.util.zip import CRC32
	
	def crcOfBytes(bytes):
		crc = CRC32()
		crc.update(bytes)
		return crc.value
		
	dataBytes = bytes(data)
	crc = "0x{:08x}".format(crcOfBytes(dataBytes))	# Force leading zeros.
	return crc[2:6] + " " + crc[6:10]				# return format "1a2b 3c4d".

Ewww! Why did you nest the defs? Just make a library script that does the whole thing the way you want.

Except for simple closures for callbacks, def doesn't belong in events or transforms. And import not at all.

If you're on 8.1.33 (or higher), I'd also suggest pulling in HexFormat (in another project library constant, naturally :slight_smile:) to do the actual hex output.

You're right. I wasn't thinking straight at all. How's this:

from java.util.zip import CRC32
def crc(data):
	crc32 = CRC32()
	crc32.update(bytes(data))
	
	checksum = "0x{:08x}".format(crc32.value)		# Force leading zeros.
	return checksum[2:6] + " " + checksum[6:10]		# return format "1a2b 3c4d".
2 Likes

Better. But there's no reason for the import to be inside the function. Put it outside.

Done!
Thanks for the education. I'm a chancer at Python.

1 Like