embedded - 我应该如何构建数据并通过 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帧应该是怎样的。在第 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;
}
推荐阅读
- firebase - 我们可以使用firestore按开始日期对事件进行排序并在结束字段上设置条件吗
- java - 通过 REST 端点处理来自 Spark 应用程序的传入请求
- c++ - 将 shared_ptr
和 shared_ptr的实例 具有相同的原始指针共享引用计数? - javascript - 更改文本区域的值
- vue.js - 从外部 js/ts 文件调用 nuxt/axios 模块
- c# - 如何在 KnapSack 问题中显示所有包含的数字?
- sql - 如何使用具有子句和自连接的 sql 计算中位数?
- angular - SELF_SIGNED_CERT_IN_CHAIN 误差角量角器
- php - ePDQ Payment by link [PHP]
- html - CSS 正在加载到页面,但未完全显示