public class NRFCutterHoldingRead
{
public static void main(String[] args) {
TCPMasterConnection con = null; //the connection
ModbusTCPTransaction trans = null; //the transaction
ReadMultipleRegistersRequest req = null; //the request
ReadMultipleRegistersResponse res = null; //the response
try {
String astr = "10.80.46.2"; //Modbus Device
InetAddress addr = InetAddress.getByName(astr);
int port =502;
int ref = 0; //the reference; offset where to start reading from
int count = 4; //the number of DI's to read
Integer[] result = new Integer[count];
//1. Setup the parameters
if (args.length < 1) {
System.exit(1);
} else {
try {
ref = Integer.decode(args[0]).intValue();
} catch (Exception ex) {
ex.printStackTrace();
System.exit(1);
}
}
/* Variables for storing the parameters */
con = new TCPMasterConnection(addr);
con.setPort(port);
con.connect();
//3. Prepare the request
req = new ReadMultipleRegistersRequest(ref, count);
//4. Prepare the transaction
trans = new ModbusTCPTransaction(con);
trans.setRequest(req);
trans.execute();
res = (ReadMultipleRegistersResponse) trans.getResponse();
for ( int w = 0; w < res.getWordCount(); w++ ) {
System.out.println("Got Modbus read {} response {}" + ref + w + res.getRegisterValue(w));
result[w] = res.getRegisterValue(w);
}
System.out.println(Arrays.toString(result));
con.close();
System.exit(1);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
Any help or guidance would be appreciated.
Last edit: Greg Cushing 2016-01-06
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Regarding reading the data, can you not do it by using the ReadInputRegistersRequest class with a count of 4? I am fairly new to this project and modbus (started today) - but that is what I am doing in my code. I basically passed in the start register address and count of 4 and the code read the next 3 registers. The net.wimpi.modbus.cmd.AITest class shows how to do this.
Not sure about the writing part yet - still exploring...:)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
There's no standard way of reading or writing floating point values. You'd have to find out what the byte order and floating point format for your slave device is, so you can marshall the data correctly. Once you've done that, you'd populate enough 16-bit "short" values with the byte representation, then do the WRITE HOLDING REGISTER request. Just remember that "input" and "holding" registers are in a different Modbus address space, so you need to make sure your READ request is correct.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Here is what I used. You call ModbusCOnvUtil.wrap(response) around the response. I only filled in a couple of conversions, and the byte ordering is for a Modicon M340. As has been pointed out here there isn't really a "standard" but this byte ordering works most places I have needed it. Hope it works fo ryou.
Hi,
I am trying to write a float value to a 984-265 quantum using the jamod library.
I cannot seem to find really good documentation/code regarding this. As such I have two requests.
1)I was hoping to find a decent example of writing a hold register(any hold register)
2) I was hoping to find out if its possible to actually write a float directly.
Below is the code I am currently using to try and read a 4x register and i get an error code 2 or i get a 0 every time.
import java.io.;
import net.wimpi.modbus.;
import net.wimpi.modbus.msg.;
import net.wimpi.modbus.io.;
import net.wimpi.modbus.net.;
import net.wimpi.modbus.util.;
public class NRFCutterHoldingRead
{
public static void main(String[] args) {
TCPMasterConnection con = null; //the connection
ModbusTCPTransaction trans = null; //the transaction
ReadMultipleRegistersRequest req = null; //the request
ReadMultipleRegistersResponse res = null; //the response
try {
String astr = "10.80.46.2"; //Modbus Device
}
Any help or guidance would be appreciated.
Last edit: Greg Cushing 2016-01-06
Regarding reading the data, can you not do it by using the ReadInputRegistersRequest class with a count of 4? I am fairly new to this project and modbus (started today) - but that is what I am doing in my code. I basically passed in the start register address and count of 4 and the code read the next 3 registers. The net.wimpi.modbus.cmd.AITest class shows how to do this.
Not sure about the writing part yet - still exploring...:)
There's no standard way of reading or writing floating point values. You'd have to find out what the byte order and floating point format for your slave device is, so you can marshall the data correctly. Once you've done that, you'd populate enough 16-bit "short" values with the byte representation, then do the WRITE HOLDING REGISTER request. Just remember that "input" and "holding" registers are in a different Modbus address space, so you need to make sure your READ request is correct.
Here is what I used. You call ModbusCOnvUtil.wrap(response) around the response. I only filled in a couple of conversions, and the byte ordering is for a Modicon M340. As has been pointed out here there isn't really a "standard" but this byte ordering works most places I have needed it. Hope it works fo ryou.