我有这个简单的 android modbus tcp 代码读取:(with jamod-1.2-SNAPSHOT) public class MainActivity extends Activity {
// The important instances of the classes mentioned before
TCPMasterConnection con = null; // the TCP connection
ModbusTCPTransaction trans = null; // the Modbus transaction
// Variables for storing the parameters
InetAddress addr = null; // the slave's address
int port = Modbus.DEFAULT_PORT;
int count = 10; // the number Address to read
@Override
protected void onCreate(Bundle savedInstanceState) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
.permitAll().build();
StrictMode.setThreadPolicy(policy);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_listener();
}
/*
* MENÜ
*
* @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the
* menu; this adds items to the action bar if it is present.
* getMenuInflater().inflate(R.menu.main, menu); return true; }
*/
public void button_listener() {
Button read = (Button) findViewById(R.id.button1);
read.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
TextView values = (TextView) findViewById(R.id.textView1);
values.setText("Gomb!");
// start register
int startReg;
startReg = 0;
ReadMultipleRegistersRequest req = null; // the request
ReadMultipleRegistersResponse res = null; // the response
count = 5;
// Prepare the request
req = new ReadMultipleRegistersRequest(startReg, count);
// Prepare the transaction
trans = new ModbusTCPTransaction(con);
trans.setRequest(req);
// execute the transaction
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();
}
// get the response
res = (ReadMultipleRegistersResponse) trans.getResponse();
}
});
}
@Override
protected void onStop() {
super.onStop();
// Close the TCP connection
con.close();
}
@Override
protected void onResume() {
super.onResume();
try {
// specify the slave IP address
addr = InetAddress.getByName("192.168.1.107");
// Open the connection
con = new TCPMasterConnection(addr);
con.setPort(port);
con.connect();
} catch (Exception e) {
Log.d("MODBUS", "connection error");
}
}
我收到了这个错误信息:
08-31 14:49:08.670: E/AndroidRuntime(7245): java.lang.NoClassDefFoundError: net.wimpi.modbus.net.TCPMasterConnection
有人可以帮助我为什么会出现此错误吗?错误是第 118 行,即:
con = new TCPMasterConnection(addr);
或者我怎样才能将它修改为正确的工作代码?因为它在 java 的 netbeans 中工作。
谢谢你的回答!