Ejemplo n.º 1
0
  /**
   * Read one value of the slave
   *
   * @param address Address of Memory of value in slave
   * @return the read value
   */
  public int sendReadRequest(int address) throws Exception {

    ReadInputRegistersResponse response = null;
    short tmp_value = 0;

    try {
      ReadInputRegistersRequest request = new ReadInputRegistersRequest(address, 1);
      response = (ReadInputRegistersResponse) this.sendRequest(request);
      tmp_value = response.getRegister(0).toShort();
      if (tmp_value == 0x7FFF) throw new Exception("0x7FFF received - value ignored");
    } catch (Exception ex) {
      log.warning("Error on reading value: " + ex.getMessage());
      throw ex;
    }
    return tmp_value;
  }
  public ModbusResponse createResponse() {
    ReadInputRegistersResponse response = null;
    InputRegister[] inpregs = null;

    // 1. get process image
    ProcessImage procimg = ModbusCoupler.getReference().getProcessImage();
    // 2. get input registers range
    try {
      inpregs = procimg.getInputRegisterRange(this.getReference(), this.getWordCount());
    } catch (IllegalAddressException iaex) {
      return createExceptionResponse(Modbus.ILLEGAL_ADDRESS_EXCEPTION);
    }
    response = new ReadInputRegistersResponse(inpregs);
    // transfer header data
    if (!isHeadless()) {
      response.setTransactionID(this.getTransactionID());
      response.setProtocolID(this.getProtocolID());
    } else {
      response.setHeadless();
    }
    response.setUnitID(this.getUnitID());
    response.setFunctionCode(this.getFunctionCode());
    return response;
  } // createResponse