一种在嵌入式设备上将字节流转换为 C89 中的数据包流的方法 [英] A way to convert byte stream to packet stream in C89 on an embedded device
问题描述
我正在使用通过 rs232(通过 USB 的 rs232)连接到 PC 的嵌入式设备.
I’m working on with an embedded device that is connected to PC using rs232 (rs232 over USB).
我正在考虑开发自己的协议:
I’m thinking about developing my own protocol:
<MAGIC><LENGTH><BINARY DATA><CRC>
但我不想重新发明轮子.
but I don’t want to reinvent the wheel.
请注意:我正在考虑相当受限的设备:4kb 的 RAM,没有内核,也没有标准 C 库.
Please note that: I'm thinking about quite restricted device: 4kb of RAM, no kernel, nor standard C lib.
你能想出一个标准的方法来做到这一点(也许是开源库)?
Can you think about a standard way to do this (maybe open source library)?
如果您编写自己的解决方案,是否有任何最佳实践?
If you code your own solution do have any best practices?
- 您是否也在包的末尾使用 MAGIC 字节?
- 也许使用时间间隔而不是分隔符更好?
- 如何在流二进制数据中找到包的开头?
- 也许使用文本协议更好?
更新:请重新阅读问题.我不应该要求图书馆,而是要求良好的做法.
UPDATE: Please re read the question. I shouldn't ask for library but for good practices.
推荐答案
参见 我对一个非常相似的问题的回答关于简单协议的细节.
See this answer I gave to a very similar question regarding details of a simple protocol.
回应您的具体观点:
- 数据包末尾的魔术"字节不会造成任何伤害,但如果您已经知道数据包应该有多长并且具有 CRC,那么它们就是多余的.
- 指定超时时间是明智的,因此如果一个数据包中的字节之间的间隔过大,则会标记错误.使用过 Modbus,我不相信在其他地方使用基于时间的分隔符的价值.
- 您的意思是如何在二进制数据流中找到数据包的开头"?如果是这样,可以指定数据包之间的最小间隔,和/或要求接收方在每个数据包后确认.
- 使调试更容易,并且不需要在 PC 上安装任何特殊软件,但效率不高.当然,如果可用性比效率更重要,那么基于文本的系统是完全合适的.
- "Magic" bytes at the end of packets don't do any harm, but they're redundant if you already know how long the packet is supposed to be, and have a CRC.
- It can be sensible to specifiy timeout times, so if there's too big a gap between bytes within one packet, then an error is flagged. Having used Modbus, I'm not convinced of the value of using time-based delimiters elsewhere.
- Do you mean, "how do you find the beginning of packets in a stream of binary data"? If so, maybe specify a minimum gap between packets, and/or require the recipient to acknolwedge after every packet.
- Makes it easier for debugging, and doesn't require any special software on the PC, but not very efficient. Of course, if usability is more important than efficiency, than a text-based system is entirely appropriate.
这篇关于一种在嵌入式设备上将字节流转换为 C89 中的数据包流的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!