Reading register values from modbus simulator

2019-09-07 03:42发布

问题:

The connection has been established to modbus simulator but I am trying to read the register values from simulator and write it on console but not able to do. this code creates the connection successfully to modbus simulator but contains the syntax of reading the register values i.e requesting for register values and also response about the same but unable to fetch the register values. I tried initializing the register values and also the range but the code isn't working either. Below is the code i tried. I have initialized and entered the range of register values in the code but unable to fetch the values from the modbus simulator. Below is the code I tried.

package com.example.and;

import java.net.InetAddress;
import net.wimpi.modbus.Modbus;
import net.wimpi.modbus.ModbusException;
import net.wimpi.modbus.ModbusIOException;
import net.wimpi.modbus.ModbusSlaveException;
import net.wimpi.modbus.io.ModbusTCPTransaction;
import net.wimpi.modbus.msg.ReadInputDiscretesRequest;
import net.wimpi.modbus.msg.ReadInputDiscretesResponse;
import net.wimpi.modbus.net.TCPMasterConnection;
import android.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity
{   
      public static void main(String[] args) {
        try {

        } catch (Exception ex) {
          ex.printStackTrace();
        }
      }//main

    //class DITest
    /* The important instances of the classes mentioned before */
      {

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

    /* Variables for storing the parameters */
    InetAddress addr = null; //the slave's address
    int port = Modbus.DEFAULT_PORT;
    int ref = 30100; //the reference; offset where to start reading from
    int count = 10; //the number of DI's to read
    int repeat = 1; //a loop for repeating the transaction

    String args[] = null;

    //1. Setup the parameters
    if (args.length < 3) {
    //System.exit(1);
    } else {
    try {

      String astr = args[0];
      int idx = astr.indexOf(':');
      if(idx > 0) {
        port = Integer.parseInt(astr.substring(idx+1));
        astr = astr.substring(0,idx);
      }
      addr = InetAddress.getByName(astr);
      ref = Integer.decode(args[1]).intValue();
      count = Integer.decode(args[2]).intValue();
      if (args.length == 4) {
        repeat = Integer.parseInt(args[3]);
      }
    } catch (Exception ex) {
      ex.printStackTrace();
      //System.exit(1);
    }
    }

    //2. Open the connection
    con = new TCPMasterConnection(addr);
    con.setPort(port);
    try {
        con.connect();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    //3. Prepare the request
    req = new ReadInputDiscretesRequest(ref, count);

    //4. Prepare the transaction
    trans = new ModbusTCPTransaction(con);
    trans.setRequest(req);
    }

    //5. Execute the transaction repeat times
    int k = 0;
    {
    int repeat = 0;
    do {
    ModbusTCPTransaction trans = null;
    try {
        trans.execute();
    } catch (ModbusIOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ModbusSlaveException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ModbusException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    ReadInputDiscretesResponse res = (ReadInputDiscretesResponse) trans.getResponse();
    System.out.println("Digital Inputs Status=" + res.getDiscretes().toString());
    k++;
    } while (k < repeat);

    TCPMasterConnection con = null;
    //6. Close the connection
    con.close();
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_item);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
    //  getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}