MasterTest2.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. Copyright (C) 2006-2007 Serotonin Software Technologies Inc.
  3. @author Matthew Lohbihler
  4. */
  5. package com.serotonin.modbus4j.test;
  6. import com.serotonin.modbus4j.BatchRead;
  7. import com.serotonin.modbus4j.BatchResults;
  8. import com.serotonin.modbus4j.ModbusFactory;
  9. import com.serotonin.modbus4j.ModbusMaster;
  10. import com.serotonin.modbus4j.code.DataType;
  11. import com.serotonin.modbus4j.exception.ErrorResponseException;
  12. import com.serotonin.modbus4j.ip.IpParameters;
  13. import com.serotonin.modbus4j.locator.BaseLocator;
  14. /**
  15. * @author Matthew Lohbihler
  16. */
  17. public class MasterTest2 {
  18. public static void main(String[] args) throws Exception {
  19. IpParameters ipParameters = new IpParameters();
  20. ipParameters.setHost("localhost");
  21. // ipParameters.setHost("99.247.60.96");
  22. // ipParameters.setHost("193.109.41.121");
  23. //ipParameters.setHost("141.211.194.29");
  24. ipParameters.setPort(502);
  25. ModbusFactory modbusFactory = new ModbusFactory();
  26. // ModbusMaster master = modbusFactory.createTcpMaster(ipParameters, true);
  27. ModbusMaster master = modbusFactory.createTcpMaster(ipParameters, false);
  28. master.setTimeout(4000);
  29. master.setRetries(1);
  30. BatchRead<Integer> batch = new BatchRead<Integer>();
  31. // batch.addLocator(0, new ModbusLocator(1, RegisterRange.COIL_STATUS, 2048, DataType.BINARY));
  32. // batch.addLocator(1, new ModbusLocator(1, RegisterRange.COIL_STATUS, 2049, DataType.BINARY));
  33. // batch.addLocator(2, new ModbusLocator(1, RegisterRange.COIL_STATUS, 2050, DataType.BINARY));
  34. // batch.addLocator(3, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3584, DataType.BINARY));
  35. // batch.addLocator(4, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3585, DataType.BINARY));
  36. // batch.addLocator(5, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3648, DataType.BINARY));
  37. // batch.addLocator(6, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3649, DataType.BINARY));
  38. // batch.addLocator(7, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3650, DataType.BINARY));
  39. // batch.addLocator(8, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3651, DataType.BINARY));
  40. // batch.addLocator(9, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3652, DataType.BINARY));
  41. // batch.addLocator(10, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3653, DataType.BINARY));
  42. // batch.addLocator(11, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3654, DataType.BINARY));
  43. // batch.addLocator(12, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3661, DataType.BINARY));
  44. // batch.addLocator(13, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3662, DataType.BINARY));
  45. // batch.addLocator(15, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3665, DataType.BINARY));
  46. // batch.addLocator(16, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3668, DataType.BINARY));
  47. // batch.addLocator(18, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3969, DataType.BINARY));
  48. batch.addLocator(0, BaseLocator.holdingRegister(5, 80, DataType.TWO_BYTE_INT_SIGNED));
  49. batch.addLocator(1, BaseLocator.holdingRegister(5, 202, DataType.EIGHT_BYTE_INT_SIGNED));
  50. try {
  51. master.init();
  52. while (true) {
  53. batch.setContiguousRequests(false);
  54. BatchResults<Integer> results = master.send(batch);
  55. System.out.println(results.getValue(0));
  56. System.out.println(results.getValue(1));
  57. Thread.sleep(2000);
  58. }
  59. }
  60. catch (ErrorResponseException e) {
  61. System.out.println(e.getErrorResponse().getExceptionMessage());
  62. }
  63. finally {
  64. master.destroy();
  65. }
  66. }
  67. }