涉及到一些二进制协议的通信,就是开头有长度、消息类型,然后各种类型分别按各自的定义来解析的那种
我以前都是用 struct 今天读代码发现有人用 array 另外 ctypes 也听说过有人用的
所以想请教一下这三个库各自的特长,如何根据场景来做选择
1
lookStupiToForce 2022-12-13 16:03:18 +08:00
|
2
lllllliu 2022-12-13 17:09:07 +08:00
```c
typedef struct dsp_buf_t { uint16_t head; uint16_t buf_len; uint16_t cmd; uint16_t data_len; uint8_t data_and_crc[]; } dsp_buf_t; static dsp_buf_t *get_buffer(size_t data_size, void *data, uint16_t cmd) { dsp_buf_t *buffer = lv_mem_alloc(sizeof(dsp_buf_t) + data_size + sizeof(uint8_t) * 2); buffer->head = 0X55AA; buffer->cmd = cmd; buffer->data_len = data_size; buffer->buf_len = buffer->data_len + 6; //cmd + data_len + data + crc lv_memcpy(buffer->data_and_crc, data, buffer->data_len); uint16_t crc = crc16_Count((uint8_t *) &buffer->cmd, buffer->data_len + 4); buffer->data_and_crc[buffer->data_len] = (crc >> 8) & 0xFF; buffer->data_and_crc[buffer->data_len + 1] = crc & 0xFF; return buffer; } ``` |
3
julyclyde OP @lookStupiToForce 看过这篇了,感觉不太够……
|
5
lovelylain 2022-12-14 10:42:44 +08:00
|
6
NoAnyLove 2022-12-18 02:05:38 +08:00
相比于 struct ,ctypes 写起来肯定更 OOP ,更简单直观一些,当然也更慢一些。另外还可以用 cffi
|