Search in sources :

Example 1 with ModbusField

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

the class ModbusRtuProtocolLogic method read.

@Override
public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
    CompletableFuture<PlcReadResponse> future = new CompletableFuture<>();
    DefaultPlcReadRequest request = (DefaultPlcReadRequest) readRequest;
    // Example for sending a request ...
    if (request.getFieldNames().size() == 1) {
        String fieldName = request.getFieldNames().iterator().next();
        ModbusField field = (ModbusField) request.getField(fieldName);
        final ModbusPDU requestPdu = getReadRequestPdu(field);
        ModbusRtuADU modbusRtuADU = new ModbusRtuADU(unitIdentifier, requestPdu, false);
        RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
        transaction.submit(() -> context.sendRequest(modbusRtuADU).expectResponse(ModbusRtuADU.class, requestTimeout).onTimeout(future::completeExceptionally).onError((p, e) -> future.completeExceptionally(e)).unwrap(ModbusRtuADU::getPdu).handle(responsePdu -> {
            // Try to decode the response data based on the corresponding request.
            PlcValue plcValue = null;
            PlcResponseCode responseCode;
            // Check if the response was an error response.
            if (responsePdu instanceof ModbusPDUError) {
                ModbusPDUError errorResponse = (ModbusPDUError) responsePdu;
                responseCode = getErrorCode(errorResponse);
            } else {
                try {
                    plcValue = toPlcValue(requestPdu, responsePdu, field.getDataType());
                    responseCode = PlcResponseCode.OK;
                } catch (ParseException e) {
                    // Add an error response code ...
                    responseCode = PlcResponseCode.INTERNAL_ERROR;
                }
            }
            // Prepare the response.
            PlcReadResponse response = new DefaultPlcReadResponse(request, Collections.singletonMap(fieldName, new ResponseItem<>(responseCode, plcValue)));
            // Pass the response back to the application.
            future.complete(response);
            // Finish the request-transaction.
            transaction.endRequest();
        }));
    } else {
        future.completeExceptionally(new PlcRuntimeException("Modbus only supports single filed requests"));
    }
    return future;
}
Also used : PlcWriteResponse(org.apache.plc4x.java.api.messages.PlcWriteResponse) PlcWriteRequest(org.apache.plc4x.java.api.messages.PlcWriteRequest) ParseException(org.apache.plc4x.java.spi.generation.ParseException) ModbusTcpConfiguration(org.apache.plc4x.java.modbus.tcp.config.ModbusTcpConfiguration) ModbusProtocolLogic(org.apache.plc4x.java.modbus.base.protocol.ModbusProtocolLogic) org.apache.plc4x.java.modbus.readwrite(org.apache.plc4x.java.modbus.readwrite) PlcValue(org.apache.plc4x.java.api.value.PlcValue) PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) CompletableFuture(java.util.concurrent.CompletableFuture) PlcField(org.apache.plc4x.java.api.model.PlcField) ModbusField(org.apache.plc4x.java.modbus.base.field.ModbusField) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) ModbusRtuConfiguration(org.apache.plc4x.java.modbus.rtu.config.ModbusRtuConfiguration) HasConfiguration(org.apache.plc4x.java.spi.configuration.HasConfiguration) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) Duration(java.time.Duration) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) DefaultPlcWriteRequest(org.apache.plc4x.java.spi.messages.DefaultPlcWriteRequest) DefaultPlcReadRequest(org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest) Collections(java.util.Collections) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) ModbusField(org.apache.plc4x.java.modbus.base.field.ModbusField) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) CompletableFuture(java.util.concurrent.CompletableFuture) PlcValue(org.apache.plc4x.java.api.value.PlcValue) DefaultPlcReadRequest(org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) ParseException(org.apache.plc4x.java.spi.generation.ParseException)

Example 2 with ModbusField

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

the class ModbusTcpProtocolLogic method read.

