Search in sources :

Example 1 with WriteBufferByteBased

use of org.apache.plc4x.java.spi.generation.WriteBufferByteBased in project plc4x by apache.

the class CANOpenFrameDataHandler method toCAN.

@Override
public Message toCAN(CANOpenFrame frame) {
    try {
        CANOpenPayload payload = frame.getPayload();
        WriteBufferByteBased buffer = new WriteBufferByteBased(payload.getLengthInBytes(), ByteOrder.LITTLE_ENDIAN);
        payload.serialize(buffer);
        return builder.get().withId(frame.getService().getMin() + frame.getNodeId()).withData(buffer.getData()).create();
    } catch (SerializationException e) {
        throw new PlcRuntimeException(e);
    }
}
Also used : PlcRuntimeException(org.apache.plc4x.java.api.exceptions.PlcRuntimeException) SerializationException(org.apache.plc4x.java.spi.generation.SerializationException) CANOpenPayload(org.apache.plc4x.java.canopen.readwrite.CANOpenPayload) WriteBufferByteBased(org.apache.plc4x.java.spi.generation.WriteBufferByteBased)

Example 2 with WriteBufferByteBased

use of org.apache.plc4x.java.spi.generation.WriteBufferByteBased in project plc4x by apache.

the class BenchmarkGeneratedS7 method main.

public static void main(String[] args) throws Exception {
    byte[] rData = Hex.decodeHex("0300001611e00000000f00c2020100c1020311c0010a");
    long start = System.currentTimeMillis();
    int numRunsParse = 2000000;
    // Benchmark the parsing code
    TPKTPacket packet = null;
    for (int i = 0; i < numRunsParse; i++) {
        ReadBuffer rBuf = new ReadBufferByteBased(rData);
        packet = TPKTPacket.staticParse(rBuf);
    }
    long endParsing = System.currentTimeMillis();
    System.out.println("Parsed " + numRunsParse + " packets in " + (endParsing - start) + "ms");
    System.out.println("That's " + ((float) (endParsing - start) / numRunsParse) + "ms per packet");
    // Benchmark the serializing code
    int numRunsSerialize = 2000000;
    byte[] oData = null;
    for (int i = 0; i < numRunsSerialize; i++) {
        WriteBufferByteBased wBuf = new WriteBufferByteBased(packet.getLengthInBytes());
        packet.serialize(wBuf);
        oData = wBuf.getData();
    }
    long endSerializing = System.currentTimeMillis();
    System.out.println("Serialized " + numRunsSerialize + " packets in " + (endSerializing - endParsing) + "ms");
    System.out.println("That's " + ((float) (endSerializing - endParsing) / numRunsSerialize) + "ms per packet");
    if (!Arrays.equals(rData, oData)) {
        for (int i = 0; i < rData.length; i++) {
            if (rData[i] != oData[i]) {
                System.out.println("Difference in byte " + i);
            }
        }
        System.out.println("Not equals");
    } else {
        System.out.println("Bytes equal");
    }
}
Also used : ReadBuffer(org.apache.plc4x.java.spi.generation.ReadBuffer) TPKTPacket(org.apache.plc4x.java.s7.readwrite.TPKTPacket) ReadBufferByteBased(org.apache.plc4x.java.spi.generation.ReadBufferByteBased) WriteBufferByteBased(org.apache.plc4x.java.spi.generation.WriteBufferByteBased)

Example 3 with WriteBufferByteBased

use of org.apache.plc4x.java.spi.generation.WriteBufferByteBased in project plc4x by apache.

the class Df1Protocol method encode.

@Override
protected void encode(ChannelHandlerContext ctx, DF1Command msg, ByteBuf out) throws Exception {
    // Create a new df1 frame for transmitting the command
    DF1SymbolMessageFrame frame = new DF1SymbolMessageFrame(remoteAddr, localAddr, msg);
    // Serialize the message
    WriteBufferByteBased writeBuffer = new WriteBufferByteBased(frame.getLengthInBytes());
    frame.serialize(writeBuffer);
    byte[] data = writeBuffer.getData();
    // Send the serialized data
    // ctx.writeAndFlush(data);
    out.writeBytes(data);
}
Also used : DF1SymbolMessageFrame(org.apache.plc4x.java.df1.readwrite.DF1SymbolMessageFrame) WriteBufferByteBased(org.apache.plc4x.java.spi.generation.WriteBufferByteBased)

Aggregations

WriteBufferByteBased (org.apache.plc4x.java.spi.generation.WriteBufferByteBased)3 PlcRuntimeException (org.apache.plc4x.java.api.exceptions.PlcRuntimeException)1 CANOpenPayload (org.apache.plc4x.java.canopen.readwrite.CANOpenPayload)1 DF1SymbolMessageFrame (org.apache.plc4x.java.df1.readwrite.DF1SymbolMessageFrame)1 TPKTPacket (org.apache.plc4x.java.s7.readwrite.TPKTPacket)1 ReadBuffer (org.apache.plc4x.java.spi.generation.ReadBuffer)1 ReadBufferByteBased (org.apache.plc4x.java.spi.generation.ReadBufferByteBased)1 SerializationException (org.apache.plc4x.java.spi.generation.SerializationException)1