使用 RXTX 同步 java 串口连接

发布于 2024-09-14 05:05:30 字数 639 浏览 12 评论 0原文

我有一个 Java 程序,它不断侦听串行端口上某种格式的消息。当发送方应用程序未发送消息时,它会按以下格式发送心跳消息:

  • 字节 1: 1
  • 字节 2: 0xFE
  • 字节 3: 0xED
  • 字节 4-255: 0

如果接收方应用程序在发送方之前启动,则一切正常,但是,如果发送方一直在发出心跳,则几乎可以肯定接收方在启动时将在心跳消息中间开始侦听(这是一个问题,因为我以 255 字节为单位读取,因为每条消息都是 Reed-所罗门编码)。我编写了一个同步函数,它是一个简单的状态机。它在以下条件下一次读取 1 个字节:

  • 如果我刚刚读取 1:切换到 NEW_MESSAGE 状态。

  • 如果我刚刚读取了 0xFE:切换到 POTENTIAL_HEARTBEAT 状态。

  • 如果我刚刚读取 0xED:切换到 HEARTBEAT 状态

相关检查已到位,以确保这些状态不会乱序到达,并且一旦达到 HEARTBEAT 状态,我就会读取剩余的 252 个 0,并假设接收器已同步。在迄今为止的测试中,尽管发送了正确的字节以有效触发同步,但该同步器尚未正常工作。我的问题是:有没有更好的方法来同步串行通信,因为我真的一点也不喜欢我的方法。

I have a Java program that continually listens for messages of a certain format on a serial port. When the sender application is not sending messages, it sends heartbeat messages in the following format:

  • byte 1: 1
  • byte 2: 0xFE
  • byte 3: 0xED
  • bytes 4-255: 0

If the receiver application is started before the sender, everything functions correctly, however, if the sender has been throwing down heartbeats, it's almost certain that the receiver, when started, will begin listening in the middle of a heartbeat message (which is a problem because I read in chunks of 255 bytes as each message is Reed-Solomon encoded). I wrote a synchronizing function that's a simple state machine. It reads 1 byte at a time with the following conditions:

  • If I just read a 1: switch to the NEW_MESSAGE state.

  • If I just read a 0xFE: switch to the POTENTIAL_HEARTBEAT state.

  • If I just read a 0xED: switch to the HEARTBEAT state

Relevant checks are in place to ensure that these states cannot be arrived upon out of order, and once I've reached the HEARTBEAT state, I read the remaining 252 0's and assume the receiver has been synchronized. In testing so far, this synchronizer has not worked correctly despite the correct bytes being sent down to effectively trigger a synchronization. My question is this: is there a better way to synchronize serial communications, because I really don't like my method at all.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

著墨染雨君画夕 2024-09-21 05:05:30

问题实际上只是我忘记了位排序的重要性。我正在读取的字节通道是小端字节序,因此 16 位整数 0xFEED 将以 0xED 0xFE 而不是 0xFE oxED 的形式出现。只需在状态检查中进行简单的切换,一切就可以正常工作。

衷心感谢所有评论的人。

The problem was actually just me forgetting the importance of bit ordering. The byte channel I was reading from was little endian so the 16 bit integer 0xFEED was coming to me as 0xED 0xFE instead of 0xFE oxED. With a simple switch in the state checking, everything works peachy king.

Sincere thanks to all that commented.

绝影如岚 2024-09-21 05:05:30

扩展您的状态机并检查最后 3 个接收到的字节。将它们与心跳消息的前 3 个字节进行比较。由于前 3 个字节似乎始终相同,因此您可以使用它来检测心跳消息的开头并进行同步。

Extend your state machine and check for the last 3 received bytes. Comapre them to the first 3 bytes of your heartbeat message. As the first 3 bytes seems to be always the same, you can use this to detect the beginning of the heartbeat message and do your sync.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文