feat: 增加通信帧提取功能

This commit is contained in:
bmy
2024-02-15 22:50:20 +08:00
parent 85fe5a27df
commit 1e982b4954
18 changed files with 1154 additions and 29 deletions

View File

@@ -0,0 +1,16 @@
#include "by_tiny_frame.h"
#include <stdio.h>
#include <stdint.h>
#include "by_tiny_frame_parse.h"
#include "crc16.h"
#include "zf_common_headfile.h"
void by_tiny_frame_init(void)
{
uart_init(BY_TF_UART_INDEX, BY_TF_UART_BAUDRATE, BY_TF_UART_TX_PIN, BY_TF_UART_RX_PIN);
uart_rx_interrupt(BY_TF_UART_INDEX, ENABLE);
by_tiny_frame_parse_init();
}

View File

@@ -0,0 +1,8 @@
#ifndef _BY_TINY_FRAME_H__
#define _BY_TINY_FRAME_H__
#include "by_tiny_frame_config.h"
extern void by_tiny_frame_init(void);
#endif

View File

@@ -0,0 +1,11 @@
#ifndef _BY_TINY_FRAME_CONFIG_H__
#define _BY_TINY_FRAME_CONFIG_H__
#define BY_TF_UART_TX_PIN (UART3_MAP0_TX_B10)
#define BY_TF_UART_RX_PIN (UART3_MAP0_RX_B11)
#define BY_TF_UART_INDEX (UART_3)
#define BY_TF_UART_BAUDRATE (115200)
#define BY_TF_PARSE_BUFFER_SIZE (50)
#endif

View File

@@ -0,0 +1,97 @@
#include "by_tiny_frame_parse.h"
#include "crc16.h"
#include "lwrb.h"
lwrb_t lwrb_struct;
uint8_t buffer_rb[BY_TF_PARSE_BUFFER_SIZE];
uint8_t buffer_out;
by_tf_parse_frame_t frame_now;
void by_tiny_frame_parse_init(void)
{
lwrb_init(&lwrb_struct, buffer_rb, 40);
}
uint8_t by_tiny_frame_parse_listening(by_tf_parse_frame_t *frame_s, const uint8_t slave_id, const uint8_t buff)
{
static uint8_t cnt_s = 0;
static uint8_t cnt_rest_s = 0;
printf("%0.2X\r\n", buff);
do {
if ((0 == cnt_s) && (((slave_id << 1) + 1) == buff)) {
memset(frame_s, 0, sizeof(*frame_s));
cnt_s = 1;
cnt_rest_s = 9;
frame_s->frame[0] = buff;
break;
}
if (1 <= cnt_s) {
frame_s->frame[cnt_s] = buff;
cnt_s++;
}
if (0 == --cnt_rest_s) {
cnt_s = 0;
frame_s->cmd = frame_s->frame[1];
frame_s->reg_addr |= ((uint16_t)frame_s->frame[2] << 8);
frame_s->reg_addr |= (uint16_t)frame_s->frame[3];
frame_s->data |= ((uint32_t)frame_s->frame[4] << 24);
frame_s->data |= ((uint32_t)frame_s->frame[5] << 16);
frame_s->data |= ((uint32_t)frame_s->frame[6] << 8);
frame_s->data |= (uint32_t)frame_s->frame[7];
return 0;
}
} while (0);
return 1;
}
void by_tiny_frame_parse_uart_handle(uint8_t buff)
{
lwrb_write(&lwrb_struct, &buff, 1);
}
void by_tiny_frame_parse_run(void)
{
for (uint8_t i = 0; i < lwrb_get_full(&lwrb_struct); i++) {
if (!lwrb_read(&lwrb_struct, &buffer_out, 1)) {
break;
}
if (!by_tiny_frame_parse_listening(&frame_now, 127, buffer_out)) {
if (!by_tiny_frame_parse_crc(&frame_now)) {
printf("frame parsed!\r\n");
}
// 解析帧
}
// if (!mp_cmd_parse_modbus_handle(data)) {
// mp_cmd_mb_parse(&mp_cmd_mb_now, &mp_cmd_parsed_now);
// }
}
}
uint8_t by_tiny_frame_parse_crc(by_tf_parse_frame_t *frame_s)
{
uint16_t calc_crc_val = 0;
calc_crc_val = crc16_check(frame_s->frame, (sizeof(frame_s->frame) - 2));
printf("get: %0.2X", frame_s->crc_val);
printf("\r\n");
printf("cal: %0.2X", calc_crc_val);
printf("\r\n");
if ((frame_s->crc_val == calc_crc_val) || (frame_s->crc_val == 0xFFFF)) {
return 0;
}
return 1;
}

View File

@@ -0,0 +1,25 @@
#ifndef _BY_TINY_FRAME_PARSE_H__
#define _BY_TINY_FRAME_PARSE_H__
#include <stdio.h>
#include <stdint.h>
#include "by_tiny_frame_config.h"
// 从机地址 (1b) - 功能码 (1b) - 寄存器地址 (2b) - 数据 (4b) - CRC(2b)
// 从机地址 (1b) 0-127, 最低位表示发送方,主机请求低位为 0从机应答低位为 1
typedef struct by_tf_parse_frame_t {
uint8_t frame[10];
uint8_t cmd;
uint16_t reg_addr;
uint16_t crc_val;
uint32_t data;
} by_tf_parse_frame_t;
extern void by_tiny_frame_parse_init(void);
extern void by_tiny_frame_parse_uart_handle(uint8_t buff);
extern void by_tiny_frame_parse_run(void);
extern uint8_t by_tiny_frame_parse_crc(by_tf_parse_frame_t *frame_s);
#endif

View File

View File

@@ -0,0 +1,6 @@
#ifndef _BY_TINY_FRAME_READ_H__
#define _BY_TINY_FRAME_READ_H__
#define BY_TINY_FRAME_WRITE_CMD_CODE (0x03)
#endif

View File

@@ -0,0 +1,2 @@
#include "by_tiny_frame_write.h"

View File

@@ -0,0 +1,6 @@
#ifndef _BY_TINY_FRAME_WRITE_H__
#define _BY_TINY_FRAME_WRITE_H__
#define BY_TINY_FRAME_WRITE_CMD_CODE (0x06)
#endif