RTU_ALL_AI/docs/public/API-myBase.md

113 lines
3.0 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# myBase.h — 公共基础定义 API 参考
## 模块概述
myBase.h 是所有模块共享的基础头文件。提供通用宏、类型别名、字节序转换、数据类型常量等。
## 终端颜色码
```c
#define COLOR_RESET "\033[0m" // 重置
#define COLOR_RED "\033[31m" // 红色
#define COLOR_GREEN "\033[32m" // 绿色
#define COLOR_YELLOW "\033[33m" // 黄色
#define COLOR_BLUE "\033[34m" // 蓝色
#define COLOR_MAGENTA "\033[35m" // 品红
#define COLOR_CYAN "\033[36m" // 青色
#define COLOR_WHITE "\033[37m" // 白色
```
**示例**:
```c
printf(COLOR_RED "错误: %s" COLOR_RESET "\n", "连接失败");
```
## 文件名缩写
```c
#define __SHORT_FILE__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
```
自动去掉文件路径前缀跨平台兼容Linux 用 `/`Windows 用 `\`)。
## 简易日志宏
```c
#define MY_LOG(color, level, fmt, ...) // 控制台日志(带颜色)
#define MY_LOG_I(fmt, ...) // INFO白色
#define MY_LOG_E(fmt, ...) // ERROR红色
```
## LOCAL 修饰符
```c
#define LOCAL static
```
语义化表示"文件内可见",等价于 `static`
## 定长字符串类型
```c
typedef char S_STR[64]; // 64 字节短字符串
typedef char L_STR[128]; // 128 字节长字符串
```
## 字节序转换宏
### uint16
| 宏 | 功能 |
|----|------|
| `BE16_GET(p)` | 大端字节序 → uint16 |
| `BE16_SET(m, n)` | uint16 → 大端字节序 |
| `LE16_GET(p)` | 小端字节序 → uint16 |
| `LE16_SET(m, n)` | uint16 → 小端字节序 |
### int32
| 宏 | 功能 |
|----|------|
| `BE32_GET(p)` / `BE32_SET(m, n)` | 大端 int32 |
| `LE32_GET(p)` / `LE32_SET(m, n)` | 小端 int32 |
### uint64
| 宏 | 功能 |
|----|------|
| `BE64_GET(p)` / `BE64_SET(m, n)` | 大端 uint64 |
| `LE64_GET(p)` / `LE64_SET(m, n)` | 小端 uint64 |
**示例**:
```c
uint8_t buf[4] = { 0x00, 0x00, 0x00, 0xFF };
uint32_t val = BE32_GET(buf);
printf("val = %u\n", val); // 255
```
## 类型化数据存取
```c
#define GET_BY_TYPE(p, type) (*(type *)(p))
#define SET_BY_TYPE(p, v, type) (*(type *)(p) = *(type *)(v))
```
## 数据类型常量22 种,用于协议编解码)
| 常量 | 值 | 含义 | 常量 | 值 | 含义 |
|------|-----|------|------|-----|------|
| `DATA_TYPE_B` | 1 | bool | `DATA_TYPE_IP` | 100 | IP 地址 |
| `DATA_TYPE_S8` | 43 | int8 | `DATA_TYPE_MAC` | 101 | MAC 地址 |
| `DATA_TYPE_U8` | 32 | uint8 | `DATA_TYPE_C8` | 102 | char[8] |
| `DATA_TYPE_S16` | 33 | int16 | `DATA_TYPE_C32` | 103 | char[32] |
| `DATA_TYPE_U16` | 45 | uint16 | `DATA_TYPE_C64` | 104 | char[64] |
| `DATA_TYPE_S32` | 2 | int32 | `DATA_TYPE_C128` | 105 | char[128] |
| `DATA_TYPE_U32` | 35 | uint32 | `DATA_TYPE_C1` | 106 | char[1] |
| `DATA_TYPE_L64` | 36 | int64 | `DATA_TYPE_STR` | 107 | string |
| `DATA_TYPE_UL64` | 37 | uint64 | | | |
| `DATA_TYPE_F32` | 38 | float | | | |
| `DATA_TYPE_D64` | 39 | double | | | |
## 依赖关系
- **依赖**: 标准 C/POSIX 头文件
- **被依赖**: 所有模块