Hi, I'm not bkhashfeh but I have the same problem and I'd apreciate the help :D . Here is my code where I'm reading three registers per second (or trying).
package modbusmastertest;
import com.serotonin.modbus4j.ModbusFactory;
import com.serotonin.modbus4j.ModbusMaster;
import com.serotonin.modbus4j.code.DataType;
import com.serotonin.modbus4j.ip.IpParameters;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Calendar;
class MBMasterTest {
public static void main(String[] args) throws Exception {
// Modbus master settings
ModbusFactory factory = new ModbusFactory();
IpParameters params = new IpParameters();
params.setHost("192.168.0.1");
params.setPort(502);
params.setEncapsulated(false);
ModbusMaster master = factory.createTcpMaster(params, false);
master.setTimeout(333);
master.setRetries(1);
// CSV writter settings
FileWriter fwriter;
fwriter = new FileWriter("C:/Users/NESTOR/Dropbox/Siemens_S7-1200.csv");
PrintWriter outputFile = new PrintWriter(fwriter);
outputFile.println("Date,Sensor_1,Sensor_2,Sensor_3"); // Primera línea
Object []Register = new Object[3];
// Date settings
Calendar cal = Calendar.getInstance();
long elapsedTime = 0;
long time = 0;
// Cuerpo del programa
long start = System.currentTimeMillis(); // Tiempo de inicio
try {
master.init();
while(System.currentTimeMillis()<(start+10000)) {
time = System.currentTimeMillis();
for(int i = 0; i<3; i++) {
Register* = master.getValue(1,40001+i,DataType.TWO_BYTE_INT_SIGNED);
}
System.out.println(Register[0]+","+Register[1]+","+Register[2]);
System.out.println(""
+ "");
elapsedTime = System.currentTimeMillis()-time;
cal.setTimeInMillis(cal.getTime().getTime() + elapsedTime);
outputFile.println(cal.getTime().toString()+","+Register[0]+","+Register[1]+","+Register[2]); // Se escribe nueva linea en archivo
}
outputFile.close(); // Se cierra el archivo
}
finally {
master.destroy(); //Se destruye el objeto master
}
System.out.println(""
+ "");
System.out.println("Tardo " + (System.currentTimeMillis()-start) + "ms");
}
}