程浥 发表于 2018-11-21 11:49:39

串口命令行移植

本帖最后由 程浥 于 2019-4-19 17:00 编辑

本程序由FreeRTOS操作系统移植到裸机的串口上使用。

先来看一下效果:


命令文件构成:



移植注意点:
1、接收不定长串口数据,给命令行处理,demo中采用队列完成;
2、命令行内部会发送,
3、命令由链表管理,需要对其初始化

命令的格式:
cmd <param> <param> ... <param>(param的个数需要指定)
const CLI_Command_Definition_t xTest =
{
    "test", /* The command string to type. */
    "test:test <cmd> \r\n\    /* The help of the cmd. */
      'version' list the software vesion.\r\n\
      'reboot' reboot now.\r\n",
    prvTestCommand, /* The function to run. */
    1 /* The num of param. */
};

参数的使用:
解析相应的参数,以字符串相比较。
例如解析第一个参数,该参数等于“version”,就输出当前软件的版本信息。
使用的时候注意参数检测,非法参数做报错处理。
    /* Obtain the parameter string. */
    pcParameter = FreeRTOS_CLIGetParameter\
                  (
                        pcCommandString,      /* The command string itself. */
                        1,                        /* Return the first parameter. */
                        &lParameterStringLength    /* Store the parameter string length. */
                  );

    /* Sanity check something was returned. */
    configASSERT( pcParameter );


    if( strncmp( pcParameter, "version", strlen( "version" ) ) == 0 )
    {
      printf("%s\r\n",versions_tab);
    }
    else if( strncmp( pcParameter, "reboot", strlen( "reboot" ) ) == 0 )
    {
      printf("reboot now.\r\n");
      HAL_NVIC_SystemReset();
    }
    else
    {
      sprintf( pcWriteBuffer," '%s' Is Not a Valid parameters\r\n",pcParameter);
    }

附件:

(demo程序基于stm32f103的HAL库)
git:https://github.com/redocCheng/cmd

张金权-11级电子 发表于 2018-12-20 14:25:50

不明觉厉,,,,,哈哈哈哈哈

程浥 发表于 2018-12-28 17:40:34

张金权-11级电子 发表于 2018-12-20 14:25
不明觉厉,,,,,哈哈哈哈哈

不明觉厉 是什么意思

程浥 发表于 2019-4-19 16:59:26

更新:https://github.com/redocCheng/cmd
又增加了一个可选择模块。
页: [1]
查看完整版本: 串口命令行移植