gateway_mcu/CH32V303-FreeRTOS/App/application/Slave/Src/slaveMcuDataParse.c

287 lines
6.5 KiB
C
Raw 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.

#include "slaveMcuDataParse.h"
#include "slaveMcuComm.h"
static void stateMachine(void);
/* 状态机函数 */
static uint8_t analysisWait(void);
static uint8_t analysisStartFlagSL(void);
static uint8_t analysisPortSL(void);
static uint8_t analysisLengthSL(void);
static uint8_t analysisEndFlagSL(void);
/**
* @brief 状态机函数
* @param
* @retval
*/
void stateMachine(void)
{
if (state == wait) {
if (analysisWait() == TRUE) {
mcuUartRxTime = xTaskGetTickCount();
}
}
else if (state == startFlagSL) {
analysisStartFlagSL();
}
else if (state == portSL) {
analysisPortSL();
}
else if (state == lengthSL) {
analysisLengthSL();
}
else if (state == endFlagSL) {
analysisEndFlagSL();
}
}
/**
* @brief 状态 wait
* @param
* @retval 0:解析失败
1:解析成功
*/
#define waitMaxLen 2
uint8_t analysisWait(void)
{
if (mcuUartRxBufferIndex >= 2) {
/* 透传数据帧包头 */
if (mcuUartRxBuffer[0] == 'S' && mcuUartRxBuffer[1] == 'L') {
// log_info("startFlagSL\n");
state = startFlagSL;
return TRUE;
}
}
if (mcuUartRxBufferIndex < waitMaxLen) {
return FALSE;
}
state = wait;
mcuUartRxBufferIndex--;
memcpy(mcuUartRxBuffer, mcuUartRxBuffer + 1, mcuUartRxBufferIndex);
return FALSE;
}
/**
* @brief 状态 startFlagSL
* @param
* @retval 0:解析失败
1:解析成功
*/
uint8_t analysisStartFlagSL(void)
{
/* 校验端口号 */
if (mcuUartRxBuffer[2] < 13) {
// log_info("portSL\n");
state = portSL;
return TRUE;
}
// log_error("portSL %d \n", gw485RxBuffer[2]);
state = wait;
mcuUartRxBufferIndex--;
memcpy(mcuUartRxBuffer, mcuUartRxBuffer + 1, mcuUartRxBufferIndex);
return FALSE;
}
/**
* @brief 状态 portSL
* @param
* @retval 0:解析失败
1:解析成功
*/
#define PortSLMaxLen 5
uint8_t analysisPortSL(void)
{
/* 解析数据包的度 */
if (mcuUartRxBufferIndex >= PortSLMaxLen) {
uint32_t tempLen = 0;
tempLen = (mcuUartRxBuffer[3] << 8) | mcuUartRxBuffer[4];
if (tempLen <= 1024) {
// log_info("lengthSL\n");
state = lengthSL;
frameLength = 6 + tempLen;
return TRUE;
}
}
if (mcuUartRxBufferIndex < PortSLMaxLen) {
return FALSE;
}
// log_error("lengthSL %d \n", (gw485RxBuffer[3] << 8) | gw485RxBuffer[4]);
state = wait;
mcuUartRxBufferIndex--;
memcpy(mcuUartRxBuffer, mcuUartRxBuffer + 1, mcuUartRxBufferIndex);
return FALSE;
}
/**
* @brief 状态 lengthSL
* @param
* @retval 0:解析失败
1:解析成功
*/
#define LengthSLMaxLen frameLength
uint8_t analysisLengthSL(void)
{
/* 解析帧尾 */
if (mcuUartRxBufferIndex >= LengthSLMaxLen) {
if (mcuUartRxBuffer[LengthSLMaxLen - 1] == 0x17) {
// log_info("endFlagSL\n");
state = endFlagSL;
return TRUE;
}
}
if (mcuUartRxBufferIndex < LengthSLMaxLen) {
return FALSE;
}
// log_info("endFlagSL %d\n", gw485RxBuffer[LengthSLMaxLen - 1]);
state = wait;
mcuUartRxBufferIndex--;
memcpy(mcuUartRxBuffer, mcuUartRxBuffer + 1, mcuUartRxBufferIndex);
return FALSE;
}
/**
* @brief 状态 endFlagSL
* @param
* @retval 0:解析失败
1:解析成功
*/
uint8_t analysisEndFlagSL(void)
{
uint32_t tempLen = 0;
tempLen = (mcuUartRxBuffer[3] << 8) | mcuUartRxBuffer[4];
if (mcuUartRxBuffer[2] == SlaveMcu) {
slaveFrameDataAnalyze(mcuUartRxBuffer, mcuUartRxBufferIndex);
mcuUartRxBufferIndex = 0;
state = wait;
return 1;
}
//系统内存不足,丢去当前包
if (xPortGetFreeHeapSize() < tempLen + 1024) {
goto err;
}
/* 对于不同通端口,传输的结构体不同 */
uint8_t *Buff;
Buff = (uint8_t *)pvPortMalloc(tempLen + slaveQueueUartSendInfoSize);
slaveQueueUartSendInfo *sendBuff = (slaveQueueUartSendInfo *)Buff;
sendBuff->length = tempLen;
sendBuff->data = Buff + slaveQueueUartSendInfoSize;
memcpy((char *)sendBuff->data, (char *)&mcuUartRxBuffer[5], tempLen);
/* 通过不同的端口将数据发送到不同的地方 */
if (mcuUartRxBuffer[2] == J1) {
if (uxQueueSpacesAvailable(J1_485_Queue)) {
xQueueSend(J1_485_Queue, &Buff, 10);
}
/* 队列中无空间,释放内存,退出 */
else {
vPortFree(Buff);
}
}
else if (mcuUartRxBuffer[2] == J3) {
if (uxQueueSpacesAvailable(J3_485_Queue)) {
xQueueSend(J3_485_Queue, &Buff, 10);
}
/* 队列中无空间,释放内存,退出 */
else {
vPortFree(Buff);
}
}
else if (mcuUartRxBuffer[2] == J5) {
if (uxQueueSpacesAvailable(J5_485_Queue)) {
xQueueSend(J5_485_Queue, &Buff, 10);
}
/* 队列中无空间,释放内存,退出 */
else {
vPortFree(Buff);
}
}
else if (mcuUartRxBuffer[2] == J7) {
if (uxQueueSpacesAvailable(J7_485_Queue)) {
xQueueSend(J7_485_Queue, &Buff, 10);
}
/* 队列中无空间,释放内存,退出 */
else {
vPortFree(Buff);
}
}
else if (mcuUartRxBuffer[2] == J8) {
if (uxQueueSpacesAvailable(J8_485_Queue)) {
xQueueSend(J8_485_Queue, &Buff, 10);
}
/* 队列中无空间,释放内存,退出 */
else {
vPortFree(Buff);
}
}
else if (mcuUartRxBuffer[2] == J9) {
if (uxQueueSpacesAvailable(J9_485_Queue)) {
xQueueSend(J9_485_Queue, &Buff, 10);
}
/* 队列中无空间,释放内存,退出 */
else {
vPortFree(Buff);
}
}
err:
//清零buff
state = wait;
mcuUartRxBufferIndex = 0;
return 1;
}
/**
* @brief 解析智能模块发送来的数据
* @param device 用于接收智能模块发送来的数据的串口设备
* @retval
*/
void slaveMcuUartDataAnalysis(device_handle device)
{
/* 每次函数最多执行10ms */
static uint32_t tickstart = 0U;
tickstart = xTaskGetTickCount();
/* 2S未解析出来一帧数据将数据清零 */
if (getTickDiff(mcuUartRxTime) >= tick_2S) {
mcuUartRxTime = xTaskGetTickCount();
mcuUartRxBufferIndex = 0;
state = wait;
}
while (uart_dev_char_present(device) == 1 && ((xTaskGetTickCount() - tickstart) < 5)) {
mcuUartRxBuffer[mcuUartRxBufferIndex++] = uart_dev_in_char(device);
stateMachine();
}
if (uart_dev_char_present(device) != 1 && state != wait) {
stateMachine();
}
}