我正在尝试编写开关板上触摸传感器的代码,该传感器使用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帧应该是什么样子。在2点,我想读写帧命令来停止和启动led闪烁。
我的问题是
- 我应该如何通过uart发送多个字节
- 我需要发送一帧数据包吗?如果是,我该怎么办那个
- 此外,我应该如何阅读它的回应
我研究了如何通过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);
更大的挑战可能是如何首先构建帧,特别是如何计算校验和。
发送多帧
发送多帧也是直接的。只需多次调用上述函数即可。该协议经过精心设计,使得接收器能够将字节流分割成帧。
但是,您应该防止多个任务同时发送帧。那样的话,沟通可能会变得混乱。
接收帧
接收也不是问题。一帧一帧地读。这是一个两步过程:
- 读取3个字节。第三个字节提供帧的长度
- 读取剩余的字节
看起来可能是这样的:
#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;
}