Modbus Requests and Java

G

Thread Starter

gishac

Hi, I'm trying to read data from a PM500 power meter, I'm using java to get connected, the connection is fine. the problem is I don't know the correct format to send the request to the device, the code is the following:

InetAddress ipaddress = InetAddress.getByAddress("192.168.8.42", new byte[]{(byte)192, (byte)168, (byte)8, (byte)42});
Socket xocket = new Socket(ipaddress, 771);
OutputStream output = xocket.getOutputStream();
BufferedInputStream input = new BufferedInputStream(xocket.getInputStream());
byte buffer[] = new byte[261];
DataOutputStream dos = new DataOutputStream(output);

//Here is the problem, the request I want to do is
// 01 03 03 00 00 62 C4 67, and I don't know how to build the request buffer
dos.writeByte(1); // device id
dos.writeShort(3);// function
dos.writeByte(0x0300);//register to start
dos.writeByte(0x0062); // register to end
dos.writeByte(0xC467); // crc
output.flush();
int totalRead = input.read(buffer, 0, 261);

The response I always get is -1208011111411632105115321111171163211110232114971101031010

Any idea of how to build the request buffer correctly or some code sample? I tried using jamod but I get lot of errors when I send the request, so I decided used my own code.

thanks in advance

gishac
 
J
Hey,

I was able to create a Java Modbus library just by working from the online specs. If you need to understand what the packets contain you should really take a look at them.

It is fairly straight forward to create the packets once you know what they look like.

Let me know if you have questions.
 
Top