Beispiel #1
0
  public static void main(String[] args) throws Exception {
    IpParameters tcpParameters = new IpParameters();
    tcpParameters.setHost("localhost");

    ModbusFactory modbusFactory = new ModbusFactory();
    ModbusMaster master = modbusFactory.createTcpMaster(tcpParameters, true);

    try {
      BatchRead<String> batchRead = new BatchRead<String>();
      int slaveId = 31;
      batchRead.addLocator(
          "00011 sb true", slaveId, RegisterRange.COIL_STATUS, 10, DataType.BINARY);
      batchRead.addLocator("00012 sb false", slaveId, 12, DataType.BINARY);
      batchRead.addLocator(
          "00013 sb true", slaveId, RegisterRange.COIL_STATUS, 12, DataType.BINARY);
      batchRead.addLocator("00014 sb true", slaveId, 14, DataType.BINARY);

      batchRead.addLocator(
          "10011 sb false", slaveId, RegisterRange.INPUT_STATUS, 10, DataType.BINARY);
      batchRead.addLocator("10012 sb false", slaveId, 10012, DataType.BINARY);
      batchRead.addLocator(
          "10013 sb true", slaveId, RegisterRange.INPUT_STATUS, 12, DataType.BINARY);
      batchRead.addLocator("10014 sb false", slaveId, 10014, DataType.BINARY);

      batchRead.addLocator("40016-0 sb true", slaveId, 40016, (byte) 0);
      batchRead.addLocator("40016-1 sb false", slaveId, 40016, (byte) 1);
      batchRead.addLocator("40016-2 sb false", slaveId, 40016, (byte) 2);
      batchRead.addLocator("40016-3 sb true", slaveId, 40016, (byte) 3);
      batchRead.addLocator("40016-4 sb false", slaveId, 40016, (byte) 4);
      batchRead.addLocator("40016-5 sb false", slaveId, 40016, (byte) 5);
      batchRead.addLocator("40016-6 sb false", slaveId, 40016, (byte) 6);
      batchRead.addLocator("40016-7 sb true", slaveId, 40016, (byte) 7);
      batchRead.addLocator("40016-8 sb true", slaveId, 40016, (byte) 8);
      batchRead.addLocator("40016-9 sb false", slaveId, 40016, (byte) 9);
      batchRead.addLocator("40016-a sb false", slaveId, 40016, (byte) 10);
      batchRead.addLocator("40016-b sb false", slaveId, 40016, (byte) 11);
      batchRead.addLocator("40016-c sb false", slaveId, 40016, (byte) 12);
      batchRead.addLocator("40016-d sb false", slaveId, 40016, (byte) 13);
      batchRead.addLocator("40016-e sb true", slaveId, 40016, (byte) 14);
      batchRead.addLocator("40016-f sb false", slaveId, 40016, (byte) 15);

      batchRead.addLocator("30016-0 sb true", slaveId, 30016, (byte) 0);
      batchRead.addLocator("30016-1 sb false", slaveId, 30016, (byte) 1);
      batchRead.addLocator("30016-2 sb false", slaveId, 30016, (byte) 2);
      batchRead.addLocator("30016-3 sb false", slaveId, 30016, (byte) 3);
      batchRead.addLocator("30016-4 sb false", slaveId, 30016, (byte) 4);
      batchRead.addLocator("30016-5 sb false", slaveId, 30016, (byte) 5);
      batchRead.addLocator("30016-6 sb false", slaveId, 30016, (byte) 6);
      batchRead.addLocator("30016-7 sb true", slaveId, 30016, (byte) 7);
      batchRead.addLocator("30016-8 sb true", slaveId, 30016, (byte) 8);
      batchRead.addLocator("30016-9 sb false", slaveId, 30016, (byte) 9);
      batchRead.addLocator("30016-a sb false", slaveId, 30016, (byte) 10);
      batchRead.addLocator("30016-b sb false", slaveId, 30016, (byte) 11);
      batchRead.addLocator("30016-c sb false", slaveId, 30016, (byte) 12);
      batchRead.addLocator("30016-d sb false", slaveId, 30016, (byte) 13);
      batchRead.addLocator("30016-e sb false", slaveId, 30016, (byte) 14);
      batchRead.addLocator("30016-f sb true", slaveId, 30016, (byte) 15);

      batchRead.addLocator("40017 sb -1968", slaveId, 40017, DataType.TWO_BYTE_INT_SIGNED);
      batchRead.addLocator("40018 sb -123456789", slaveId, 40018, DataType.FOUR_BYTE_INT_SIGNED);
      batchRead.addLocator(
          "40020 sb -123456789", slaveId, 40020, DataType.FOUR_BYTE_INT_SIGNED_SWAPPED);
      batchRead.addLocator("40022 sb 1968.1968", slaveId, 40022, DataType.FOUR_BYTE_FLOAT);
      batchRead.addLocator("40024 sb -123456789", slaveId, 40024, DataType.EIGHT_BYTE_INT_SIGNED);
      batchRead.addLocator(
          "40028 sb -123456789", slaveId, 40028, DataType.EIGHT_BYTE_INT_SIGNED_SWAPPED);
      batchRead.addLocator("40032 sb 1968.1968", slaveId, 40032, DataType.EIGHT_BYTE_FLOAT);

      batchRead.addLocator("30017 sb -1968 tc", slaveId, 30017, DataType.TWO_BYTE_INT_UNSIGNED);
      batchRead.addLocator(
          "30018 sb -123456789 tc", slaveId, 30018, DataType.FOUR_BYTE_INT_UNSIGNED);
      batchRead.addLocator(
          "30020 sb -123456789 tc", slaveId, 30020, DataType.FOUR_BYTE_INT_UNSIGNED_SWAPPED);
      batchRead.addLocator("30022 sb 1968.1968", slaveId, 30022, DataType.FOUR_BYTE_FLOAT_SWAPPED);
      batchRead.addLocator(
          "30024 sb -123456789 tc", slaveId, 30024, DataType.EIGHT_BYTE_INT_UNSIGNED);
      batchRead.addLocator(
          "30028 sb -123456789 tc", slaveId, 30028, DataType.EIGHT_BYTE_INT_UNSIGNED_SWAPPED);
      batchRead.addLocator("30032 sb 1968.1968", slaveId, 30032, DataType.EIGHT_BYTE_FLOAT_SWAPPED);

      master.init();

      BatchResults<String> results = master.send(batchRead);

      System.out.println(results);
    } finally {
      master.destroy();
    }
  }
