Search in sources :

Example 1 with ModbusFieldDiscreteInput

use of org.apache.plc4x.java.modbus.field.ModbusFieldDiscreteInput in project plc4x by apache.

the class ModbusProtocolLogic method getReadRequestPdu.

private ModbusPDU getReadRequestPdu(PlcField field) {
    if (field instanceof ModbusFieldDiscreteInput) {
        ModbusFieldDiscreteInput discreteInput = (ModbusFieldDiscreteInput) field;
        return new ModbusPDUReadDiscreteInputsRequest(discreteInput.getAddress(), discreteInput.getNumberOfElements());
    } else if (field instanceof ModbusFieldCoil) {
        ModbusFieldCoil coil = (ModbusFieldCoil) field;
        return new ModbusPDUReadCoilsRequest(coil.getAddress(), coil.getNumberOfElements());
    } else if (field instanceof ModbusFieldInputRegister) {
        ModbusFieldInputRegister inputRegister = (ModbusFieldInputRegister) field;
        return new ModbusPDUReadInputRegistersRequest(inputRegister.getAddress(), Math.max(inputRegister.getLengthWords(), 1));
    } else if (field instanceof ModbusFieldHoldingRegister) {
        ModbusFieldHoldingRegister holdingRegister = (ModbusFieldHoldingRegister) field;
        return new ModbusPDUReadHoldingRegistersRequest(holdingRegister.getAddress(), Math.max(holdingRegister.getLengthWords(), 1));
    } else if (field instanceof ModbusExtendedRegister) {
        ModbusExtendedRegister extendedRegister = (ModbusExtendedRegister) field;
        int group1Address = extendedRegister.getAddress() % 10000;
        int group2Address = 0;
        int group1Quantity;
        int group2Quantity;
        short group1FileNumber = (short) (Math.floor((float) extendedRegister.getAddress() / 10000) + 1);
        short group2FileNumber;
        List<ModbusPDUReadFileRecordRequestItem> itemArray;
        if ((group1Address + extendedRegister.getLengthWords()) <= FC_EXTENDED_REGISTERS_FILE_RECORD_LENGTH) {
            // If request doesn't span file records, use a single group
            group1Quantity = extendedRegister.getLengthWords();
            ModbusPDUReadFileRecordRequestItem group1 = new ModbusPDUReadFileRecordRequestItem((short) 6, group1FileNumber, group1Address, group1Quantity);
            itemArray = Collections.singletonList(group1);
        } else {
            // If it doesn't span a file record. e.g. 609998[10] request 2 words in first group and 8 in second.
            group1Quantity = FC_EXTENDED_REGISTERS_FILE_RECORD_LENGTH - group1Address;
            group2Quantity = extendedRegister.getLengthWords() - group1Quantity;
            group2FileNumber = (short) (group1FileNumber + 1);
            ModbusPDUReadFileRecordRequestItem group1 = new ModbusPDUReadFileRecordRequestItem((short) 6, group1FileNumber, group1Address, group1Quantity);
            ModbusPDUReadFileRecordRequestItem group2 = new ModbusPDUReadFileRecordRequestItem((short) 6, group2FileNumber, group2Address, group2Quantity);
            itemArray = Arrays.asList(group1, group2);
        }
        return new ModbusPDUReadFileRecordRequest(itemArray);
    }
    throw new PlcRuntimeException("Unsupported read field type " + field.getClass().getName());
}
Also used : PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) ModbusFieldDiscreteInput(org.apache.plc4x.java.modbus.field.ModbusFieldDiscreteInput) ModbusFieldInputRegister(org.apache.plc4x.java.modbus.field.ModbusFieldInputRegister) ModbusFieldHoldingRegister(org.apache.plc4x.java.modbus.field.ModbusFieldHoldingRegister) ModbusExtendedRegister(org.apache.plc4x.java.modbus.field.ModbusExtendedRegister) ModbusFieldCoil(org.apache.plc4x.java.modbus.field.ModbusFieldCoil)

Example 2 with ModbusFieldDiscreteInput

use of org.apache.plc4x.java.modbus.field.ModbusFieldDiscreteInput in project plc4x by apache.

the class ModbusFieldDataTypeTest method testDiscreteInput_DataType.

@Test
public void testDiscreteInput_DataType() {
    // Datatype, Length in Bytes
    String[][] datatypes = { { "BOOL", "1" } };
    for (int i = 0; i < datatypes.length; i++) {
        ModbusFieldDiscreteInput discrete = ModbusFieldDiscreteInput.of("discrete-input:1:" + datatypes[i][0]);
        Assertions.assertEquals(datatypes[i][0], discrete.getDataType().name());
        Assertions.assertEquals(1, discrete.getNumberOfElements());
    }
}
Also used : ModbusFieldDiscreteInput(org.apache.plc4x.java.modbus.field.ModbusFieldDiscreteInput) Test(org.junit.jupiter.api.Test)

Example 3 with ModbusFieldDiscreteInput

use of org.apache.plc4x.java.modbus.field.ModbusFieldDiscreteInput in project plc4x by apache.

the class ModbusFieldTest method testDiscreteInput_INT_ARRAY_RANGE.

@Test
public void testDiscreteInput_INT_ARRAY_RANGE() {
    for (int i = 1; i < 2000; i++) {
        final ModbusFieldDiscreteInput discreteInput = ModbusFieldDiscreteInput.of("100001:BOOL[" + i + "]");
        Assertions.assertEquals(i, discreteInput.getNumberOfElements());
    }
}
Also used : ModbusFieldDiscreteInput(org.apache.plc4x.java.modbus.field.ModbusFieldDiscreteInput) Test(org.junit.jupiter.api.Test)

Aggregations

ModbusFieldDiscreteInput (org.apache.plc4x.java.modbus.field.ModbusFieldDiscreteInput)3 Test (org.junit.jupiter.api.Test)2 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)1 ModbusExtendedRegister (org.apache.plc4x.java.modbus.field.ModbusExtendedRegister)1 ModbusFieldCoil (org.apache.plc4x.java.modbus.field.ModbusFieldCoil)1 ModbusFieldHoldingRegister (org.apache.plc4x.java.modbus.field.ModbusFieldHoldingRegister)1 ModbusFieldInputRegister (org.apache.plc4x.java.modbus.field.ModbusFieldInputRegister)1