2

I'm reading a file from serialport using x-modem protocol and 133 bytes packet. I'm reading in that

      1 byte is SOH
      2 byte packet number
      3 byte nagative of packet number
next  128 bytes data
      2 bytes CRC sent from other side.

I have to calculate CRC of 128 bytes data and 2 bytes crc sent from other side that I have to make it single byte and have to comapare with my calculated crc. How can I do this in java?

John
  • 15,418
  • 12
  • 44
  • 65
  • Ok, what course is setting assignments about reading serial ports in Java? http://stackoverflow.com/questions/348777/reading-com-port-value-and-printing-in-textarea-which-located-inside-the-panel-in – The Archetypal Paul Jun 15 '11 at 16:41

3 Answers3

5

Try using Jacksum.

Craig McQueen
  • 41,871
  • 30
  • 130
  • 181
sangupta
  • 2,396
  • 3
  • 23
  • 37
1

Sun JDK 1.6 contains sun.misc.CRC16, but there is a possibility this is not the CRC16 you're looking for, since there's several different polynomials in use.

krosenvold
  • 75,535
  • 32
  • 152
  • 208
  • The Sun class uses init=0, polynomial=0x1021. It also uses the naive bitwise implementation, not the table-driven implementation. – user207421 Mar 11 '11 at 06:51
1

Here is my C code, which is trivial to port to Java - you are free to use it in any way you like. The references to word are for a 16 bit unsigned value - you should be able to use a char instead in Java.

It's been too long since I worked with 16 bit CRC's so I don't recall if there are variations based on seeding. I am pretty sure I used this code in a C implementation of X-Modem way back when.

The source is posted on tech.dolhub.com.

Lawrence Dol
  • 63,018
  • 25
  • 139
  • 189