我使用代码对应的上位机版本为v6,具体通信协议格式在打开V6版本上位机–帮助信息–通信协议里可以看到。
这部分数据传输的核心简单的解帧。这部分的讲解以及匿名的通信协议的简介可以看另一篇文章**《串口通信协议》**
核心内容在下面一点,如果只需要研究通信协议可以直接看下面。
先来分析代码
依然注意到头文件中的一个结构体,这是一堆标志位,就不做赘述了。
typedef struct
{
u8 msg_id;
u8 msg_data;
u8 send_check;
u8 send_version;
u8 send_status;
u8 send_senser;
u8 send_senser2;
u8 send_rcdata;
u8 send_offset;
u8 send_motopwm;
u8 send_power;
u8 send_user;
u8 send_speed;
u8 send_sensorsta;
u8 send_location;
u8 send_omv_ct;
u8 send_omv_lt;
u8 send_vef;
u16 send_parame;
u16 paraToSend;
} dt_flag_t;
接下来的代码类似于任务调度,在不同时刻将不同的代码发出。(可以忽略以下一大段代码)
/
//Data_Exchange函数处理各种数据发送请求,比如想实现每5ms发送一次传感器数据至上位机,即在此函数内实现
//此函数应由用户每1ms调用一次
extern float ultra_dis_lpf;
void ANO_DT_Data_Exchange(void)
{
static u16 cnt = 0;
static u16 senser_cnt = 10;
static u16 senser2_cnt = 50;
static u16 user_cnt = 10;
static u16 status_cnt = 15;
static u16 rcdata_cnt = 20;
static u16 motopwm_cnt = 20;
static u16 power_cnt = 50;
static u16 speed_cnt = 50;
static u16 sensorsta_cnt = 500;
static u16 omv_cnt = 100;
static u16 location_cnt = 500;
static u8 flag_send_omv = 0;
if((cnt % senser_cnt) == (senser_cnt-1))
f.send_senser = 1;
if((cnt % senser2_cnt) == (senser2_cnt-1))
f.send_senser2 = 1;
if((cnt % user_cnt) == (user_cnt-2))
f.send_user = 1;
if((cnt % status_cnt) == (status_cnt-1))
f.send_status = 1;
if((cnt % rcdata_cnt) == (rcdata_cnt-1))
f.send_rcdata = 1;
if((cnt % motopwm_cnt) == (motopwm_cnt-2))
f.send_motopwm = 1;
if((cnt % power_cnt) == (power_cnt-2))
f.send_power = 1;
if((cnt % speed_cnt) == (speed_cnt-3))
f.send_speed = 1;
if((cnt % sensorsta_cnt) == (sensorsta_cnt-2))
{
f