Search in sources :

Example 1 with CANOpenFrame

use of org.apache.plc4x.java.canopen.readwrite.CANOpenFrame in project plc4x by apache.

the class CANOpenProtocolLogic method writeInternally.

private void writeInternally(DefaultPlcWriteRequest writeRequest, CANOpenPDOField field, CompletableFuture<PlcWriteResponse> response) {
    PlcValue writeValue = writeRequest.getPlcValues().get(0);
    try {
        String fieldName = writeRequest.getFieldNames().iterator().next();
        WriteBufferByteBased writeBuffer = new WriteBufferByteBased(DataItem.getLengthInBytes(writeValue, field.getCanOpenDataType(), writeValue.getLength()), ByteOrder.LITTLE_ENDIAN);
        DataItem.staticSerialize(writeBuffer, writeValue, field.getCanOpenDataType(), writeValue.getLength(), ByteOrder.LITTLE_ENDIAN);
        final CANOpenPDOPayload payload = new CANOpenPDOPayload(new CANOpenPDO(writeBuffer.getData()));
        context.sendToWire(new CANOpenFrame((short) field.getNodeId(), field.getService(), payload));
        response.complete(new DefaultPlcWriteResponse(writeRequest, Collections.singletonMap(fieldName, PlcResponseCode.OK)));
    } catch (Exception e) {
        response.completeExceptionally(e);
    }
}
Also used : PlcValue(org.apache.plc4x.java.api.value.PlcValue) CANOpenPDO(org.apache.plc4x.java.canopen.readwrite.CANOpenPDO) DefaultPlcWriteResponse(org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse) CANOpenPDOPayload(org.apache.plc4x.java.canopen.readwrite.CANOpenPDOPayload) CANOpenAbortException(org.apache.plc4x.java.canopen.transport.CANOpenAbortException) PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) CANOpenFrame(org.apache.plc4x.java.canopen.readwrite.CANOpenFrame)

Example 2 with CANOpenFrame

use of org.apache.plc4x.java.canopen.readwrite.CANOpenFrame in project plc4x by apache.

the class CANOpenFrameDataHandler method fromCAN.

@Override
public CANOpenFrame fromCAN(FrameData frame) {
    CANOpenService service = StaticHelper.serviceId((short) frame.getNodeId());
    int nodeId = Math.abs(service.getMin() - frame.getNodeId());
    return new CANOpenFrame((short) nodeId, service, frame.read(CANOpenPayload::staticParse, service));
}
Also used : CANOpenService(org.apache.plc4x.java.canopen.readwrite.CANOpenService) CANOpenFrame(org.apache.plc4x.java.canopen.readwrite.CANOpenFrame)

Example 3 with CANOpenFrame

use of org.apache.plc4x.java.canopen.readwrite.CANOpenFrame in project plc4x by apache.

the class Main method main.

public static void main(String[] args) throws Exception {
    PlcDriverManager driverManager = new PlcDriverManager();
    CANOpenDriverContext.CALLBACK.addCallback(new Callback() {

        @Override
        public void receive(CANOpenFrame frame) {
        // System.err.println("Received frame " + frame);
        }
    });
    PlcConnection connection = driverManager.getConnection("canopen:javacan://vcan0?nodeId=11");
    // UUID.randomUUID().toString();
    String value = "abcdef";
    CompletableFuture<? extends PlcWriteResponse> response = connection.writeRequestBuilder().addItem("foo", "SDO:13:0x2000/0x0:VISIBLE_STRING", value).build().execute();
    response.whenComplete((writeReply, writeError) -> {
        System.out.println("====================================");
        if (writeError != null) {
            System.out.println("Error ");
            writeError.printStackTrace();
        } else {
            System.out.println("Result " + writeReply.getResponseCode("foo") + " " + value);
            PlcReadRequest.Builder builder = connection.readRequestBuilder();
            builder.addItem("foo", "SDO:13:0x2000/0x0:VISIBLE_STRING");
            CompletableFuture<? extends PlcReadResponse> future = builder.build().execute();
            future.whenComplete((readReply, readError) -> {
                System.out.println("====================================");
                if (readError != null) {
                    System.out.println("Error ");
                    readError.printStackTrace();
                } else {
                    System.out.println("Result " + readReply.getString("foo"));
                }
            });
        }
    });
// while (true) {
// }
}
Also used : Callback(org.apache.plc4x.java.canopen.listener.Callback) PlcReadRequest(org.apache.plc4x.java.api.messages.PlcReadRequest) PlcDriverManager(org.apache.plc4x.java.PlcDriverManager) PlcConnection(org.apache.plc4x.java.api.PlcConnection) CANOpenFrame(org.apache.plc4x.java.canopen.readwrite.CANOpenFrame)

