N
<p>The following crc byte-generating code was produced by another student who is no longer around for me to discuss with.
<pre>
public class CCITTcrc
{
public static final int CRCINIT = 0xffff;
public static final int CRC_OK = 0;
public static final int CRC_FAULT = -1;
static final String HEXCHARS = "0123456789ABCDEF";
static final int [] CRCTAB_CCITT =
{
//sample of 16-crc lookup table
0x0000,0xC0C1,0xC181,0x0140,0xC301,0x03C0,0x0280,0xC241,0xC601,0x06C0,0x0780,
0xC741,0x0500,0xC5C1,0xC481,0x0440,0xCC01,0x0CC0,0x0D80,0xCD41,0x0F00,0xCFC1,
//etc. ...
};
public int calc(byte[] buffer, int n) {
int i,x;
crcaccu=(short)CRCINIT;
for (i=0; i<n; i++) {
x=(byte)((crcaccu>>8)^buffer); //switched from >>
if (x<0)
x+=0x100;
crcaccu=(short)((crcaccu<<8)^CRCTAB_CCITT[x]); //switched from <<
}
buffer[n]=(byte)(crcaccu>>8);
buffer[n+1]=(byte)crcaccu;
return n+2;
}
}
</pre>
<p>This code is not giving me the correct crc bytes, and so I cannot establish communication from my PC master to the slave device... if you see anything that you recognize as being wrong, please let me know,
<p>Or if you have similar code that you know works, please share!
<p>Huge thanks,<br>
Nancy
<pre>
public class CCITTcrc
{
public static final int CRCINIT = 0xffff;
public static final int CRC_OK = 0;
public static final int CRC_FAULT = -1;
static final String HEXCHARS = "0123456789ABCDEF";
static final int [] CRCTAB_CCITT =
{
//sample of 16-crc lookup table
0x0000,0xC0C1,0xC181,0x0140,0xC301,0x03C0,0x0280,0xC241,0xC601,0x06C0,0x0780,
0xC741,0x0500,0xC5C1,0xC481,0x0440,0xCC01,0x0CC0,0x0D80,0xCD41,0x0F00,0xCFC1,
//etc. ...
};
public int calc(byte[] buffer, int n) {
int i,x;
crcaccu=(short)CRCINIT;
for (i=0; i<n; i++) {
x=(byte)((crcaccu>>8)^buffer); //switched from >>
if (x<0)
x+=0x100;
crcaccu=(short)((crcaccu<<8)^CRCTAB_CCITT[x]); //switched from <<
}
buffer[n]=(byte)(crcaccu>>8);
buffer[n+1]=(byte)crcaccu;
return n+2;
}
}
</pre>
<p>This code is not giving me the correct crc bytes, and so I cannot establish communication from my PC master to the slave device... if you see anything that you recognize as being wrong, please let me know,
<p>Or if you have similar code that you know works, please share!
<p>Huge thanks,<br>
Nancy