// CRC.java // Maria Kazandjieva, marykaz@hotmail.com, Summer 2003 // This file puts together a byte array over which CRC has to be calculated // Right now the CRC function is not working // In theaory, when correct CRC is calculated, it should be appended to the // end of the byte array and the resulting will be a valid comman for the RFID reader import java.io.*; import java.util.*; import java.lang.String; import java.math.BigInteger; public class CRC { public CRC() {} public static void main(String [] args){ short crc_value = 0; int tagType = 01; String[] tagstr; int[] tagID = new int[8]; byte[] data = new byte[4]; byte[] tag = new byte[8]; byte[] startB = new byte[1]; byte[] numB = new byte[1]; // ** STX byte byte[] stxB = new BigInteger("2", 16).toByteArray(); System.out.println("****STX: " + stxB[0]); // ** TagType 01 byte[] tagTypeB = new BigInteger("1", 16).toByteArray(); System.out.println("****TagType: " + tagTypeB[0]); // ** set the appropriate flags and construct the FLAGS byte String[] flags = {"RID_F", "TID_F", "CRC_F", "AFI_F", "RF_F", "LOCK_F", "INV_F", "LOOP_F"}; BufferedReader buffer = new BufferedReader(new InputStreamReader(System.in)); String s1 = ""; String[] flag = new String[8]; String flagString = ""; System.out.println("\n\nStep 1: Constructing the FLAGS byte. Enter 1 or 0 for each flag."); for (int i=0; i>> 0) & 0xff ); // ** construct the byte used for calculating the CRC if (flag[1].equals("1")){ if(intcommand==14){ byte[] ba = {stxB[0], flagValue[3], command[0], tagTypeB[0], tag[0], tag[1], tag[2], tag[3], tag[4], tag[5] ,tag[6], tag[7]}; CRCcalc(ba); } if(intcommand==24){ byte[] ba = {stxB[0], flagValue[3], command[0], tagTypeB[0], tag[0], tag[1], tag[2], tag[3], tag[4], tag[5], tag[6],tag[7], startB[0], numB[0]}; CRCcalc(ba); } if(intcommand==44){ byte[] ba = {stxB[0], flagValue[3], command[0], tagTypeB[0], tag[0], tag[1], tag[2], tag[3], tag[4], tag[5], tag[6],tag[7], startB[0], numB[0], data[0], data[1], data[2], data[3]}; CRCcalc(ba); } } else { if (intcommand==14){ byte[] ba = {stxB[0], flagValue[3], command[0], tagTypeB[0]}; CRCcalc(ba); } if (intcommand==24){ byte[] ba = {stxB[0], flagValue[3], command[0], tagTypeB[0], startB[0], numB[0]}; CRCcalc(ba); } if(intcommand==44){ byte[] ba = {stxB[0], flagValue[3], command[0], tagTypeB[0], startB[0], numB[0], data[0], data[1], data[2], data[3]}; CRCcalc(ba); } } } // end main // ** Method for CRC calculation public static void CRCcalc(byte[] b){ int x,y; int crc_16 = 0x0000; int compare = 0x0001; for (x=1; x < b.length; x++){ crc_16 ^=b[x]; for (y=0; y<8; y++){ if ((crc_16 & compare) == 1){ crc_16>>=1; crc_16 ^=0x8408; } else { crc_16>>=1; } } } System.out.println("CRC = " + crc_16); System.out.println("HexCRC = " + Integer.toHexString(crc_16)); } // ** end of method public static void printBytes(byte[] ba){ for (int i = 0; i