Example 4 with CANOpenFrame

use of org.apache.plc4x.java.canopen.readwrite.CANOpenFrame in project plc4x by apache.

the class SDOUploadConversation method fetch.

private void fetch(ByteStorage.SDOUploadStorage storage, BiConsumer<Integer, byte[]> valueCallback, CompletableFuture<PlcValue> receiver, boolean toggle, int size) {
    logger.info("Request next data block for address {}/{}", Integer.toHexString(address.getIndex()), Integer.toHexString(address.getSubindex()));
    delegate.send(createFrame(new SDOSegmentUploadRequest(toggle))).check(new NodeIdPredicate(answerNodeId)).onTimeout(receiver::completeExceptionally).unwrap(CANOpenFrame::getPayload).only(CANOpenSDOResponse.class).onError((payload, error) -> onError(receiver, payload, error)).unwrap(CANOpenSDOResponse::getResponse).check(new TypeOrAbortPredicate<>(SDOSegmentUploadResponse.class)).unwrap(payload -> unwrap(SDOSegmentUploadResponse.class, payload)).handle(either -> {
        if (either.isLeft()) {
            SDOAbort abort = either.getLeft();
            receiver.completeExceptionally(new CANOpenAbortException("Could not complete operation", abort.getCode()));
        } else {
            SDOSegmentUploadResponse response = either.get();
            if (response.getToggle() != toggle) {
                // TODO find proper error code in specs
                receiver.completeExceptionally(new CANOpenAbortException("Remote operation failed", 1000));
                SDOAbort abort = new SDOAbort(address, 1000);
                delegate.sendToWire(createFrame(new SDOAbortRequest(abort)));
                return;
            }
            storage.append(either.get());
            if (response.getLast()) {
                // validate size
                logger.trace("Completed reading of data from {}/{}, collected {}, wanted {}", Integer.toHexString(address.getIndex()), Integer.toHexString(address.getSubindex()), storage.size(), size);
                valueCallback.accept(Long.valueOf(size).intValue(), storage.get());
            } else {
                logger.trace("Continue reading of data from {}/{}, collected {}, wanted {}", Integer.toHexString(address.getIndex()), Integer.toHexString(address.getSubindex()), storage.size(), size);
                fetch(storage, valueCallback, receiver, !toggle, size);
            }
        }
    });
}
Also used : PlcException(org.apache.plc4x.java.api.exceptions.PlcException) CANOpenAbortException(org.apache.plc4x.java.canopen.transport.CANOpenAbortException) ParseException(org.apache.plc4x.java.spi.generation.ParseException) Logger(org.slf4j.Logger) CANOpenFrame(org.apache.plc4x.java.canopen.readwrite.CANOpenFrame) org.apache.plc4x.java.canopen.readwrite(org.apache.plc4x.java.canopen.readwrite) LoggerFactory(org.slf4j.LoggerFactory) BiConsumer(java.util.function.BiConsumer) PlcValue(org.apache.plc4x.java.api.value.PlcValue) CompletableFuture(java.util.concurrent.CompletableFuture) ByteStorage(org.apache.plc4x.java.canopen.api.segmentation.accumulator.ByteStorage) CANOpenAbortException(org.apache.plc4x.java.canopen.transport.CANOpenAbortException) CANOpenFrame(org.apache.plc4x.java.canopen.readwrite.CANOpenFrame)

