Good morning, get the modbus master tcp to work.
This gives me the values in hexadecimal, I did some tests and it works perfectly.
The last two pairs of 00 show me the data reading, at first I think it is the information of the slave, the type of data to read, in this case holding register.
What I can not do is get the data I read in a floating number
importjava.io.*;importjava.lang.*;importjava.net.InetAddress;importnet.wimpi.modbus.Modbus;importnet.wimpi.modbus.io.ModbusTCPTransaction;importnet.wimpi.modbus.msg.WriteCoilRequest;importnet.wimpi.modbus.msg.ReadInputRegistersRequest;importnet.wimpi.modbus.msg.ReadInputRegistersResponse;importnet.wimpi.modbus.msg.ReadCoilsRequest;importnet.wimpi.modbus.msg.ReadCoilsResponse;importnet.wimpi.modbus.msg.ReadInputDiscretesRequest;importnet.wimpi.modbus.msg.ReadInputDiscretesResponse;importnet.wimpi.modbus.msg.ReadMultipleRegistersRequest;importnet.wimpi.modbus.msg.ReadMultipleRegistersResponse;importnet.wimpi.modbus.net.TCPMasterConnection;importjava.io.FileOutputStream;importorg.apache.poi.hssf.usermodel.HSSFCell;importorg.apache.poi.hssf.usermodel.HSSFRichTextString;importorg.apache.poi.hssf.usermodel.HSSFRow;importorg.apache.poi.hssf.usermodel.HSSFSheet;importorg.apache.poi.hssf.usermodel.HSSFWorkbook;publicclasstest_modbus{publicstaticvoidmain(Stringargs[]){try{/*Theimportantinstancesoftheclass*/TCPMasterConnectioncon=null;//laconexionModbusTCPTransactiontrans=null;//latransaccionReadMultipleRegistersRequestrreq=null;//lalecturarequeridaReadMultipleRegistersResponserres=null;//lalecturaentregada/*Variablesparaguardarlosparametros*/InetAddressaddr=null;//ladirecciondelesclavointport=502;//elpuertopordefecto//1.Configurarlosparámetrosaddr=InetAddress.getByName("84.31.57.55");//**Ladireccion//asignadaael//modulo**//2.Abriendolaconexioncon=newTCPMasterConnection(addr);con.setPort(port);con.connect();System.out.println("--- Message: Line:36 success --- ");//~~~~~~~~~~~~~~~~~~~~Lasolicituddelecturadefectuosa~~~~~~~~~~~~~~~~~~~~//3r.Prepararlapeticiondelecturaintk=5391;//Numeroderegistroaleer,sinel4rreq=newReadMultipleRegistersRequest(k,1);//4r.Preparandolatransacciondelecturatrans=newModbusTCPTransaction(con);trans.setRequest(rreq);System.out.println("--- Message: Line:46 success --- ");//5r.Ejecutarlatransaccionleertrans.execute();rres=(ReadMultipleRegistersResponse)trans.getResponse();System.out.println("Valor hexa de registro "+" = "+rres.getHexMessage());//Secreaunarchivodetextoparaguardarelvalorobtenido//Abrostream,creaelficherosinoexisteFileWriterfw=newFileWriter("C:\\Users\\sels.ldisa\\Documents\\netbeans ejemplos\\fichero1.txt",true);fw.write(rres.getHexMessage()+",\r\n");//Cierroelstreamfw.close();//Abroelstream,elficherodebeexistirFileReaderfr=newFileReader("C:\\Users\\sels.ldisa\\Documents\\netbeans ejemplos\\fichero1.txt");//Leemoselficheroylomostramosporpantalla//intvalor=fr.read();//while(valor!=-1){//System.out.print((char)valor);//valor=fr.read();}//6.Closetheconnection//con.close();}catch(Exceptionex){System.out.println("Error");ex.printStackTrace();}}}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Good morning, get the modbus master tcp to work.
This gives me the values in hexadecimal, I did some tests and it works perfectly.
The last two pairs of 00 show me the data reading, at first I think it is the information of the slave, the type of data to read, in this case holding register.
What I can not do is get the data I read in a floating number