我应该如何构建数据并通过 uart 发送多个字节?

How should I frame the data and send multiple bytes over uart?

我正在尝试编写使用 uart 协议与 mcu(esp32) 通信的开关板上触摸传感器的代码。我们有数据包帧,我们必须将其写入 uart 以获取读数。让我分享一些文档,

1.

In API frame structure following is the fixed definition of any command frame

     Every first byte of frame is fixed 0x7B (“{” in ASCII, 123 in decimal).
    Every second byte of frame is ‘command type’ byte, it informs what you need to do with rest of the data. This will act as a frame Identifier in the data received from touch panel (response frame and event frame)
    Third byte is length of frame. It is 1-byte value (L - 4) where L is total numbers of bytes of whole frame.
    Second Last byte is checksum. Checksum is a lower byte of addition of whole individual bytes of frame except First byte (Start byte), Second byte (Command type byte), 
       Second Last byte (Checksum byte itself) and Last byte is 0x7D.
    Last byte is 0x7D, it is End code to indicate the end of frame. (“}” in ASCII, 125  in decimal).

For Example, consider following frame.
Table 1.1 Frame example 1.

1st Byte    2nd Byte    3rd Byte    4th Byte    5th Byte    6th Byte
0x7B        0x03        0x02        0x05        0x07        0x7D
Start Code  Command     Length         Data        Checksum     End Code

So the checksum will be lower byte of addition of third byte and fourth byte.
0x02 + 0x05 = 0x07 so we will consider 0x07 as checksum here.


Example 2, consider following frame.
Table 1.2 Frame example 2.

1st Byte    2nd Byte    3rd Byte    4th Byte    5th Byte    6th Byte    7th Byte    8th Byte
0x7B        0x52        0x04        0x02        0xFF        0x14        0x19        0x7D
Start Code  Frame       Length      Data        Data        Data        Checksum    End Code          
            Identifier

In this example 2 the checksum will be lower byte of addition of third to sixth byte.
0x04 + 0x02 + 0xFF + 0x14 = 0x0119 so we will consider 0x19 as checksum here.

2. 

    Blink LED (All slider LED) control.
    1.Command
    
    This package is used to control blinking of LED. Hardware Version 2.0 has dedicated status LED. Which will be used to indicate status of product as on need. 
    
    Table 1.6 Blink LED command package detail.
    Status  1st Byte    2nd Byte    3rd Byte    4th Byte        5th Byte            6th Byte    8th Byte
    Start   0x7B        0x05        0x03    0x01  (Start)   (0x01 to 0xNN*)     Checksum    0x7D
    Stop    0x7B        0x05        0x03    0x00  (Stop)     0x00               Checksum    0x7D
        Start Code      Command     Length              Pulse with (x100ms)     Checksum    End Code
    
    To start status LED blinking, the start command frame is sent with required value of 4th byte as 0x01. For Example, to make status LED blinking as time duration 200ms, the value of 5th byte is 0x02. 
    And to stop status LED blinking the stop frame is sent
    
    2.Response
    Table 1.7 Blink LED response detail.
    
    1st Byte    2nd Byte    3rd Byte    4th Byte    5th Byte
    0x7B        0x55        0x01        0x01        0x7D

n 1分,我们可以看出uart frame应该是怎样的。在2点,我想读写帧命令来停止和开始闪烁led。

我的问题是

我研究了如何通过 uart 构建数据包和发送帧,但没有找到任何有用的博客和答案。

更多信息:

Language: C
Compiler: GCC 
MCU: ESP32

希望我能解释清楚。

在此先感谢您的帮助!!

发送多个字节

使用 ESP-IDF 框架可以直接发送多个字节。假设您的命令帧位于一个名为 frame 的数组中,并且帧的长度(以字节为单位)存储在 frame_length:

uint8_t frame[] = { 0x7B, 0x03, 0x02, 0x05, 0x07, 0x7D };
int frame_length = 6;
uart_write_bytes(uart_port, frame, frame_length);

更大的挑战可能首先是如何构造框架,尤其是如何计算校验和。

发送多帧

发送多个帧也很简单。只需多次调用上述函数即可。该协议经过精心设计,使接收方能够将字节流拆分为帧。

但是您应该防止多个任务同时发送帧。这样沟通可能会混乱。

接收帧

接收也不是问题。只需逐帧阅读。这是一个两步过程:

  1. 读取3个字节。第三个字节提供帧的长度。
  2. 读取剩余的字节。

它可能看起来像这样:

#define MAX_FRAME_LENGTH 80

uint8_t frame[MAX_FRAME_LENGTH];
int frame_length = 0;

int read_frame(uint8_t* frame) {
    uart_read_bytes(uart_port, frame, 3, portMAX_DELAY);
    uart_read_bytes(uart_port, frame + 3, frame[2] + 4, portMAX_DELAY);
    return frame[2] + 4;
}