steveohara / j2mod

Enhanced Modbus library implemented in the Java programming language
Apache License 2.0
265 stars 111 forks source link

Timeout for readInputRegister #83

Closed tvraghavan closed 6 years ago

tvraghavan commented 6 years ago

Expected Behavior

Am testing my code against a modBus simulator (mod_RSsim.exe from Embedded Intelligence) The purpose of my code is to read the data inputRegister from specific address

As part of the testing, I've created simulated USB ports (COM6) in my Windows7 OS and use RSsim modbus simulator to simulate inputRegister data.

Now, when the simulator is down, however (COM6) port is up. the readInputRegister(unitID, refID, readCount) waits forever and does not timeout the read call.

Expected behavior is to timeout the readInputRegister() or support a timeout feature

Note: I tried to move the logic to a Callable implementation and tried to stop the child thread (future.cancel(true)). Even this does not terminate the readInputRegister() call.

Actual Behavior

readInputRegister() stays blocked

Steps to Reproduce the Problem

Code snippet

                                        ResponseData responseData = new ResponseData();
                    SerialParameters serialParamters = new SerialParameters();
                    serialParamters.setBaudRate(19200);
                    serialParamters.setPortName("COM6");
                    serialParamters.setStopbits(2);
                    serialParamters.setParity(0);
                    serialParamters.setDatabits(8);
                    serialParamters.setEncoding("rtu");
                    ModbusSerialMaster master = new ModbusSerialMaster(serialParamters,1000 );
                    logger.debug("trying to read response");
                    try {
                        InputRegister[] inputRegister = master.readInputRegisters(unitID,refID, readCount);
                        logger.debug("Length of input register:" + inputRegister.length);

                        for(InputRegister ir: inputRegister) {
                            sb.append(ir.getValue());
                            logger.debug("bytes="+ir.getValue());
                        }
                        responseData.setResponseData(new String(sb.toString()));

                    } catch (Exception e1) {
                        logger.error("Unable to read data from register because of:",e1);
                    }

Specifications

steveohara commented 6 years ago

I've added a test case to the code that tests this specific issue and I can't replicate it.

Take a look at TestModbusSerialRTUMasterRead.testBadCommsPort() The call master.readInputRegisters(UNIT_ID, 0, 5) tries 5 times to get through and comes back after about 9 seconds.

Try out the v2.5.1 snapshot and let me know if there is still an issue.

steveohara commented 6 years ago

No response for over 2 months so I'm closing this ticket