已建立与 modbus 模拟器的连接,但我试图从模拟器中读取寄存器值并将其写入控制台但无法执行。此代码成功地创建了与 modbus 模拟器的连接,但包含读取寄存器值的语法,即请求寄存器值以及响应大致相同但无法获取寄存器值。我尝试初始化寄存器值和范围,但代码也不起作用。下面是我试过的代码。我已经在代码中初始化并输入了寄存器值的范围,但无法从 modbus 模拟器中获取值。下面是我试过的代码。
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;
}
}