Beispiel #2
0
  public static void main(String[] args) throws Exception {
    IpParameters ipParameters = new IpParameters();
    // ipParameters.setHost("99.247.60.96");
    // ipParameters.setHost("193.109.41.121");
    ipParameters.setHost("141.211.194.29");
    ipParameters.setPort(502);

    ModbusFactory modbusFactory = new ModbusFactory();
    // ModbusMaster master = modbusFactory.createTcpMaster(ipParameters, true);
    ModbusMaster master = modbusFactory.createTcpMaster(ipParameters, false);
    master.setTimeout(4000);
    master.setRetries(1);

    BatchRead<Integer> batch = new BatchRead<Integer>();
    batch.addLocator(0, new ModbusLocator(1, RegisterRange.COIL_STATUS, 2048, DataType.BINARY));
    batch.addLocator(1, new ModbusLocator(1, RegisterRange.COIL_STATUS, 2049, DataType.BINARY));
    batch.addLocator(2, new ModbusLocator(1, RegisterRange.COIL_STATUS, 2050, DataType.BINARY));
    batch.addLocator(3, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3584, DataType.BINARY));
    batch.addLocator(4, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3585, DataType.BINARY));
    batch.addLocator(5, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3648, DataType.BINARY));
    batch.addLocator(6, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3649, DataType.BINARY));
    batch.addLocator(7, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3650, DataType.BINARY));
    batch.addLocator(8, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3651, DataType.BINARY));
    batch.addLocator(9, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3652, DataType.BINARY));
    batch.addLocator(10, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3653, DataType.BINARY));
    batch.addLocator(11, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3654, DataType.BINARY));
    batch.addLocator(12, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3661, DataType.BINARY));
    batch.addLocator(13, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3662, DataType.BINARY));
    batch.addLocator(15, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3665, DataType.BINARY));
    batch.addLocator(16, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3668, DataType.BINARY));
    batch.addLocator(18, new ModbusLocator(1, RegisterRange.COIL_STATUS, 3969, DataType.BINARY));

    try {
      master.init();

      while (true) {
        batch.setContiguousRequests(false);
        master.send(batch);

        Thread.sleep(2000);
      }
    } catch (ErrorResponseException e) {
      System.out.println(e.getErrorResponse().getExceptionMessage());
    } finally {
      master.destroy();
    }
  }