Modbu4J, Help for read register
-
Hi,
thanks for your library.. Have you an sample to read a marker (word) in a plc?
Thanks -
Here my code:
public static void main(String[] args) { ModbusFactory m = new ModbusFactory(); IpParameters ip = new IpParameters(); ip.setHost("127.0.0.1"); ip.setPort(502); ModbusMaster mp = m.createTcpMaster(ip, false); try { mp.init(); ModbusRequest req = new ReadInputRegistersRequest(1, 0, 1); ReadInputRegistersResponse rep = (ReadInputRegistersResponse) mp.send(req); System.out.println(rep.getData()); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } }
I've this exception:
Exception in thread "main" java.lang.NoSuchMethodError: com.serotonin.io.messaging.StreamTransport.<init>(Ljava/io/InputStream;Ljava/io/OutputStream;)V at com.serotonin.modbus4j.ip.tcp.TcpMaster.openConnection(TcpMaster.java:113) at com.serotonin.modbus4j.ip.tcp.TcpMaster.send(TcpMaster.java:62) at javaapplication6.Main.main(Main.java:45)
Thanks for your furture help
-
Is seroUtils.jar in your classpath?
-
Ok its works..
my code :public static void main(String[] args) { ModbusFactory m = new ModbusFactory(); IpParameters ip = new IpParameters(); ip.setHost("192.168.10.2"); ip.setPort(502); ModbusMaster mp = m.createUdpMaster(ip); try { mp.init(); ModbusRequest req = new WriteRegisterRequest(1,0,1800); mp.send(req); ModbusRequest req2 = new ReadHoldingRegistersRequest(1,0,1); ReadResponse rep = (ReadResponse) mp.send(req2); System.out.println(rep.getShortData()[0]); } catch (Exception ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } }
in my classpath:
axis.jar
bacnet.jar
modbus4j.jar
RTXXcomm.jar
serioUtil.jarAll this library are necessary?
It's not possible to get direct the value short double string? In the good typage?
Thanks four your help and for your job, it's great and its works under windows and linux, before I've tried UDP under linux with jamod and there is serious problems..
It's should be very interessant if the necessary library are of sourceforge.net..
For the novice, a little tutorial should be nice..
-
You shouldn't need bacnet.jar and axis.jar. And if you're using UDP you shouldn't need RXTX either.
Regarding your code, you should be able to simplify it a bit. ModbusMaster has a number of "getValue" and "setValue" convenience methods. For example, here's something you could do:
// Define the point locator. ModbusLocator loc = new ModbusLocator(1, RegisterRange.HOLDING_REGISTER, 0, DataType.TWO_BYTE_INT_UNSIGNED); // Set the point value master.setValue(loc, 1800); // Get the point value System.out.println(master.getValue(loc));
-
Thanks!