替换\n结尾,replace with $

This commit is contained in:
2025-06-28 16:09:05 +08:00
parent 10be34ec09
commit 03dba7bcc3
13 changed files with 6870 additions and 7121 deletions

View File

@@ -77,7 +77,7 @@ void HCBle_SendData(char *p,...)
// <20><><EFBFBD>ڷ<EFBFBD><DAB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD>Լ<EFBFBD><D4BC><EFBFBD>һ<EFBFBD><D2BB>DMA
// <20>ҷ<EFBFBD><D2B7>ֵ<EFBFBD><D6B5><EFBFBD><EFBFBD><EFBFBD> --- \n <20><><EFBFBD>ֿ<EFBFBD><D6BF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD>Ծ<EFBFBD>һֱ<D2BB><D6B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
{
if (huart->Instance == UART4)
@@ -87,65 +87,44 @@ void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size)
uint8_t ch = uart_dma_rx_buf[i];
uint16_t next_head = (ble_rx_ring.head + 1) % RING_BUFFER_SIZE;
if (next_head != ble_rx_ring.tail)
{
ble_rx_ring.buffer[ble_rx_ring.head] = ch;
ble_rx_ring.head = next_head;
}
if(next_head != ble_rx_ring.tail)
{
ble_rx_ring.buffer[ble_rx_ring.head] = ch;
ble_rx_ring.head = next_head;
}
}
tx_event_flags_set(&ble_event_flags, BLE_EVENT_DATA_READY, TX_OR);
tx_event_flags_set(&ble_event_flags, BLE_EVENT_DATA_READY, TX_OR);
HAL_UARTEx_ReceiveToIdle_IT(&huart4, uart_dma_rx_buf, UART_DMA_RX_BUF_SIZE);
}
}
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƕ<EFBFBD><C7B6><EFBFBD> Json<6F><6E><EFBFBD>ݴ<EFBFBD><DDB4><EFBFBD> <20>Լ<EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݸ<EFBFBD><DDB8>ֻ<EFBFBD>App
void HCBle_ParseAndHandleFrame(const char *frame)
{
int left, right;
float lat, lon, angle;
void HCBle_ExtractAndParseFrame(void) {
static char json_buf[128];
static int idx = 0;
static int parsing = 0;
// if (sscanf(frame, "#{\"leftSpeed\":%d,\"rightSpeed\":%d}", &left, &right) == 2) {
// cmd.LeftSpeed = left;
// cmd.RightSpeed = right;
// HCBle_SendData("? <20><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>: <20><>=%d, <20><>=%d\r\n", left, right);
// return;
// }
while (ble_rx_ring.tail != ble_rx_ring.head) {
char c = ble_rx_ring.buffer[ble_rx_ring.tail];
ble_rx_ring.tail = (ble_rx_ring.tail + 1) % RING_BUFFER_SIZE;
if (!parsing) {
if (c == '#') {
idx = 0;
parsing = 1;
memset(json_buf, 0, sizeof(json_buf));
json_buf[idx++] = c;
}
continue;
}
if (idx < sizeof(json_buf) - 1) {
json_buf[idx++] = c;
if (c == '\n') {
json_buf[idx] = '\0';
int left, right;
if (sscanf(json_buf, "#{\"leftSpeed\":%d,\"rightSpeed\":%d}\n", &left, &right) == 2) {
HCBle_SendData("? <20><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>: <20><>=%d, <20><>=%d\r\n", left, right);
cmd.LeftSpeed = left;
cmd.RightSpeed = right;
} else {
HCBle_SendData("? JSON<4F><4E>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>: %s\r\n", json_buf);
}
parsing = 0;
idx = 0;
}
} else {
parsing = 0;
idx = 0;
memset(json_buf, 0, sizeof(json_buf));
}
if (sscanf(frame, "%*[^0-9]%d%*[^0-9]%d", &left, &right) == 2) {
cmd.LeftSpeed = left;
cmd.RightSpeed = right;
HCBle_SendData("? <20><><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD>: <20><>=%d, <20><>=%d\r\n", left, right);
return;
}
HCBle_SendData("? <20><><EFBFBD><EFBFBD>ʧ<EFBFBD><CAA7>: %s\r\n", frame);
}
//Ϊɶһֱʹ<D6B1>ò<EFBFBD><C3B2>ˣ<EFBFBD><CBA3><EFBFBD><EFBFBD><EFBFBD>һֱ<D2BB>ٵȴ<D9B5>TX<54><58><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// <20>ں<EFBFBD>̨<EFBFBD><CCA8><EFBFBD>е<EFBFBD>һ<EFBFBD><D2BB><EFBFBD>̣߳<DFB3><CCA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȴ<EFBFBD>UART<52>յ<EFBFBD>BLE<4C><45><EFBFBD>ݺ󴥷<DDBA><F3B4A5B7><EFBFBD><EFBFBD>¼<EFBFBD><C2BC><EFBFBD>Ȼ<EFBFBD><C8BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Щ<EFBFBD><D0A9><EFBFBD><EFBFBD>
void ble_rx_task_entry(ULONG thread_input)
@@ -153,19 +132,47 @@ void ble_rx_task_entry(ULONG thread_input)
HCBle_InitDMAReception();
ULONG actual_flags;
static char json_buf[128];
static int idx = 0;
static int parsing = 0;
while (1)
{
tx_event_flags_get(&ble_event_flags, BLE_EVENT_DATA_READY, TX_OR_CLEAR, &actual_flags, TX_WAIT_FOREVER);
if (actual_flags & BLE_EVENT_DATA_READY)
while (ble_rx_ring.tail != ble_rx_ring.head)
{
HCBle_ExtractAndParseFrame();
char c = ble_rx_ring.buffer[ble_rx_ring.tail];
ble_rx_ring.tail = (ble_rx_ring.tail + 1) % RING_BUFFER_SIZE;
if (!parsing) {
if (c == '#') {
parsing = 1;
idx = 0;
json_buf[idx++] = c;
}
continue;
}
if (idx < sizeof(json_buf) - 1) {
json_buf[idx++] = c;
if (c == '$') {
json_buf[idx] = '\0';
HCBle_SendData("<EFBFBD><EFBFBD><EFBFBD><EFBFBD>json_buf: %s\r\n", json_buf);
HCBle_ParseAndHandleFrame(json_buf);
parsing = 0;
idx = 0;
}
} else {
parsing = 0;
idx = 0;
}
}
}
}
// "#{\"lat\":%.6f,\"lon\":%.6f,\"angle\":%.2f}\n",23.123456, 113.654321, 95.0);
// HCBle_SendData("#{\"lat\":%.6f,\"lon\":%.6f,\"angle\":%.2f}\n",
// current_location.lat, current_location.lon, current_location.angle);