[Upload Code]:add SerializeFrame Code
This commit is contained in:
parent
237c17e181
commit
3bdf15cce7
@ -52,6 +52,7 @@ typedef struct __attribute__((packed))
|
||||
|
||||
void Protocol_BuildFrame(ProtocolFrame *frame, uint8_t frame_ctl, uint16_t seq,
|
||||
const uint8_t *payload, uint16_t payload_len, bool enable_crc);
|
||||
uint16_t Protocol_SerializeFrame(uint8_t *buffer, uint16_t buffer_size, const ProtocolFrame *frame);
|
||||
|
||||
bool Protocol_ParseFrame(const uint8_t *data, uint16_t len, ProtocolFrame *frame);
|
||||
uint16_t Protocol_CRC16_CCITT(uint16_t pre_crc, const uint8_t *data, uint16_t len);
|
||||
|
@ -14,7 +14,7 @@ static uint8_t send_buf[512];
|
||||
static uint8_t recv_buf[512];
|
||||
static uint16_t recv_len = 0;
|
||||
static rt_tick_t start_tick = 0;
|
||||
static rt_uint16_t seq_num = 1;
|
||||
static rt_uint16_t seq_num = 0xffff;
|
||||
static rt_uint16_t Upload_Time = 100;
|
||||
static CommState comm_state = STATE_WAIT_RECV;
|
||||
|
||||
@ -36,9 +36,9 @@ static void upload_StatusPackage(void)
|
||||
myPayload,
|
||||
sizeof(myPayload),
|
||||
true);
|
||||
// uart_send((uint8_t *)&resp, 8 + sizeof(payload) + 2);
|
||||
hex_dump_simple((uint8_t *)&resp, 8 + sizeof(myPayload) + 2);
|
||||
rt_memcpy(send_buf, (uint8_t *)&resp, 8 + sizeof(myPayload) + 2);
|
||||
Protocol_SerializeFrame(send_buf, sizeof(send_buf), &resp);
|
||||
HAL_UART_Transmit(&huart3, send_buf, 8 + sizeof(myPayload) + 2, 100);
|
||||
hex_dump_simple(send_buf, 8 + sizeof(myPayload) + 2);
|
||||
}
|
||||
|
||||
// 状态机处理
|
||||
@ -50,7 +50,7 @@ static void comm_state_machine_run(void)
|
||||
case STATE_WAIT_RECV:
|
||||
{
|
||||
// rt_kprintf("Wait Message...\n");
|
||||
if (rt_event_recv(&output_uart_rx_event, 0x01, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, RT_WAITING_FOREVER, &e) == RT_EOK)
|
||||
if (rt_event_recv(&output_uart_rx_event, 0x01, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, 100, &e) == RT_EOK)
|
||||
{
|
||||
rt_kprintf("Deal Data Ready...\n");
|
||||
hex_dump_simple(output_uart_rx_buffer, output_uart_rx_len);
|
||||
@ -107,8 +107,10 @@ static void comm_state_machine_run(void)
|
||||
payload,
|
||||
sizeof(payload),
|
||||
true);
|
||||
// uart_send((uint8_t *)&resp, 8 + sizeof(payload) + 2);
|
||||
hex_dump_simple((uint8_t *)&resp, 8 + sizeof(payload) + 2);
|
||||
// 发送 数据
|
||||
Protocol_SerializeFrame(send_buf, sizeof(send_buf), &resp);
|
||||
HAL_UART_Transmit(&huart3, send_buf, 8 + sizeof(payload) + 2, 100);
|
||||
hex_dump_simple(send_buf, 8 + sizeof(payload) + 2);
|
||||
}
|
||||
// 设置状态包上报间隔
|
||||
else if (mcmd == 0x02 && scmd == 0x10)
|
||||
@ -131,8 +133,10 @@ static void comm_state_machine_run(void)
|
||||
payload,
|
||||
sizeof(payload),
|
||||
true);
|
||||
// uart_send((uint8_t *)&resp, 8 + sizeof(payload) + 2);
|
||||
hex_dump_simple((uint8_t *)&resp, 8 + sizeof(payload) + 2);
|
||||
// 发送 数据
|
||||
Protocol_SerializeFrame(send_buf, sizeof(send_buf), &resp);
|
||||
HAL_UART_Transmit(&huart3, send_buf, 8 + sizeof(payload) + 2, 100);
|
||||
hex_dump_simple(send_buf, 8 + sizeof(payload) + 2);
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -174,8 +178,7 @@ static void output_thread_entry(void *parameter)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
// comm_state_machine_run();
|
||||
upload_StatusPackage();
|
||||
comm_state_machine_run();
|
||||
rt_thread_mdelay(15);
|
||||
}
|
||||
}
|
||||
|
@ -44,9 +44,9 @@ void Protocol_BuildFrame(ProtocolFrame *frame, uint8_t frame_ctl, uint16_t seq,
|
||||
frame->head[0] = FRAME_HEAD_1;
|
||||
frame->head[1] = FRAME_HEAD_2;
|
||||
frame->version = VERSION;
|
||||
frame->frame_ctl = frame_ctl;
|
||||
frame->seq_num = BSWAP16(seq);
|
||||
frame->payload_len = BSWAP16(payload_len);
|
||||
frame->frame_ctl = (frame_ctl);
|
||||
frame->seq_num = (seq);
|
||||
frame->payload_len = (payload_len);
|
||||
|
||||
rt_memcpy(frame->payload, payload, payload_len);
|
||||
|
||||
@ -58,8 +58,7 @@ void Protocol_BuildFrame(ProtocolFrame *frame, uint8_t frame_ctl, uint16_t seq,
|
||||
if (enable_crc)
|
||||
{
|
||||
crc = Protocol_CRC16_CCITT(0xFFFF, (uint8_t *)frame, 6 + payload_len);
|
||||
// 高低位交换
|
||||
frame->crc = BSWAP16(crc);
|
||||
frame->crc = crc;
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -104,3 +103,53 @@ bool Protocol_ParseFrame(const uint8_t *data, uint16_t len, ProtocolFrame *frame
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将ProtocolFrame结构体的值序列化到字节数组
|
||||
* @param buffer 目标缓冲区
|
||||
* @param buffer_size 缓冲区大小
|
||||
* @param frame 源ProtocolFrame结构体
|
||||
* @return 实际写入的字节数,若缓冲区不足则返回0
|
||||
*/
|
||||
uint16_t Protocol_SerializeFrame(uint8_t *buffer, uint16_t buffer_size, const ProtocolFrame *frame)
|
||||
{
|
||||
uint16_t offset = 0;
|
||||
uint16_t required_size = sizeof(frame->head) + sizeof(frame->version) +
|
||||
sizeof(frame->frame_ctl) + sizeof(frame->seq_num) +
|
||||
sizeof(frame->payload_len) + frame->payload_len +
|
||||
sizeof(frame->crc);
|
||||
|
||||
// 检查缓冲区是否足够大
|
||||
if (buffer_size < required_size)
|
||||
{
|
||||
return 0; // 缓冲区不足
|
||||
}
|
||||
|
||||
// 复制头部
|
||||
rt_memcpy(buffer + offset, frame->head, sizeof(frame->head));
|
||||
offset += sizeof(frame->head);
|
||||
|
||||
// 复制版本号
|
||||
buffer[offset++] = frame->version;
|
||||
|
||||
// 复制帧控制字段
|
||||
buffer[offset++] = frame->frame_ctl;
|
||||
|
||||
// 复制序列号(按大端格式写入)
|
||||
buffer[offset++] = (frame->seq_num >> 8) & 0xFF;
|
||||
buffer[offset++] = frame->seq_num & 0xFF;
|
||||
|
||||
// 复制负载长度(按大端格式写入)
|
||||
buffer[offset++] = (frame->payload_len >> 8) & 0xFF;
|
||||
buffer[offset++] = frame->payload_len & 0xFF;
|
||||
|
||||
// 复制负载数据
|
||||
rt_memcpy(buffer + offset, frame->payload, frame->payload_len);
|
||||
offset += frame->payload_len;
|
||||
|
||||
// 复制CRC校验值(按大端格式写入)
|
||||
buffer[offset++] = (frame->crc >> 8) & 0xFF;
|
||||
buffer[offset++] = frame->crc & 0xFF;
|
||||
|
||||
return offset; // 返回实际写入的字节数
|
||||
}
|
||||
|
File diff suppressed because one or more lines are too long
@ -158,40 +158,7 @@
|
||||
<Name>-U-O142 -O2254 -S0 -C0 -N00("ARM CoreSight SW-DP") -D00(2BA01477) -L00(0) -TO18 -TC10000000 -TP21 -TDS8007 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -FO7 -FD20000000 -FC800 -FN1 -FF0STM32F4xx_1024.FLM -FS08000000 -FL0100000 -FP0($$Device:STM32F405RGTx$CMSIS\Flash\STM32F4xx_1024.FLM)</Name>
|
||||
</SetRegEntry>
|
||||
</TargetDriverDllRegistry>
|
||||
<Breakpoint>
|
||||
<Bp>
|
||||
<Number>0</Number>
|
||||
<Type>0</Type>
|
||||
<LineNumber>60</LineNumber>
|
||||
<EnabledFlag>1</EnabledFlag>
|
||||
<Address>134227730</Address>
|
||||
<ByteObject>0</ByteObject>
|
||||
<HtxType>0</HtxType>
|
||||
<ManyObjects>0</ManyObjects>
|
||||
<SizeOfObject>0</SizeOfObject>
|
||||
<BreakByAccess>0</BreakByAccess>
|
||||
<BreakIfRCount>1</BreakIfRCount>
|
||||
<Filename>..\Core\Src\protocol_parser.c</Filename>
|
||||
<ExecCommand></ExecCommand>
|
||||
<Expression>\\XM_01\../Core/Src/protocol_parser.c\60</Expression>
|
||||
</Bp>
|
||||
<Bp>
|
||||
<Number>1</Number>
|
||||
<Type>0</Type>
|
||||
<LineNumber>41</LineNumber>
|
||||
<EnabledFlag>1</EnabledFlag>
|
||||
<Address>134255062</Address>
|
||||
<ByteObject>0</ByteObject>
|
||||
<HtxType>0</HtxType>
|
||||
<ManyObjects>0</ManyObjects>
|
||||
<SizeOfObject>0</SizeOfObject>
|
||||
<BreakByAccess>0</BreakByAccess>
|
||||
<BreakIfRCount>1</BreakIfRCount>
|
||||
<Filename>..\Core\Src\myOutput_deal.c</Filename>
|
||||
<ExecCommand></ExecCommand>
|
||||
<Expression>\\XM_01\../Core/Src/myOutput_deal.c\41</Expression>
|
||||
</Bp>
|
||||
</Breakpoint>
|
||||
<Breakpoint/>
|
||||
<WatchWindow1>
|
||||
<Ww>
|
||||
<count>0</count>
|
||||
@ -208,6 +175,11 @@
|
||||
<WinNumber>1</WinNumber>
|
||||
<ItemText>send_buf</ItemText>
|
||||
</Ww>
|
||||
<Ww>
|
||||
<count>3</count>
|
||||
<WinNumber>1</WinNumber>
|
||||
<ItemText>resp</ItemText>
|
||||
</Ww>
|
||||
</WatchWindow1>
|
||||
<Tracepoint>
|
||||
<THDelay>0</THDelay>
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user