Search in sources :

Example 1 with ModbusFieldCoil

use of org.apache.plc4x.java.modbus.field.ModbusFieldCoil 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 ModbusFieldCoil

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

the class ModbusProtocolLogic method getWriteRequestPdu.

private ModbusPDU getWriteRequestPdu(PlcField field, PlcValue plcValue) {
    if (field instanceof ModbusFieldCoil) {
        ModbusFieldCoil coil = (ModbusFieldCoil) field;
        ModbusPDUWriteMultipleCoilsRequest request = new ModbusPDUWriteMultipleCoilsRequest(coil.getAddress(), coil.getNumberOfElements(), fromPlcValue(field, plcValue));
        if (request.getQuantity() == coil.getNumberOfElements()) {
            return request;
        } else {
            throw new PlcRuntimeException("Number of requested bytes (" + request.getQuantity() + ") doesn't match number of requested addresses (" + coil.getNumberOfElements() + ")");
        }
    } else if (field instanceof ModbusFieldHoldingRegister) {
        ModbusFieldHoldingRegister holdingRegister = (ModbusFieldHoldingRegister) field;
        ModbusPDUWriteMultipleHoldingRegistersRequest request = new ModbusPDUWriteMultipleHoldingRegistersRequest(holdingRegister.getAddress(), holdingRegister.getLengthWords(), fromPlcValue(field, plcValue));
        if (request.getValue().length == holdingRegister.getLengthWords() * 2) {
            return request;
        } else {
            throw new PlcRuntimeException("Number of requested values (" + request.getValue().length / 2 + ") doesn't match number of requested addresses (" + holdingRegister.getLengthWords() + ")");
        }
    } else if (field instanceof ModbusExtendedRegister) {
        ModbusExtendedRegister extendedRegister = (ModbusExtendedRegister) field;
        int group1Address = extendedRegister.getAddress() % FC_EXTENDED_REGISTERS_FILE_RECORD_LENGTH;
        int group2Address = 0;
        int group1Quantity;
        int group2Quantity;
        byte[] plcValue1, plcValue2;
        short group1FileNumber = (short) (Math.floor((float) extendedRegister.getAddress() / FC_EXTENDED_REGISTERS_FILE_RECORD_LENGTH) + 1);
        short group2FileNumber;
        List<ModbusPDUWriteFileRecordRequestItem> 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();
            ModbusPDUWriteFileRecordRequestItem group1 = new ModbusPDUWriteFileRecordRequestItem((short) 6, group1FileNumber, group1Address, fromPlcValue(field, plcValue));
            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);
            plcValue1 = ArrayUtils.subarray(fromPlcValue(field, plcValue), 0, group1Quantity);
            plcValue2 = ArrayUtils.subarray(fromPlcValue(field, plcValue), group1Quantity, fromPlcValue(field, plcValue).length);
            ModbusPDUWriteFileRecordRequestItem group1 = new ModbusPDUWriteFileRecordRequestItem((short) 6, group1FileNumber, group1Address, plcValue1);
            ModbusPDUWriteFileRecordRequestItem group2 = new ModbusPDUWriteFileRecordRequestItem((short) 6, group2FileNumber, group2Address, plcValue2);
            itemArray = Arrays.asList(group1, group2);
        }
        return new ModbusPDUWriteFileRecordRequest(itemArray);
    }
    throw new PlcRuntimeException("Unsupported write field type " + field.getClass().getName());
}
Also used : PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) 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 3 with ModbusFieldCoil

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

the class ModbusEncodeTest method testEncodeBooleanBOOL.

@Test
public void testEncodeBooleanBOOL() {
    Boolean[] object = { true, false, true, false, true, false, true, true, false };
    ModbusFieldCoil coils = ModbusFieldCoil.of("coil:8:BOOL");
    PlcList list = (PlcList) IEC61131ValueHandler.of(coils, object);
    Assertions.assertEquals("[true,false,true,false,true,false,true,true,false]", list.toString());
}
Also used : PlcList(org.apache.plc4x.java.spi.values.PlcList) ModbusFieldCoil(org.apache.plc4x.java.modbus.field.ModbusFieldCoil) Test(org.junit.jupiter.api.Test)

Example 4 with ModbusFieldCoil

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

the class ModbusFieldDataTypeTest method testCoil_DataType.

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

Example 5 with ModbusFieldCoil

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

the class ModbusFieldTest method testCoil_INT_ARRAY_RANGE.

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

Aggregations

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