一种在嵌入式设备上将C89字节流转换为包流的方法

发布于 2024-07-19 20:28:20 字数 515 浏览 3 评论 0原文

我正在使用使用 rs232(通过 USB 的 RS232)连接到 PC 的嵌入式设备。

我正在考虑开发自己的协议:

但我不想重新发明轮子。

注意:我正在考虑非常受限的设备:4kb RAM,没有内核,也没有标准 C 库。

您能考虑一个标准方法来做到这一点(也许是开源库)?

如果您编写自己的解决方案是否有任何最佳实践?

  • 您是否也在包的末尾使用 MAGIC 字节?
  • 也许使用时间间隔而不是分隔符更好?
  • 如何在流二进制数据中找到包的开头?
  • 也许使用文本协议更好?

更新: 请重新阅读问题。 我不应该要求图书馆,而应该要求良好的实践。

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.

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?

  • Do you use MAGIC bytes also at the end of packages?
  • Maybe it is better to use time gaps instead of delimiters?
  • How do you find the beginning of packages in a stream binary data?
  • Maybe it is better to use text protocols?

UPDATE:
Please re read the question. I shouldn't ask for library but for good practices.

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

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

发布评论

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

评论(3

写给空气的情书 2024-07-26 20:28:20

请参阅我对一个非常相似的问题给出的答案关于简单协议的细节。

回应您的具体观点:

  1. 数据包末尾的“魔术”字节不会造成任何损害,但如果您已经知道数据包应该有多长并且有 CRC,那么它们就是多余的。
  2. 指定超时时间是明智的,因此如果一个数据包内的字节之间存在太大的间隙,则会标记一个错误。 使用过 Modbus 后,我不相信在其他地方使用基于时间的分隔符的价值。
  3. 您的意思是“如何在二进制数据流中找到数据包的开头”? 如果是这样,可以指定数据包之间的最小间隙,和/或要求接收者在每个数据包后进行确认。
  4. 使调试更容易,并且不需要PC上任何特殊软件,但效率不是很高。 当然,如果可用性比效率更重要,那么基于文本的系统是完全合适的。

See this answer I gave to a very similar question regarding details of a simple protocol.

To respond to your specific points:

  1. "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.
  2. 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.
  3. 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.
  4. 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.
野味少女 2024-07-26 20:28:20

对于这样的事情,当您获得可在您的设备上运行的现有解决方案时,重新发明轮子会更容易。

void buffer_packet(unsigned char rx_byte)
{
    static unsigned char byte_count = 0;
    static unsigned char packet[8];

    packet[byte_count++] = rx_byte;
    if (byte_count == 8)
    {
        unsigned char crc = calculate_crc(packet, 8);

        write_uart(0x55);
        write_uart(8);
        while (byte_count--)
        {
            write_uart(packet[7 - byte_count]);
        }
        write_uart(crc);
    }
}

或者也许我低估了你的问题。 如果您正在寻找如何生成 RS232 位,请查看微控制器数据表。

For something like this by the time you get an existing solution to work on your device, it would have been easier just to reinvent the wheel.

void buffer_packet(unsigned char rx_byte)
{
    static unsigned char byte_count = 0;
    static unsigned char packet[8];

    packet[byte_count++] = rx_byte;
    if (byte_count == 8)
    {
        unsigned char crc = calculate_crc(packet, 8);

        write_uart(0x55);
        write_uart(8);
        while (byte_count--)
        {
            write_uart(packet[7 - byte_count]);
        }
        write_uart(crc);
    }
}

Or maybe I am underestimating your problem. If you're looking for how to generate the RS232 bits look in your microcontrollers datasheet.

二货你真萌 2024-07-26 20:28:20

除了 I/O 原语之外,唯一的事情就是 CRC 计算。 这里有一篇带有代码的精彩文章

About the only thing there beyond your I/O primitives is going to be the CRC calculation. There's a nifty article, with code, here.

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