修改命令指令

This commit is contained in:
2026-08-16 11:27:59 +08:00
parent abb8633b62
commit bdb6df0626
3 changed files with 53 additions and 27 deletions
+25 -21
View File
@@ -262,13 +262,14 @@ static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
{
/* USER CODE BEGIN 6 */
/* 命令帧解析状态机
* 帧格式: 55 AA p0 p1 p2 p3 CC (共7字节)
* 55 AA 11 11 11 11 CC -> 置采集标志
* 55 AA 00 00 00 00 CC -> 清采集标志
* 帧格式: 55 AA XX YY CC (共5字节)
* XX YY = 采集时间(ms),小端格式(XX为低字节, YY为高字节)
* 时间>0 : 启动采集,采集指定时长后自动停止
* 时间=0 : 强制停止当前采集
*/
static uint8_t rxState = 0; /* 0:等55 1:等AA 2:收4字节 3:等CC */
static uint8_t payload[4];
static uint8_t idx = 0;
static uint8_t rxState = 0; /* 0:等55 1:等AA 2:收XX 3:收YY 4:等CC */
static uint8_t timeL = 0; /* 采集时间低字节 */
static uint8_t timeH = 0; /* 采集时间高字节 */
uint32_t i;
for (i = 0U; i < *Len; i++)
@@ -287,7 +288,6 @@ static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
if (b == 0xAA)
{
rxState = 2;
idx = 0;
}
else if (b == 0x55)
{
@@ -299,26 +299,31 @@ static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
}
break;
case 2: /* 收集4字节载荷 */
payload[idx++] = b;
if (idx >= 4U)
{
rxState = 3;
}
case 2: /* 采集时间低字节 XX */
timeL = b;
rxState = 3;
break;
case 3: /* 等待帧尾 0xCC */
case 3: /* 采集时间高字节 YY */
timeH = b;
rxState = 4;
break;
case 4: /* 等待帧尾 0xCC */
if (b == 0xCC)
{
if ((payload[0] == 0x11) && (payload[1] == 0x11) &&
(payload[2] == 0x11) && (payload[3] == 0x11))
uint16_t acqMs = (uint16_t)((uint16_t)timeL | ((uint16_t)timeH << 8));
if (acqMs == 0U)
{
g_acqFlag = 1; /* 开始采集 */
/* 时间为0,强制停止当前采集 */
g_acqRemainMs = 0U;
g_acqFlag = 0U;
}
else if ((payload[0] == 0x00) && (payload[1] == 0x00) &&
(payload[2] == 0x00) && (payload[3] == 0x00))
else
{
g_acqFlag = 0; /* 停止采集 */
/* 启动采集;先置剩余时间再置标志,确保TIM3中断读到正确时长 */
g_acqRemainMs = acqMs;
g_acqFlag = 1U;
}
rxState = 0;
}
@@ -334,7 +339,6 @@ static int8_t CDC_Receive_FS(uint8_t* Buf, uint32_t *Len)
default:
rxState = 0;
idx = 0;
break;
}
}