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);
}
}
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));
}
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) {
// }
}
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);
}
}
});
}
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());
}
});
}
Aggregations