Java Modbus Checksum (LRC) issue

Discussion in 'Programmer's Corner' started by jamespshan, Oct 3, 2012.

  1. jamespshan

    Thread Starter New Member

    Oct 3, 2012

    I am trying to get existing java code to communicate with a device through a serial port using net.wimpi.modbus java libraries and ascii encoding.

    My problem is I believe that the LRC checksum value being generated from the modbus java libraries appear to be incorrect but I cannot find where this occurs or see any means of influencing it.

    The packet I am sending is :320300010054
    I'm told the correct LRC value to be appended for this is AE however if I snoop on the serial port data I see that the java application is sending 76.

    If I use use a third party tool to send the correct packet :)320300010054A4) it receives the correct response.
    Does anyone know how I can fix this?

    Below is some sample code which I cam using. The red text in bold (trans.execute(); ) is where the packet is sent.

    Any help is greatly apppreciated.



    Code ( (Unknown Language)):
    2.    SerialParameters params=new SerialParameters();
    3.    params.setPortName("COM1");
    4.    params.setBaudRate(9600);
    5.    params.setParity("Even");
    6.    params.setEncoding("ascii");
    7.    params.setReceiveTimeout(3000);
    9.    SerialConnection con = con = new SerialConnection(params);
    10.    try {
    12.    }
    13.    catch (Exception e) {
    14.        throw new IOException("Problem opening serial port");
    15.    }    
    16.    try {
    17.         con.getSerialPort().enableReceiveTimeout(SERIAL_RECEIVE_TIMEOUT);
    18.    }
    19.     catch (UnsupportedCommOperationException e) { throw new    IOException("Failed to set serial port timeout of " + SERIAL_RECEIVE_TIMEOUT + "ms");
    20. }
    21.    ModbusRequest req = new ReadMultipleRegistersRequest(1, 84);
    22.    req.setUnitID(50);
    23.    req.setHeadless();
    24.    req.setHeadless();
    26.    ModbusTransaction trans = new ModbusSerialTransaction(con);
    27.    trans.setRequest(req);
    28.    [B][COLOR=Red]trans.execute();[/COLOR][/B]
    29.    ModbusResponse response = trans.getResponse();
    Last edited by a moderator: Oct 4, 2012
  2. jamespshan

    Thread Starter New Member

    Oct 3, 2012
    Ok, so I removed the jarmod jar file reference from my project and unpacked the full source code into my project instead. This give me the visibility into the checksum calculation performed in that I couldn't see before.

    The method seems correct which leads me to believe that the device Im communicating with via modbus contains a bug or custom LRC calculation.

    I will take this up with the engineer but for now my immediate issue was solved by applying the original source code to the project in place of the jamod library.