Menu

I/O exception - failed to read

Alikxus
2014-06-02
2014-06-10
  • Alikxus

    Alikxus - 2014-06-02

    I have a problem - Exception in thread "main" com.ghgande.j2mod.modbus.ModbusIOException: I/O exception - failed to read at com.ghgande.j2mod.modbus.io.ModbusRTUTransport.readResponse(ModbusRTUTransport.java:213)
    at com.ghgande.j2mod.modbus.io.ModbusSerialTransaction.execute(ModbusSerialTransaction.java:189)
    at j2mod.J2mod.main(J2mod.java:73)

    when I run this program:

    package j2mod;

    import com.ghgande.j2mod.modbus.ModbusCoupler;
    import com.ghgande.j2mod.modbus.io.ModbusSerialTransaction;
    import com.ghgande.j2mod.modbus.msg.ReadInputRegistersRequest;
    import com.ghgande.j2mod.modbus.msg.ReadInputRegistersResponse;
    import com.ghgande.j2mod.modbus.net.SerialConnection;
    import com.ghgande.j2mod.modbus.util.SerialParameters;

    public class J2mod {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
    

    / The important instances of the classes mentioned before /
    SerialConnection con = null; //the connection
    ModbusSerialTransaction trans = null; //the transaction
    ReadInputRegistersRequest req = null; //the request
    ReadInputRegistersResponse res = null; //the response
    / Variables for storing the parameters /
    String portname = null; //the name of the serial port to be used
    int unitid = 0; //the unit identifier we will be talking to
    int ref = 0; //the reference, where to start reading from
    int count = 0; //the count of IR's to read
    int repeat = 1; //a loop for repeating the transaction
    try {
    portname = "COM1";
    unitid = 2;
    ref = 0;
    count = 8;
    } catch (Exception ex) {
    ex.printStackTrace();
    System.exit(1);
    }
    //2. Set master identifier
    //ModbusCoupler.createModbusCoupler(null);
    ModbusCoupler.getReference().setUnitID(1);
    //3. Setup serial parameters
    SerialParameters params = new SerialParameters();
    params.setPortName(portname);
    params.setBaudRate(1200);
    params.setDatabits(8);
    params.setParity("None");
    params.setStopbits(2);
    params.setEncoding("rtu");
    params.setEcho(false);
    //4. Open the connection
    con = new SerialConnection(params);
    con.open();
    //5. Prepare a request
    req = new ReadInputRegistersRequest(ref, count);
    req.setUnitID(unitid);
    req.setHeadless();

    //6. Prepare a transaction
    trans = new ModbusSerialTransaction(con);
    trans.setRequest(req);
    int k = 0;
    do {
    trans.execute();
    res = (ReadInputRegistersResponse) trans.getResponse();
    for (int n = 0; n < res.getWordCount(); n++) {
    System.out.println("Word " + n + "=" + res.getRegisterValue(n));
    }
    k++;
    } while (k < repeat);
    //8. Close the connection
    con.close();
    }
    }

    Help me, please.

     

    Last edit: Alikxus 2014-06-02
    • acp

      acp - 2014-06-06

      I have a similar issue with Serial connection in an ubuntu.

       
      • Julie Haugh

        Julie Haugh - 2014-06-08

        You are both using 1200 baud, or you are both seeing I/O exceptions because the program you wrote is unable to read the response?

        If you're having issues with 1200 baud, it is going to take me some doing before I can put together a 1200 baud piece of test hardware.

        acp, if you aren't using 1200 baud, please start a different thread. The reason is that 1200 baud is very slow and is probably exposing bugs in the timing code.

        As an aside, Modbus will work reliabily at 9600 and 19200 baud for distance up to several hundred meters.

         
  • acp

    acp - 2014-06-09

    Julie, we are using 115200 baud. I will move this to a different thread. Thanks.

     
  • Alikxus

    Alikxus - 2014-06-10

    I am limited to 1200

     

Log in to post a comment.