我通过以太网modbus tcp连接有问题。 我在AndroidManifest中插入权限: 我创建任务或连接和阅读Modbus。 我使用jamod库。
当我启动应用程序时,会给我一个连接失败错误。
这是我使用的代码:
class Task implements Runnable {
@
Override
public void run() {
try {
ReadMultipleRegistersResponse result = null;
//Read And Write Register Sample
int port = Modbus.DEFAULT_PORT;
String refe = "4000"; //HEX Address
int ref = Integer.parseInt(refe, 16); //Hex to int
int count = 98; //the number Address to read
int SlaveAddr = 1;
String astr = "192.168.0.18"; //Modbus Device
InetAddress addr = InetAddress.getByName(astr);
TCPMasterConnection con = new TCPMasterConnection(addr);
ModbusTCPTransaction trans = null; //the transaction
//1.Prepare the request
/************************************/
ReadMultipleRegistersRequest Rreq = new ReadMultipleRegistersRequest(ref, count);
ReadMultipleRegistersResponse Rres = new ReadMultipleRegistersResponse();
Rreq.setUnitID(SlaveAddr); //set Slave Address
Rres.setUnitID(SlaveAddr); //set Slave Address
//2. Open the connection
con.setPort(port);
con.connect();
con.setTimeout(2500);
//3. Start Transaction
trans = new ModbusTCPTransaction(con);
trans.setRetries(5);
trans.setReconnecting(true);
trans.setRequest(Rreq);
trans.execute();
/*Print Response*/
Rres = (ReadMultipleRegistersResponse) trans.getResponse();
} catch (ModbusSlaveException me) {
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (java.lang.Exception e) {;
e.printStackTrace();
}
}
}
我在哪里做错了?
答案 0 :(得分:0)
按照步骤
检查您的奴隶是否正常工作
检查与主设备具有相同网络的从设备
检查您的安全端口设置(默认为502)