CRC16 implementation?

15 views
Skip to first unread message

vsurducan

unread,
Aug 6, 2024, 5:37:28 AM8/6/24
to jal...@googlegroups.com
Hi Rob,
Is there any CRC16 implementation example? DS18B20 library has a CRC8 implemented, but only for bit checking.
I need a CRC16 computation for reading a  MODBUS slave. The polynomial generator is X16+X12+X2+1 and the value for computation 0xA0001.

The C implementation for CRC16 MODBUS seems to be this one:
and an application library here:

The problem is that translating this C to jal seems very painful...
I can find each CRC value for each needed command by running a MODBUS software, but the CRC values will be different for each slave address and/or numbers of bytes to be read from one particular slave...which is inconvenient.

thank you,
Vasile

Rob CJ

unread,
Aug 6, 2024, 12:48:54 PM8/6/24
to jal...@googlegroups.com
Hi Vasile,

In the virtual_wire library I do the following CRC calculation. A bit different from yours.

-- Calculate new crc over given data. Formula is:
-- Xexp16 + Xexp12 + Xexp5 + 1
function _vw_calc_crc(word in crc, byte in data) return word is
var word data_2
  data = data ^ byte(crc & 0x00ff)
  data = data ^ (data << 4)
  data_2 = word(data)
  return (((data_2 << 8) | (crc >> 8)) ^ (data_2 >> 4) ^ (data_2 << 3))
end function

Kind regards,

Rob


Van: jal...@googlegroups.com <jal...@googlegroups.com> namens vsurducan <vsur...@gmail.com>
Verzonden: dinsdag 6 augustus 2024 11:37
Aan: jal...@googlegroups.com <jal...@googlegroups.com>
Onderwerp: [jallib] CRC16 implementation?
 
--
You received this message because you are subscribed to the Google Groups "jallib" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jallib+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jallib/CAM%2Bj4qvE3tUP%2BuKwiPNxNcWLpvi_4uR69fETm59z4cGFS_30zA%40mail.gmail.com.

vsurducan

unread,
Aug 7, 2024, 1:00:11 PM8/7/24
to jal...@googlegroups.com
Thank you, this is helpful. It would be even better if would be commented... :). 

Reply all
Reply to author
Forward
0 new messages