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);
}
}
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");
}
}
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);
}
Aggregations