【AT-START-F425测评】USB转CAN之五 串口命令解释
[复制链接]
【AT-START-F425测评】USB转CAN 预告帖 https://bbs.eeworld.com.cn/thread-1199896-1-1.html
【AT-START-F425测评】USB转CAN之二 https://bbs.eeworld.com.cn/thread-1199901-1-1.html
【AT-START-F425测评】USB转CAN之三 https://bbs.eeworld.com.cn/thread-1200055-1-1.html
【AT-START-F425测评】USB转CAN之四 完成收发测试 https://bbs.eeworld.com.cn/thread-1200061-1-1.html
接上面4篇帖子堆代码:
先借鉴别人的帖子把命令提示跟警示用USB串口输出来,本来想用printf写出来的,可能不如直接用usb发出来快所以代码有点乱:
void HELPText(void)
{
// char usb_send_buff[40];
// sprintf(usb_send_buff,"**********************************************罗斯维尔电子技术淘宝旗舰店***************************************\n");
usb_vcp_send_data(&otg_core_struct.dev,"*********************lugl@qq.com*****************************\n",sizeof("*********************lugl@qq.com*****************************\n")-1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev,"设计:lugl\n",sizeof("设计:lugl\n")-1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev,"\n",1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev,"\n",1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev," 1.CAN速率自动检索侦听(基于OBD接口的CAN) 指令: SCANOBD \n",sizeof(" 1.CAN速率自动检索侦听(基于OBD接口的CAN) 指令: SCANOBD \n")-1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev,"\n",1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev," 2.标准CAN数据滤波侦听 指令: STDxxxxxxx XXX XXX XXX XXX \n",sizeof(" 2.标准CAN数据滤波侦听 指令: STDxxxxxxx XXX XXX XXX XXX \n")-1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev," 例子: 设置500K速率侦听ID为 7E0 和 7E8 的数据 \n",sizeof(" 例子: 设置500K速率侦听ID为 7E0 和 7E8 的数据 \n")-1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev," STD0500000 7E0 7E8 7FF 7FF \n",sizeof(" STD0500000 7E0 7E8 7FF 7FF \n")-1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev,"\n",1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev," 3.扩展CAN数据滤波侦听 指令: EXTxxxxxxx XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX \n",sizeof(" 3.扩展CAN数据滤波侦听 指令: EXTxxxxxxx XXXXXXXX XXXXXXXX XXXXXXXX XXXXXXXX \n")-1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev," 例子: 设置33.3K速率侦听ID为 18DAF110 18DAF118 \n",sizeof(" 例子: 设置33.3K速率侦听ID为 18DAF110 18DAF118 \n")-1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev," EXT0033300 18DAF110 18DAF118 1FFFFFFF 1FFFFFFF \n",sizeof(" EXT0033300 18DAF110 18DAF118 1FFFFFFF 1FFFFFFF \n")-1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev,"\n",1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev," 4.标准CAN数据发送 指令:SENDSTDxxxxxxx XXX X XX XX XX XX XX XX XX XX \n",sizeof(" 4.标准CAN数据发送 指令:SENDSTDxxxxxxx XXX X XX XX XX XX XX XX XX XX \n")-1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev," 例子:发送250K速率的标准CAN数据 7E0 8 01 02 03 04 05 06 07 08 \n",sizeof(" 例子:发送250K速率的标准CAN数据 7E0 8 01 02 03 04 05 06 07 08 \n")-1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev," SENDSTD0250000 7E0 8 01 02 03 04 05 06 07 08 \n",sizeof(" SENDSTD0250000 7E0 8 01 02 03 04 05 06 07 08 \n")-1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev,"\n",1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev," 5.扩展CAN数据发送 指令:SENDEXTxxxxxxx XXXXXXXX X XX XX XX XX XX XX XX XX \n",sizeof(" 5.扩展CAN数据发送 指令:SENDEXTxxxxxxx XXXXXXXX X XX XX XX XX XX XX XX XX \n")-1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev," 例子:发送500K速率的扩展CAN数据 18DAF110 3 01 02 03 \n",sizeof(" 例子:发送500K速率的扩展CAN数据 18DAF110 3 01 02 03 \n")-1);
usb_vcp_send_data(&otg_core_struct.dev," SENDEXT0500000 18DAF110 3 01 02 03 \n",sizeof(" SENDEXT0500000 18DAF110 3 01 02 03 \n")-1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev,"\n",1);
CotrolVale = 0;
}
void WarningText(void)
{
usb_vcp_send_data(&otg_core_struct.dev," 指令无效!\n",sizeof(" 指令无效!\n")-1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev,"\n",1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev," 提示:输入内容字母必须为大写;\n",sizeof(" 提示:输入内容字母必须为大写;\n")-1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev,"\n",1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev," 字符间间隔必须为一个空格,多则无效;\n",sizeof(" 字符间间隔必须为一个空格,多则无效;\n")-1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev,"\n",1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev," 详细指令信息,可输入指令\"HELP\"查阅。\n",sizeof(" 详细指令信息,可输入指令\"HELP\"查阅。\n")-1);
usb_delay_ms(10);
usb_vcp_send_data(&otg_core_struct.dev,"\n",1);
usb_delay_ms(10);
CotrolVale = 0;
}
在主程序中对usb串口接收到的串口命令进行解析:
/**
* [url=home.php?mod=space&uid=159083]@brief[/url] 这是一个命令解释器
* @param none
* @retval none
*/
void cmd_exp(void)
{
uint16_t data_len;
data_len = usb_vcp_get_rxdata(&otg_core_struct.dev, Cmd);
if(data_len>0)
{
if (!strncmp((const char *)Cmd,"HELP",4) && data_len == 4)
{
CotrolVale = 0x01;
}
else if (!strncmp((const char *)Cmd,"SCANOBD",7))
{
CotrolVale = 0x02;
}
else if (!strncmp((const char *)Cmd,"STD",3))
{
CotrolVale = 0x03;
}
else if (!strncmp((const char *)Cmd,"EXT",3))
{
CotrolVale = 0x04;
}
else if (!strncmp((const char *)Cmd,"SEND",4))
{
CotrolVale = 0x05;
}
else
{
CotrolVale = 0x80;
}
}
cc = 0;
}
然在的main中调用,按照当前状态作出判断:
while(1)
{
cmd_exp();
if(can_rec_state ==1)
{
can_test();
can_rec_state = 0;
}
if (CotrolVale == 0x01)
{
HELPText();
}
if (CotrolVale == 0x80)
{
WarningText();
}
}
}
实验效果:发送HELP命令:
随便发点什么:
基本上命令解析已经完成。下一步按照相应的命令对CAN进行动态配置。
还有一个好消息是,前段时我在迪文屏的试用中得了优秀奖,中了1000块奖金。然后,我又从迪文屏申请到两块屏,做一个新冠核酸采样的工程。今天看快递信息,应该是通过了。我将在这里与迪文屏论坛进行同步帖子。到时希望各位给予捧场,指导。又是一场硬仗呀!
|