Example 5 with CANOpenFrame

use of org.apache.plc4x.java.canopen.readwrite.CANOpenFrame in project plc4x by apache.

the class SDOUploadConversation method execute.

public void execute(CompletableFuture<PlcValue> receiver) {
    SDOInitiateUploadRequest rq = new SDOInitiateUploadRequest(address);
    delegate.send(createFrame(rq)).check(new NodeIdPredicate(answerNodeId)).onTimeout(receiver::completeExceptionally).unwrap(CANOpenFrame::getPayload).only(CANOpenSDOResponse.class).onError((payload, error) -> onError(receiver, payload, error)).unwrap(CANOpenSDOResponse::getResponse).check(new TypeOrAbortPredicate<>(SDOInitiateUploadResponse.class)).unwrap(payload -> unwrap(SDOInitiateUploadResponse.class, payload)).check(either -> either.isLeft() || either.get().getAddress().equals(address)).handle(either -> {
        if (either.isLeft()) {
            SDOAbort abort = either.getLeft();
            receiver.completeExceptionally(new CANOpenAbortException("Could not complete operation", abort.getCode()));
        } else {
            handle(receiver, either.get());
        }
    });
}
Also used : PlcException(org.apache.plc4x.java.api.exceptions.PlcException) CANOpenAbortException(org.apache.plc4x.java.canopen.transport.CANOpenAbortException) ParseException(org.apache.plc4x.java.spi.generation.ParseException) Logger(org.slf4j.Logger) CANOpenFrame(org.apache.plc4x.java.canopen.readwrite.CANOpenFrame) org.apache.plc4x.java.canopen.readwrite(org.apache.plc4x.java.canopen.readwrite) LoggerFactory(org.slf4j.LoggerFactory) BiConsumer(java.util.function.BiConsumer) PlcValue(org.apache.plc4x.java.api.value.PlcValue) CompletableFuture(java.util.concurrent.CompletableFuture) ByteStorage(org.apache.plc4x.java.canopen.api.segmentation.accumulator.ByteStorage) CANOpenAbortException(org.apache.plc4x.java.canopen.transport.CANOpenAbortException)

Aggregations

CANOpenFrame (org.apache.plc4x.java.canopen.readwrite.CANOpenFrame)5 PlcValue (org.apache.plc4x.java.api.value.PlcValue)3 CANOpenAbortException (org.apache.plc4x.java.canopen.transport.CANOpenAbortException)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 BiConsumer (java.util.function.BiConsumer)2 PlcException (org.apache.plc4x.java.api.exceptions.PlcException)2 ByteStorage (org.apache.plc4x.java.canopen.api.segmentation.accumulator.ByteStorage)2 org.apache.plc4x.java.canopen.readwrite (org.apache.plc4x.java.canopen.readwrite)2 ParseException (org.apache.plc4x.java.spi.generation.ParseException)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 PlcDriverManager (org.apache.plc4x.java.PlcDriverManager)1 PlcConnection (org.apache.plc4x.java.api.PlcConnection)1 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)1 PlcReadRequest (org.apache.plc4x.java.api.messages.PlcReadRequest)1 Callback (org.apache.plc4x.java.canopen.listener.Callback)1 CANOpenPDO (org.apache.plc4x.java.canopen.readwrite.CANOpenPDO)1 CANOpenPDOPayload (org.apache.plc4x.java.canopen.readwrite.CANOpenPDOPayload)1 CANOpenService (org.apache.plc4x.java.canopen.readwrite.CANOpenService)1 DefaultPlcWriteResponse (org.apache.plc4x.java.spi.messages.DefaultPlcWriteResponse)1