1

I'm using serial Modbus (jamod) to read data from device. I'm able to connect to the device but unable to read data from the register.

Here is my code:

public class Serial {

    static SerialConnection connection = null;
    SerialParameters params;
    ModbusSerialTransaction transaction = null;

    ReadMultipleRegistersRequest holdingRequest = null;
    ReadMultipleRegistersResponse holdingResponse = null;

    public void getData() {
        try {
            ModbusCoupler.getReference().setUnitID(1);
            params = new SerialParameters();
            params.setPortName("/dev/ttyS0");
            params.setBaudRate(9600);
            params.setDatabits(8);
            params.setEncoding(Modbus.SERIAL_ENCODING_RTU);
            params.setParity("None");
            params.setStopbits(1);
            params.setEcho(false);
            params.setReceiveTimeout(500);
            connection = new SerialConnection(params);
            connection.open();
            checkRegister(40257, 2);
            connection.close();

        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public void registerData(int address, int count) {
        holdingRequest = new ReadMultipleRegistersRequest(address, count);
        holdingResponse = new ReadMultipleRegistersResponse();
        holdingRequest.setUnitID(130); // I tried with 1 and getting same error
        holdingRequest.setHeadless();
        holdingResponse = (ReadMultipleRegistersResponse) executeRequest(connection, holdingRequest);
        System.out.println("Response: " + holdingResponse);
    }

    private ModbusResponse executeRequest(SerialConnection connection, ModbusRequest request) {
        try {
            transaction = new ModbusSerialTransaction(connection);
            transaction.setRequest(request);
            Thread.sleep(500);
            transaction.execute(); // Error at this line
            return transaction.getResponse();
        } catch(Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

I'm getting the following error at line transaction.execute():

net.wimpi.modbus.ModbusIOException: I/O exception - failed to read
    at net.wimpi.modbus.io.ModbusRTUTransport.readResponse(ModbusRTUTransport.java:163)
    at net.wimpi.modbus.io.ModbusSerialTransaction.execute(ModbusSerialTransaction.java:187)
    at Serial.executeRequest(Serial.java:279)
    at Serial.checkRegister(Serial.java:337)
    at Serial.getData(Serial.java:66)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.camel.component.bean.MethodInfo.invoke(MethodInfo.java:408)
    at org.apache.camel.component.bean.MethodInfo$1.doProceed(MethodInfo.java:279)
    at org.apache.camel.component.bean.MethodInfo$1.proceed(MethodInfo.java:252)
    at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:177)
    at org.apache.camel.management.InstrumentationProcessor.process(InstrumentationProcessor.java:77)
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:460)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:190)
    at org.apache.camel.component.timer.TimerConsumer.sendTimerExchange(TimerConsumer.java:165)
    at org.apache.camel.component.timer.TimerConsumer$1.run(TimerConsumer.java:73)
    at java.util.TimerThread.mainLoop(Timer.java:555)
    at java.util.TimerThread.run(Timer.java:505)

Any advice would be appreciated.

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
user2782405
  • 393
  • 1
  • 6
  • 20
  • What is the device? What is the physical interface (RS-232, RS-422, something else?) Sounds like you are going to have to determine if the device is actually responding, which is down at the hardware level. – Jim Garrison Jun 15 '16 at 05:50
  • It's an energy meter. I'm using RS-485 and Yes, the device is responding. – user2782405 Jun 15 '16 at 05:57
  • Hey! @user2782405 , Were you able to resolve this problem.. If Yes! then how. Actually I'm facing the same issue with same device.. – Kumar Anil Chaurasiya May 17 '20 at 13:07
  • There was an issue with my embedded computer. For some reason, it couldn't recognize serial ports. When I switched to a different unit, it started working – user2782405 May 20 '20 at 14:24
  • @KumarAnilChaurasiya also, I remember opening serial port before running this code. But I don't remember the steps. Hope this helps! – user2782405 May 21 '20 at 07:36
  • @user2782405 , Can look into this once. let me know know if I am doing anything wrong. https://stackoverflow.com/questions/61852628/j2mod-i-o-exception-failed-to-read – Kumar Anil Chaurasiya May 21 '20 at 15:18

0 Answers0