@Override
public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
    CompletableFuture<PlcReadResponse> future = new CompletableFuture<>();
    DefaultPlcReadRequest request = (DefaultPlcReadRequest) readRequest;
    // Example for sending a request ...
    if (request.getFieldNames().size() == 1) {
        String fieldName = request.getFieldNames().iterator().next();
        ModbusField field = (ModbusField) request.getField(fieldName);
        final ModbusPDU requestPdu = getReadRequestPdu(field);
        int transactionIdentifier = transactionIdentifierGenerator.getAndIncrement();
        // If we've reached the max value for a 16 bit transaction identifier, reset back to 1
        if (transactionIdentifierGenerator.get() == 0xFFFF) {
            transactionIdentifierGenerator.set(1);
        }
        ModbusTcpADU modbusTcpADU = new ModbusTcpADU(transactionIdentifier, unitIdentifier, requestPdu, false);
        RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
        transaction.submit(() -> context.sendRequest(modbusTcpADU).expectResponse(ModbusTcpADU.class, requestTimeout).onTimeout(future::completeExceptionally).onError((p, e) -> future.completeExceptionally(e)).check(p -> ((p.getTransactionIdentifier() == transactionIdentifier) && (p.getUnitIdentifier() == unitIdentifier))).unwrap(ModbusTcpADU::getPdu).handle(responsePdu -> {
            // Try to decode the response data based on the corresponding request.
            PlcValue plcValue = null;
            PlcResponseCode responseCode;
            // Check if the response was an error response.
            if (responsePdu instanceof ModbusPDUError) {
                ModbusPDUError errorResponse = (ModbusPDUError) responsePdu;
                responseCode = getErrorCode(errorResponse);
            } else {
                try {
                    plcValue = toPlcValue(requestPdu, responsePdu, field.getDataType());
                    responseCode = PlcResponseCode.OK;
                } catch (ParseException e) {
                    // Add an error response code ...
                    responseCode = PlcResponseCode.INTERNAL_ERROR;
                }
            }
            // Prepare the response.
            PlcReadResponse response = new DefaultPlcReadResponse(request, Collections.singletonMap(fieldName, new ResponseItem<>(responseCode, plcValue)));
            // Pass the response back to the application.
            future.complete(response);
            // Finish the request-transaction.
            transaction.endRequest();
        }));
    } else {
        future.completeExceptionally(new PlcRuntimeException("Modbus only supports single filed requests"));
    }
    return future;
}
Also used : PlcWriteResponse(org.apache.plc4x.java.api.messages.PlcWriteResponse) PlcWriteRequest(org.apache.plc4x.java.api.messages.PlcWriteRequest) ParseException(org.apache.plc4x.java.spi.generation.ParseException) ModbusTcpConfiguration(org.apache.plc4x.java.modbus.tcp.config.ModbusTcpConfiguration) ModbusProtocolLogic(org.apache.plc4x.java.modbus.base.protocol.ModbusProtocolLogic) org.apache.plc4x.java.modbus.readwrite(org.apache.plc4x.java.modbus.readwrite) PlcValue(org.apache.plc4x.java.api.value.PlcValue) PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) CompletableFuture(java.util.concurrent.CompletableFuture) PlcField(org.apache.plc4x.java.api.model.PlcField) ModbusField(org.apache.plc4x.java.modbus.base.field.ModbusField) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) ModbusRtuConfiguration(org.apache.plc4x.java.modbus.rtu.config.ModbusRtuConfiguration) HasConfiguration(org.apache.plc4x.java.spi.configuration.HasConfiguration) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) Duration(java.time.Duration) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) DefaultPlcWriteRequest(org.apache.plc4x.java.spi.messages.DefaultPlcWriteRequest) DefaultPlcReadRequest(org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest) Collections(java.util.Collections) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) ModbusField(org.apache.plc4x.java.modbus.base.field.ModbusField) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) CompletableFuture(java.util.concurrent.CompletableFuture) PlcValue(org.apache.plc4x.java.api.value.PlcValue) DefaultPlcReadRequest(org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) ParseException(org.apache.plc4x.java.spi.generation.ParseException)

