0

I am using jamod1.2 , I tried to send boolean values though this code and it works perfectly.

Now I am trying to send an integer value to some registers I have , like sending 5 to %mw100 ! how I can do this through the jamod library or if there is any easier way to do that?

    TCPMasterConnection con = null;
    try {
        con = new TCPMasterConnection(InetAddress.getByName(IP));
    } catch (UnknownHostException e) {
        e.printStackTrace();
    }
    con.setPort(502);
    try {
        con.connect();
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Prepare the transaction
    ModbusTCPTransaction trans = new ModbusTCPTransaction(con);

    // Prepare the request
    WriteCoilRequest wcr = new WriteCoilRequest();

    // Don't know what unit ID is; 1 seems to work
    wcr.setUnitID(1);

    // This is the number of the coil to set.
    wcr.setReference(unit_num);

    // Turn coil on or off?
    // This doesn't physically do anything; it just specifies
    // what state should be set later on.
    if(value=="false") {
        wcr.setCoil(false);
    }else{
        wcr.setCoil(true);
    }

    // Must execute a transaction now to actually do stuff.
    trans.setRequest(wcr);
    try {
        trans.execute();
    } catch (ModbusException e) {
        e.printStackTrace();
    }

    WriteCoilResponse res = (WriteCoilResponse) trans.getResponse();

    if (res != null) {
        System.out.println("Set coil " + res.getReference() + " to "
                + res.getCoil());
    }

    con.close();
rand
  • 515
  • 2
  • 5
  • 14

1 Answers1

0

i have an method to set 16 bit value in register.

public class PlcMethods {

TCPMasterConnection con = null; //the connection
ModbusTCPTransaction trans = null; //the transaction
ReadInputDiscretesRequest req = null; //the request
ReadInputDiscretesResponse res = null; //the response

public PlcMethods( String ipaddress , int port ) throws UnknownHostException {

    InetAddress ipaddr = null;

    ipaddr = InetAddress.getByName(ipaddress);

    con = new TCPMasterConnection(ipaddr);
    con.setPort(port);
    System.out.println("Making connection to Plc..");

    try {
        con.connect();
    } catch (Exception e) {
        System.err.println("Connection successful");
        e.printStackTrace();
    }
}

public void writeRegister(int regaddr, int value) throws ModbusIOException, ModbusSlaveException, ModbusException {

    WriteSingleRegisterRequest WriteReq = null; 
    SimpleRegister MyReg = new SimpleRegister(1);

    //3. Prepare the request
    WriteReq = new WriteSingleRegisterRequest();
    WriteReq.setReference(regaddr);  //register number
    MyReg.setValue(value);         //value for register
    WriteReq.setRegister(MyReg);

    //4. Prepare the transaction

    trans = new ModbusTCPTransaction(con);
    trans.setRequest(WriteReq);

    trans.execute();

} 
 public static void main(String[] args) throws UnknownHostException {
    PlcMethods plc = new PlcMethods("192.168.0.34",502);
    try {
        plc.writeRegister(0x1000, 58880);
    } catch (ModbusException e) {
        e.printStackTrace();
    }
}


}

Hope it answers your question!