使用 header 和 body 从串行端口发送消息 java
sending a mesage with a header and body out a serial port with java
我四处搜索都找不到这个问题的答案。我相当确定这并不困难,可能是我的搜索没有使用正确的措辞。
我正在研究与万向节控制器的通信。我已经能够连接到它所连接的串行端口。我找不到有关如何格式化消息的信息:
Each command consists
of the header and the body, both with checksum. Commands with the wrong header or body checksum, or
with the body size that differs from expected, should be ignored.
任何人都可以指出如何写入串行端口的方向吗?
谢谢,
洛伦
有两种通过串行端口与设备通信的典型方法。在这两种情况下,最终结果都是通过线路发送原始位。如果您的设备需要它,您可以发送 ASCII 字符串,但由于您的摘录特别提到了数据包包装,我敢说他们需要字节。
您复制粘贴的摘录听起来像是来自解释通信所需的确切协议的手册。简而言之,您将执行以下操作。
- 设置串口
- 准备串口数据
- 发送串口数据
我喜欢使用 jSSC 进行串行通信,尽管很多其他人使用 RXTX。 jSSC 对我来说更可靠,所以我将在示例中使用它。您的设备手册应指定所需的波特率、数据位、停止位、奇偶校验和握手(如果有)。
这里我们设置一个端口(根据需要替换您的参数)。有关详细信息,请参阅文档。 https://github.com/scream3r/java-simple-serial-connector
SerialPort _port = new SerialPort(portName);
_port .openPort();
_port.setParams(baudRate, dataBits, stopBits, parity, setRTS, setDTR);
_port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
要构建您的数据包,您需要知道:
- Header格式
- 长度requirement/restrictions
- 校验和方法
这里我将制作一个超级简单的数据包,这只是一个示例,很可能不适用于您的用例。
// Format is: [length][7 data bytes][8 bit additive checksum]
// Create an empty byte array
byte[] packet = new byte[8];
// Our simple header
packet[0] = packet.length;
// Some data
byte[] dummyData = new byte[] { 1, 2, 3, 4, 5, 6, 7 };
// Copy data to packet
System.arraycopy(dummyData, 0, packet, 0, dummyData.length);
// Go do the checksum (good exercise for you)
byte checksum = getChecksum(packet);
packet[7] = checksum;
所以现在我们有了一个端口,一些数据...然后呢?发送吧。
// Just send those bytes
_port.writeBytes(packet);
一旦掌握了窍门,请阅读如何读取响应、设置 OnDataReceivedEvents 以及如何更有效地创建数据包。 Google的一些好词:
- jSSC
- Java RXTX
- 波特率
- Async/Sync 数据处理
我四处搜索都找不到这个问题的答案。我相当确定这并不困难,可能是我的搜索没有使用正确的措辞。
我正在研究与万向节控制器的通信。我已经能够连接到它所连接的串行端口。我找不到有关如何格式化消息的信息:
Each command consists of the header and the body, both with checksum. Commands with the wrong header or body checksum, or with the body size that differs from expected, should be ignored.
任何人都可以指出如何写入串行端口的方向吗?
谢谢,
洛伦
有两种通过串行端口与设备通信的典型方法。在这两种情况下,最终结果都是通过线路发送原始位。如果您的设备需要它,您可以发送 ASCII 字符串,但由于您的摘录特别提到了数据包包装,我敢说他们需要字节。
您复制粘贴的摘录听起来像是来自解释通信所需的确切协议的手册。简而言之,您将执行以下操作。
- 设置串口
- 准备串口数据
- 发送串口数据
我喜欢使用 jSSC 进行串行通信,尽管很多其他人使用 RXTX。 jSSC 对我来说更可靠,所以我将在示例中使用它。您的设备手册应指定所需的波特率、数据位、停止位、奇偶校验和握手(如果有)。
这里我们设置一个端口(根据需要替换您的参数)。有关详细信息,请参阅文档。 https://github.com/scream3r/java-simple-serial-connector
SerialPort _port = new SerialPort(portName);
_port .openPort();
_port.setParams(baudRate, dataBits, stopBits, parity, setRTS, setDTR);
_port.setFlowControlMode(SerialPort.FLOWCONTROL_NONE);
要构建您的数据包,您需要知道:
- Header格式
- 长度requirement/restrictions
- 校验和方法
这里我将制作一个超级简单的数据包,这只是一个示例,很可能不适用于您的用例。
// Format is: [length][7 data bytes][8 bit additive checksum]
// Create an empty byte array
byte[] packet = new byte[8];
// Our simple header
packet[0] = packet.length;
// Some data
byte[] dummyData = new byte[] { 1, 2, 3, 4, 5, 6, 7 };
// Copy data to packet
System.arraycopy(dummyData, 0, packet, 0, dummyData.length);
// Go do the checksum (good exercise for you)
byte checksum = getChecksum(packet);
packet[7] = checksum;
所以现在我们有了一个端口,一些数据...然后呢?发送吧。
// Just send those bytes
_port.writeBytes(packet);
一旦掌握了窍门,请阅读如何读取响应、设置 OnDataReceivedEvents 以及如何更有效地创建数据包。 Google的一些好词:
- jSSC
- Java RXTX
- 波特率
- Async/Sync 数据处理