Example 3 with ModbusField

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

the class ModbusAsciiProtocolLogic method read.

@Override
public CompletableFuture<PlcReadResponse> read(PlcReadRequest readRequest) {
    CompletableFuture<PlcReadResponse> future = new CompletableFuture<>();
    DefaultPlcReadRequest request = (DefaultPlcReadRequest) readRequest;
    // Example for sending a request ...
    if (request.getFieldNames().size() == 1) {
        String fieldName = request.getFieldNames().iterator().next();
        ModbusField field = (ModbusField) request.getField(fieldName);
        final ModbusPDU requestPdu = getReadRequestPdu(field);
        ModbusAsciiADU modbusAsciiADU = new ModbusAsciiADU(unitIdentifier, requestPdu, false);
        RequestTransactionManager.RequestTransaction transaction = tm.startRequest();
        transaction.submit(() -> context.sendRequest(modbusAsciiADU).expectResponse(ModbusAsciiADU.class, requestTimeout).onTimeout(future::completeExceptionally).onError((p, e) -> future.completeExceptionally(e)).unwrap(ModbusAsciiADU::getPdu).handle(responsePdu -> {
            // Try to decode the response data based on the corresponding request.
            PlcValue plcValue = null;
            PlcResponseCode responseCode;
            // Check if the response was an error response.
            if (responsePdu instanceof ModbusPDUError) {
                ModbusPDUError errorResponse = (ModbusPDUError) responsePdu;
                responseCode = getErrorCode(errorResponse);
            } else {
                try {
                    plcValue = toPlcValue(requestPdu, responsePdu, field.getDataType());
                    responseCode = PlcResponseCode.OK;
                } catch (ParseException e) {
                    // Add an error response code ...
                    responseCode = PlcResponseCode.INTERNAL_ERROR;
                }
            }
            // Prepare the response.
            PlcReadResponse response = new DefaultPlcReadResponse(request, Collections.singletonMap(fieldName, new ResponseItem<>(responseCode, plcValue)));
            // Pass the response back to the application.
            future.complete(response);
            // Finish the request-transaction.
            transaction.endRequest();
        }));
    } else {
        future.completeExceptionally(new PlcRuntimeException("Modbus only supports single filed requests"));
    }
    return future;
}
Also used : PlcWriteResponse(org.apache.plc4x.java.api.messages.PlcWriteResponse) PlcWriteRequest(org.apache.plc4x.java.api.messages.PlcWriteRequest) ParseException(org.apache.plc4x.java.spi.generation.ParseException) ModbusAsciiConfiguration(org.apache.plc4x.java.modbus.ascii.config.ModbusAsciiConfiguration) ModbusProtocolLogic(org.apache.plc4x.java.modbus.base.protocol.ModbusProtocolLogic) org.apache.plc4x.java.modbus.readwrite(org.apache.plc4x.java.modbus.readwrite) PlcValue(org.apache.plc4x.java.api.value.PlcValue) PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) CompletableFuture(java.util.concurrent.CompletableFuture) PlcField(org.apache.plc4x.java.api.model.PlcField) ModbusField(org.apache.plc4x.java.modbus.base.field.ModbusField) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) ModbusRtuConfiguration(org.apache.plc4x.java.modbus.rtu.config.ModbusRtuConfiguration) HasConfiguration(org.apache.plc4x.java.spi.configuration.HasConfiguration) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) Duration(java.time.Duration) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) DefaultPlcWriteRequest(org.apache.plc4x.java.spi.messages.DefaultPlcWriteRequest) DefaultPlcReadRequest(org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest) Collections(java.util.Collections) ResponseItem(org.apache.plc4x.java.spi.messages.utils.ResponseItem) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) RequestTransactionManager(org.apache.plc4x.java.spi.transaction.RequestTransactionManager) ModbusField(org.apache.plc4x.java.modbus.base.field.ModbusField) PlcResponseCode(org.apache.plc4x.java.api.types.PlcResponseCode) PlcReadResponse(org.apache.plc4x.java.api.messages.PlcReadResponse) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) CompletableFuture(java.util.concurrent.CompletableFuture) PlcValue(org.apache.plc4x.java.api.value.PlcValue) DefaultPlcReadRequest(org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest) DefaultPlcReadResponse(org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse) ParseException(org.apache.plc4x.java.spi.generation.ParseException)

