1
votes

How to pass length in CRC16 modbus calculation in java?

I have found the following method in the link:

http://www.codeproject.com/Articles/19214/CRC-16-Calculation

How to pass length value in the second argument.

public int crc16(byte[] modbusframe,int Length)
 {
    int i;
    int index;
    int crc_Low = 0xFF;
    int crc_High = 0xFF;

    for (i = 0; i<Length; i++ )
    {
         index = crc_High ^ (char)modbusframe[i];
         crc_High = crc_Low ^ crc_table[index];
         crc_Low = (byte)crc_table[index+ 256];                             
    }

    return crc_High*256+crc_Low;        
 } 

Thanks in advance..

1
I don't understand your question, as you already pass the length as second argument. Note that you don't really need to pass it, you can us modbusframe.length instead. - PhiLho
i got from codeproject web. i dont know how to calculate the CRC. so i asked that. i passed the 16, modbusframe.length, and all i got a error on that. - Babu R
Define 'got an error'. And there are better versions of the computing there. You don't need to calculate the high and low bytes separately. - user207421

1 Answers

0
votes

Number of initialized variables array modbusframe.

modbus_bytes[**0**]=device_address;
modbus_bytes[**1**]=data_type;
modbus_bytes[**2**]=(byte)start_address_higer;
modbus_bytes[**3**]=(byte)start_address_lower;
modbus_bytes[**4**]=0x00;
modbus_bytes[**5**]=length;

int crc_result=CRC.crc16(modbus_bytes,**6**);