crc

Top  Previous  Next

Girder has a set of CRC function built in. These include: crc16, crc32, crcCCITT, crcDNP, crcKermit and crcSick. All are nested in the math table and have the same functions: init, update, finish and process.

Definitions for string input

crc = math.crc16.process( value )

crc = math.crc32.process( value )

crc = math.crcCCITT.process( value )

crc = math.crcDNP.process( value )

crc = math.crcKermit.process( value )

crc = math.crcSick.process( value )

 

Name

Type

Description

crc

number

CRC value

value

string

String to CRC

 

Definitions for incremental input

crcTemp = math.crc16.init()

crcTemp = math.crc32.init()

crcTemp = math.crcCCITT.init()

crcTemp = math.crcDNP.init()

crcTemp = math.crcKermit.init()

crcTemp = math.crcSick.init()

 

Name

Type

Description

crcTemp

number

CRC temporary calculation value

 

 

crcTemp = math.crc16.update( previousCrcTemp, byte )

crcTemp = math.crc32.update( previousCrcTemp, byte )

crcTemp = math.crcCCITT.update( previousCrcTemp, byte )

crcTemp = math.crcDNP.update( previousCrcTemp, byte )

crcTemp = math.crcKermit.update( previousCrcTemp, byte )

crcTemp = math.crcSick.update( previousCrcTemp, byte, previousByte )

 

Name

Type

Description

crcTemp

number

CRC temporary calculation value

previousCrcTemp

number

CRC temporary calculation value of previous cycle (or value from init call )

byte

number

Byte value (0-255) of value to add to calculation

previousByte

number

Byte value of previous cycle.

 

crc = math.crc16.finish( previousCrcTemp )

crc = math.crc32.finish( previousCrcTemp )

crc = math.crcCCITT.finish( previousCrcTemp )

crc = math.crcDNP.finish( previousCrcTemp )

crc = math.crcKermit.finish( previousCrcTemp )

crc = math.crcSick.finish( previousCrcTemp )

 

Name

Type

Description

crcTemp

number

CRC value

previousCrcTemp

number

CRC temporary calculation value of previous cycle (or value from init call )

Examples

Here is an example of how to use the incremental input CRC functions.

 

function process_crc_sick ( s, init )

 if not init then

         init = math.crcSick.init()

 end

 local last = 0

 for i=1, string.len(s) do

         local b = string.byte(s,i)

         init = math.crcSick.update(init,b,last)

         last = b

 end

 return math.crcSick.finish(init)

end

print( process_crc_sick( "Hello World" ) )

 

or an example to directly calculate the CRC:

 

print( math.crc32.process( "Hello World" ) )