Thread threadInit = new Thread(new Runnable() {
public void run() {
Message msg = null;
enExecution = true;
Integer i = 0;
while (enExecution) {
try {
i++;
readDataMb();
Thread.sleep(1000); //mmsec Refresh data
} catch (InterruptedException e) {
Log.d("[Android]Main", e.getMessage());
}
}
}
});
threadInit.start();
private void readDataMb() {
String astr = "192.168.3.88"; //Modbus Device
InetAddress addr = null;
try {
addr = InetAddress.getByName(astr);
} catch (UnknownHostException e) {
e.printStackTrace();
System.exit(1);
}
int port = 502;
TCPMasterConnection con = new TCPMasterConnection(addr);
con.setPort(port);
try {
con.connect();
} catch (Exception e) {
e.printStackTrace();
System.exit(2);
}
try {
ModbusTCPTransaction trans = null; // the transaction
String refe = "4000";// HEX Address
int ref = Integer.parseInt(refe, 16);// Hex to int
int count = 2; // the number Address to read
ReadMultipleRegistersRequest Req = new ReadMultipleRegistersRequest(
ref, count);
ReadMultipleRegistersResponse Res = new ReadMultipleRegistersResponse();
// 3. Start Transaction
trans = new ModbusTCPTransaction(con);
trans.setRetries(5);
trans.setReconnecting(true);
trans.setRequest(Req);
int k = 0;
do {
trans.execute();
Res = (ReadMultipleRegistersResponse) trans.getResponse();
Temp_Room = (Res.getRegisterValue(0));
k++;
} while (k < count);
} 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();
}
Theoretically my thread should be updated every second but in reality it is not, it is very slow but I don't no which is the problem.
Someone can help me?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
You can try to reduce your sleep time so that the read operation is able to work more frequently. As long as you have some sleep time in the loop it will prevent it from spinning and using up all of the processor time. Also you probably want to open the TCP socket in a separate method and leave it open for the duration rather than reconnecting every second. That is probably where all the extra time is going.
John
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I solved, the problem was that the modbus data simulator (http://www.plcsimulator.org/) was set with number of server connection 10, changing this parameter with 1000 the app works fine.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Theoretically my thread should be updated every second but in reality it is not, it is very slow but I don't no which is the problem.
Someone can help me?
You can try to reduce your sleep time so that the read operation is able to work more frequently. As long as you have some sleep time in the loop it will prevent it from spinning and using up all of the processor time. Also you probably want to open the TCP socket in a separate method and leave it open for the duration rather than reconnecting every second. That is probably where all the extra time is going.
John
Hi John, do you have an exampe with refresh update of 1 second?
I don't have an example, but it should work to open the connection first and close it at the end when finished. In the loop do read and display the output. If your goal is to have 1 sec output you should set a timer event for 1sec to update the output. Here is an example of a timer thread but you can do your own google search to find other examples:
https://stackoverflow.com/questions/9413656/how-to-use-timer-class-to-call-a-method-do-something-reset-timer-repeat?newreg=0d9fcf1d977f487f8c72bc2ee6b33b99
I solved, the problem was that the modbus data simulator (http://www.plcsimulator.org/) was set with number of server connection 10, changing this parameter with 1000 the app works fine.