Java Jamod - Writing to multiple registers

J

Thread Starter

James

Hello,

I am fairly new to Eclipse and Java, however I have an application at work where I need to read about 2000 modbus registers from one of our PLC's, and store it in a text file. I then need another program to write these register values back to the PLC on request.

I have the reading from the PLC into a file working correctly using the Jamod library, however I can not get the writing working. I cant find any examples of write multiple registers either.

Basically I am just wondering if anyone knows of anywhere there is a specific example of using the "WriteMultipleRegistersRequest" function/method/class etc, that would be great.

I have the program reading in each line of the file, and breaking the line up into variables. I know the number of lines etc, its just the part of storing each of the variables into a usable format for this WriteMultipleRegistersRequest function to use, I am having problems with.

I have defined a Register[] array, and created a new array, however I am not sure how to populate each position of the array with the integers I have from my file.
I assume that once I have done this, I basically can run the trasaction, and it will write the contents of the array to the PLC, starting at the modbus address I specify - and stopping when it reaches the end of the array?

If anyone is able to help and show me how I can populate the array, or whatever I need to populate, with each integer I am reading off the file, that would be a huge help.

Do I even need to define a Register[] array? If not, how do I put my values into something usable for this WriteMultipleRegistersRequest to use?

Any help would be appreciated. I have only done a touch of java when I was at uni, and havent used it for about 7 years.

Regards
James
 
I'm not a Java expert, but I've looked at the Jamod package before. If you download the source package, there are a couple of example programs called "AOTest.java" and "AIAOTest.java". Those demonstrates WriteSingleRegisterRequest rather than WriteMultipleRegistersRequest, but there is quite a bit there to help guide you. Unfortunately Jamod uses auto-generated documentation, so there is a lot of formal boilerplate but not much in the way of examples.

You do need to define a Register array, because WriteMultipleRegistersRequest wants to see one as a parameter. I believe you'll have to call the setter on each element to set the register values.

http://jamod.sourceforge.net/apidocs/net/wimpi/modbus/msg/WriteMultipleRegistersRequest.html

http://jamod.sourceforge.net/apidocs/net/wimpi/modbus/procimg/Register.html

Are you working with Modbus/TCP (Ethernet) or Modbus/RTU (RS-232 or RS-485)? If you are working with Modbus/TCP then I have some Python libraries which would be a lot easier to work with than Java. This sort of thing is very easy to do in Python with a fraction of the code that it would take to do in Java or C#.
http://sourceforge.net/projects/mblogic
 
Hello,

My name is Andrei and I am new with Eclipse and java and I'm trying to read some register from a PLC and i saw that you manage to do that so I need some help. If you could help me James I would be very grateful. My e-mail is [email protected] or you can reply here.

Regards,
Andrei.
 
I
<pre>WriteSingleRegisterRequest WriteReq = null;
WriteSingleRegisterResponse WriteRes = null;
SimpleRegister MyReg = new SimpleRegister(1);

String astr = args[0];
int idx = astr.indexOf(':');
if(idx > 0)
{
port = Integer.parseInt(astr.substring(idx+1));
astr = astr.substring(0,idx);
}
addr = InetAddress.getByName(astr);
ref = Integer.decode(args[1]).intValue();
count = Integer.decode(args[2]).intValue();

//2. Open the connection
con = new TCPMasterConnection(addr);
con.setPort(port);
con.connect();

//3. Prepare the request
WriteReq = new WriteSingleRegisterRequest();
WriteReq.setReference(0); //register number
MyReg.setValue(5); //value for register
WriteReq.setRegister(MyReg);

//4. Prepare the transaction
trans = new ModbusTCPTransaction(con);
//trans.setRequest(ReadReq);
trans.setRequest(WriteReq);

trans.execute();</pre>
 
My name is Linda and I am new with Eclipse and java. and I'm trying to read some register from a PLC and to write also. and i saw that you have managed to do that, so I need some help.

Can you help me please. My e-mail is [email protected].


 
Hi,

I am a student and i do my training project. It concerns automate: after synchronising them i have to supervise them. So, that's why i have to read about 1024 modbus registers from one of my PLC's then store them in a BDD to supervise them.

I don't know from where i begin. Could any person help me please? I would be very grateful.

Thanks.
My email is [email protected]
 
Top