public class ProtocolData { private static final String PROTOCOL_HEADER = "10Hz"; //输出10HZ private static final String PROTOCOL_SEPARATOR = ","; //英文“,”分隔 private static final String PROTOCOL_NEWLINE = "\r\n"; //\r\n结束 private int frameNumber; //帧序号 private byte systemState; //系统状态 private byte northProgress; //寻北进度 private float headingAngle; //航向角 private float pitchAngle; //俯仰角 private float rollAngle; //横滚角 private float realTimeData; //数据更新 public enum systemState{ POSITION_1((byte)0x01), POSITION_2((byte)0x02), POSITION_3((byte)0x03), POSITION_4((byte)0x04), MIDDLE_PROCESS((byte)0x05), MAVIGATION_STAT((byte)0x06); private byte value; systemState(byte value){ this.value=value; } public byte getValue(){ return value; } } public enum realTimeData{ BATTERY_VOLTIGE((byte)0x00), PRESSURE_ALTITUDE((byte)0x01), PRODUCT_VERSION_NUMBER((byte)0x02), SOFTWARE_VERSIONING((byte)0x03); private byte value; realTimeData(byte value){ this.value=value; } public byte getValue(){ return value; } } public ProtocolData(int frameNumber, byte systemState, byte northProgress, float headingAngle, float pitchAngle, float rollAngle, float realTimeData) { this.frameNumber = frameNumber; this.systemState = systemState; this.northProgress = northProgress; this.headingAngle = headingAngle; this.pitchAngle = pitchAngle; this.rollAngle = rollAngle; this.realTimeData = realTimeData; } public String toString() { StringBuilder builder = new StringBuilder(); builder.append(PROTOCOL_HEADER); builder.append(frameNumber).append(PROTOCOL_SEPARATOR); builder.append(systemState).append(PROTOCOL_SEPARATOR); builder.append(northProgress).append(PROTOCOL_SEPARATOR); builder.append(headingAngle).append(PROTOCOL_SEPARATOR); builder.append(pitchAngle).append(PROTOCOL_SEPARATOR); builder.append(rollAngle).append(PROTOCOL_SEPARATOR); builder.append(realTimeData).append(PROTOCOL_NEWLINE); return builder.toString(); } }
时间: 2024-04-06 08:28:34 浏览: 49
这段代码是一个 Java 类,名为 ProtocolData,它定义了一个协议数据的格式。其中包含了协议头、分隔符、结束符和各个数据字段,如帧序号、系统状态、寻北进度、航向角等。此外,该类还定义了两个枚举类型,分别是系统状态和实时数据类型,用于表示系统状态和实时数据的取值范围。该类还提供了一个构造方法和一个 toString 方法,用于初始化对象和将对象转换成字符串表示。
相关问题
public class ProtocolData { private static final String PROTOCOL_HEADER = "10Hz"; //输出10HZ private static final String PROTOCOL_SEPARATOR = ","; //英文“,”分隔 private static final String PROTOCOL_NEWLINE = "\r\n"; //\r\n结束 private int frameNumber; //帧序号 private byte systemState; //系统状态 private byte northProgress; //寻北进度 private float headingAngle; //航向角 private float pitchAngle; //俯仰角 private float rollAngle; //横滚角 private float realTimeData; //数据更新 public ProtocolData(int frameNumber, byte systemState, byte northProgress, float headingAngle, float pitchAngle, float rollAngle, float realTimeData) { this.frameNumber = frameNumber; this.systemState = systemState; this.northProgress = northProgress; this.headingAngle = headingAngle; this.pitchAngle = pitchAngle; this.rollAngle = rollAngle; this.realTimeData = realTimeData; } public String toString() { StringBuilder builder = new StringBuilder(); builder.append(PROTOCOL_HEADER); builder.append(frameNumber).append(PROTOCOL_SEPARATOR); builder.append(systemState).append(PROTOCOL_SEPARATOR); builder.append(northProgress).append(PROTOCOL_SEPARATOR); builder.append(headingAngle).append(PROTOCOL_SEPARATOR); builder.append(pitchAngle).append(PROTOCOL_SEPARATOR); builder.append(rollAngle).append(PROTOCOL_SEPARATOR); builder.append(realTimeData).append(PROTOCOL_NEWLINE); return builder.toString(); }
这段代码看起来是一个 Java 类,名为 ProtocolData。它有一些常量,包括协议头、分隔符和换行符。它还有一些实例变量,包括帧序号、系统状态、寻北进度、航向角、俯仰角、横滚角和实时数据。它有一个构造函数,用于初始化这些实例变量。它还有一个 toString 方法,用于将实例变量格式化成一个字符串,以便发送到其他设备。
如何根据这个协议public class Protocol { private static final String PROTOCOL_HEADER = "10Hz,"; private static final String PROTOCOL_SEPARATOR = ","; private static final String PROTOCOL_NEWLINE = "\r\n"; private int frameNumber; private int systemState; private int northProgress; private float headingAngle; private float pitchAngle; private float rollAngle; private float realTimeData; public Protocol(int frameNumber, int systemState, int northProgress, float headingAngle, float pitchAngle, float rollAngle, float realTimeData) { this.frameNumber = frameNumber; this.systemState = systemState; this.northProgress = northProgress; this.headingAngle = headingAngle; this.pitchAngle = pitchAngle; this.rollAngle = rollAngle; this.realTimeData = realTimeData; } public String toString() { StringBuilder builder = new StringBuilder(); builder.append(PROTOCOL_HEADER); builder.append(frameNumber).append(PROTOCOL_SEPARATOR); builder.append(systemState).append(PROTOCOL_SEPARATOR); builder.append(northProgress).append(PROTOCOL_SEPARATOR); builder.append(headingAngle).append(PROTOCOL_SEPARATOR); builder.append(pitchAngle).append(PROTOCOL_SEPARATOR); builder.append(rollAngle).append(PROTOCOL_SEPARATOR); builder.append(realTimeData).append(PROTOCOL_NEWLINE); return builder.toString(); }}解析这个数据举例: 1,10,0,10.56,0.1234,0.23456,75.0
这个协议的数据结构是固定的,每个字段的意义如下:
- frameNumber:帧数
- systemState:系统状态
- northProgress:北向进度
- headingAngle:航向角
- pitchAngle:俯仰角
- rollAngle:横滚角
- realTimeData:实时数据
根据这个协议,数据 "1,10,0,10.56,0.1234,0.23456,75.0" 可以解析为:
- frameNumber = 1
- systemState = 10
- northProgress = 0
- headingAngle = 10.56
- pitchAngle = 0.1234
- rollAngle = 0.23456
- realTimeData = 75.0
如果你想要解析更多的数据,只需要将数据按照协议中定义的格式进行拼接,然后再使用相应的方法进行解析即可。
阅读全文