Writing to Modbus with Jamod

I came across a curious situation when using jamod to write to modbus. The following reading code works fine:

public static void main(String[] args) throws Exception { InetAddress address = InetAddress.getByName("host.somewhere"); TCPMasterConnection connection = new TCPMasterConnection(address); connection.setPort(502); connection.connect(); ReadMultipleRegistersRequest request = new ReadMultipleRegistersRequest(0, 1); ReadMultipleRegistersResponse response = (ReadMultipleRegistersResponse) executeTransaction(connection, request); } private static ModbusResponse executeTransaction(TCPMasterConnection connection, ModbusRequest request) throws ModbusIOException, ModbusSlaveException, ModbusException { ModbusTCPTransaction transaction = new ModbusTCPTransaction(connection); transaction.setRequest(request); transaction.execute(); return transaction.getResponse(); } 

But an attempt to write a similar method does not work (Jamod tries 3 times, each time it encounters a SocketTimeoutException and finally throws a ModbusException).

 public static void main(String[] args) throws Exception { final InetAddress address = InetAddress.getByName("host.somewhere"); final TCPMasterConnection connection = new TCPMasterConnection(address); connection.setPort(502); connection.connect(); Register reg = new SimpleRegister(0); WriteMultipleRegistersRequest request = new WriteMultipleRegistersRequest(0, new Register[]{reg}); executeTransaction(connection, request); } 

Yes, I know that I use multi-disk versions of request objects, but the device I'm working with only supports function codes 3 and 16.

I also wrote a raw-socket tester to write registers, and as far as I can see, it works correctly. But it would be nice to use jamod in both situations.

Does anyone have experience using jamod and will he be kind enough to say what I'm doing wrong? This happens with jamod versions 1.1 and 1.2rc1. Or is it, perhaps, a specific situation with the seller?

+4
source share
5 answers

In the end, I wrote my own implementation of modbus. I only needed to support 2 different functional codes, so the implementation was simple.

Although later I found another open source modbus library for Java. I have someone else facing the same problem, using modbus4j can help.

+2
source

Modbus requests have a device identifier id = 0 by default. Therefore, any other identifier must be set for the request, for example:

 WriteCoilRequest writeCoilRequest = new WriteCoilRequest(ref, bool); writeCoilRequest.setUnitID(unitid); 

Spent a few hours trying to solve the same problem that you described in the question.

+1
source

My method, which I wrote based on your question, works!

 try { ModbusTCPTransaction trans = null; // the transaction String refe = "0";// HEX Address int ref = Integer.parseInt(refe, 16);// Hex to int // int count = 1; // the number Address to read ReadMultipleRegistersRequest ainReq = new ReadMultipleRegistersRequest( ref, count); ReadMultipleRegistersResponse ainRes = new ReadMultipleRegistersResponse(); // 3. Start Transaction trans = new ModbusTCPTransaction(con); trans.setRetries(5); trans.setReconnecting(true); trans.setRequest(ainReq); int k = 0; do { trans.execute(); ainRes = (ReadMultipleRegistersResponse) trans.getResponse(); Register reg = new SimpleRegister(ertekInt); WriteMultipleRegistersRequest request = new WriteMultipleRegistersRequest(0, new Register[]{reg}); System.out.println("Állított AOUT: " + request.getRegisterValue(k)); lista.add(createPlanet("AOUT", "" + k + " " + request.getRegisterValue(k))); k++; } while (k < count); } catch (ModbusIOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ModbusSlaveException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ModbusException e) { // TODO Auto-generated catch block e.printStackTrace(); } 
0
source

I had a similar problem, I tried to write the value to the register, which was defined in the "job device" as a coil register. Therefore, I used:

 WriteCoilRequest coil_req = new WriteCoilRequest(registerReference,value2write) 

and this solved the problem. Maybe this help.

Bye!

0
source

I ran into this problem on Android.

Since this task can take some considerable time, since it is waiting for a response from an external device, the solution I found was to write and read in another thread.

0
source

All Articles