Example 4 with ModbusField

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

the class ModbusProtocolLogic method fromPlcValue.

protected byte[] fromPlcValue(PlcField field, PlcValue plcValue) {
    ModbusDataType fieldDataType = ((ModbusField) field).getDataType();
    try {
        if (plcValue instanceof PlcList) {
            WriteBufferByteBased writeBuffer = new WriteBufferByteBased(DataItem.getLengthInBytes(plcValue, fieldDataType, plcValue.getLength()));
            DataItem.staticSerialize(writeBuffer, plcValue, fieldDataType, plcValue.getLength(), ByteOrder.BIG_ENDIAN);
            byte[] data = writeBuffer.getData();
            if (((ModbusField) field).getDataType() == ModbusDataType.BOOL) {
                // Reverse Bits in each byte as
                // they should be ordered like this: 8 7 6 5 4 3 2 1 | 0 0 0 0 0 0 0 9
                byte[] bytes = new byte[data.length];
                for (int i = 0; i < data.length; i++) {
                    bytes[i] = reverseBitsOfByte(data[i]);
                }
                return bytes;
            }
            return data;
        } else {
            WriteBufferByteBased writeBuffer = new WriteBufferByteBased(DataItem.getLengthInBytes(plcValue, fieldDataType, plcValue.getLength()));
            DataItem.staticSerialize(writeBuffer, plcValue, fieldDataType, plcValue.getLength(), ByteOrder.BIG_ENDIAN);
            return writeBuffer.getData();
        }
    } catch (SerializationException e) {
        throw new PlcRuntimeException("Unable to parse PlcValue :- " + e);
    }
}
Also used : PlcList(org.apache.plc4x.java.spi.values.PlcList) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) ModbusField(org.apache.plc4x.java.modbus.base.field.ModbusField)

Aggregations

PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)4 ModbusField (org.apache.plc4x.java.modbus.base.field.ModbusField)4 Duration (java.time.Duration)3 Collections (java.util.Collections)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 PlcReadRequest (org.apache.plc4x.java.api.messages.PlcReadRequest)3 PlcReadResponse (org.apache.plc4x.java.api.messages.PlcReadResponse)3 PlcWriteRequest (org.apache.plc4x.java.api.messages.PlcWriteRequest)3 PlcWriteResponse (org.apache.plc4x.java.api.messages.PlcWriteResponse)3 PlcField (org.apache.plc4x.java.api.model.PlcField)3 PlcResponseCode (org.apache.plc4x.java.api.types.PlcResponseCode)3 PlcValue (org.apache.plc4x.java.api.value.PlcValue)3 ModbusProtocolLogic (org.apache.plc4x.java.modbus.base.protocol.ModbusProtocolLogic)3 org.apache.plc4x.java.modbus.readwrite (org.apache.plc4x.java.modbus.readwrite)3 ModbusRtuConfiguration (org.apache.plc4x.java.modbus.rtu.config.ModbusRtuConfiguration)3 HasConfiguration (org.apache.plc4x.java.spi.configuration.HasConfiguration)3 ParseException (org.apache.plc4x.java.spi.generation.ParseException)3 DefaultPlcReadRequest (org.apache.plc4x.java.spi.messages.DefaultPlcReadRequest)3 DefaultPlcReadResponse (org.apache.plc4x.java.spi.messages.DefaultPlcReadResponse)3 DefaultPlcWriteRequest (org.apache.plc4x.java.spi.messages.DefaultPlcWriteRequest)3