user2782405
user2782405

Reputation: 393

Modbuspal slave and Jamod as tcp master

I'm using Modbuspal simulator as slave and Jamod tcp master code as master. The following is my Jamod tcp master code to read data from registers:

import java.net.InetAddress;
import net.wimpi.modbus.io.ModbusTCPTransaction;
import net.wimpi.modbus.msg.ReadInputDiscretesRequest;
import net.wimpi.modbus.msg.ReadInputDiscretesResponse;
import net.wimpi.modbus.net.TCPMasterConnection;

public class SlaveTest {

Logger LOG = LoggerFactory.getLogger(SlaveTest.class);

TCPMasterConnection connection;
ModbusTCPTransaction transaction = null; //the transaction
ReadInputDiscretesRequest request = null; //the request
ReadInputDiscretesResponse response = null; //the response


public void test() {

    connection = null; //the connection             
    InetAddress addr = null; //the slave's address
    int port = 502;
    int ref = 1, count = 1;

    try {
        addr = InetAddress.getByName("127.0.0.1");
        connection = new TCPMasterConnection(addr);
        connection.setPort(port);
        connection.connect();
        request = new ReadInputDiscretesRequest(ref, count);
        transaction = new ModbusTCPTransaction(connection);
        transaction.setRequest(request);
        transaction.execute();
        response = (ReadInputDiscretesResponse) transaction.getResponse();
        System.out.println("Digital Inputs Status=" + response.getDiscretes().toString());
        connection.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Modbuspal Slave:

Modbuspal slave

When I execute my code, connection is established but I get the following error:

net.wimpi.modbus.ModbusIOException: Executing transaction failed (tried 3 times)
at         net.wimpi.modbus.io.ModbusTCPTransaction.execute(ModbusTCPTransaction.java:197)
at modbus.slave.SlaveTest.test(SlaveTest.java:49)
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.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:109)
at org.apache.camel.component.bean.BeanProcessor.process(BeanProcessor.java:68)
at org.apache.camel.component.bean.BeanProducer.process(BeanProducer.java:38)
at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:141)
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)

Error is triggers at line "transaction.execute();". Please help.

Thanks in Advance.

Upvotes: 1

Views: 1513

Answers (1)

Matin Meskin
Matin Meskin

Reputation: 86

You need to provide unit ID for your master. So, try the following:

request = new ReadInputDiscretesRequest(ref, count);
request.setUnitID(ID)

and the ID is your slave unit ID and should be a number

Upvotes: 1

Related Questions