你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

基于串口写了个类似shell的东西

[复制链接]
freeelectron 发布时间:2017-2-12 23:10
本帖最后由 freeelectron 于 2017-2-12 23:10 编辑

前面的话:一直对通过在超级终端里面输入命令,查看系统参数等等比较好奇,前一阵才了解到linux里面叫做shell,同样的,也可以用单片机来实现一个,方便调试,当然,也可以输入参数等等。

先上个图:
1.png
PS:其他的命令类似,正确输入命令后,按回车键

代码1:
  1. typedef void (*process_cmd)(void);
  2. typedef struct _CMD
  3. {
  4.         char *str;
  5.         char *help;
  6.         process_cmd  spectical_cmd_process;
  7. }PC_CMD;

  8. PC_CMD PC_CMD_TAB[]=
  9. {
  10.         {"help",    "List all of the valid commands.",help_process},
  11.         {"led1",    "Led1 state.",led1_process},
  12.         {"led2on",  "Enable led2.",led2on_process},
  13.         {"led2off", "Disable led2.",led2off_process},
  14.         {"reset",   "Software reset mcu.",reset_process},
  15. };
复制代码
其中:
typedef void (*process_cmd)(void);是一个函数指针,指向的函数是void类型,无参数。

typedef struct _CMD
{
        char *str;
        char *help;
        process_cmd  spectical_cmd_process;
}PC_CMD;这个是命令结构体,里面包含两个字符类型的指针,一个函数指针。


PC_CMD PC_CMD_TAB[]=
{
        {"help",    "List all of the valid commands.",help_process},
        {"led1",    "Led1 state.",led1_process},
        {"led2on",  "Enable led2.",led2on_process},
        {"led2off", "Disable led2.",led2off_process},
        {"reset",   "Software reset mcu.",reset_process},
};这个是结构体数组,我们可以看到具体的命令就是在这里面定义的。

代码2:
  1. void find_cmd(uint8_t *cmd)
  2. {        
  3.         uint8_t  cnt=0;
  4.         for(cnt=0;cnt<sizeof(PC_CMD_TAB)/sizeof(PC_CMD_TAB[0]);cnt++)
  5.         {
  6.                 if(strcmp(PC_CMD_TAB[cnt].str,(char*)cmd)==0)
  7.                 {
  8.                         buff_cnt=0;
  9.                         PC_CMD_TAB[cnt].spectical_cmd_process();
  10.                         return;
  11.                 }
  12.         }
  13.         buff_cnt=0;
  14.         printf("\r\nCmd error.\r\n");
  15. }
复制代码
说明:这个函数主要是用于遍历命令,并且执行命令对应的函数。

附上代码:基于stm32l1的工程代码。
L1(shell).rar (5.24 MB, 下载次数: 386)

评分

参与人数 1 ST金币 +5 收起 理由
creep + 5 很给力!

查看全部评分

1 收藏 5 评论21 发布时间:2017-2-12 23:10

举报

21个回答
creep 回答时间:2017-2-13 08:43:53
感谢分享      
hwb_dream 回答时间:2017-2-13 08:54:11
谢谢分享            
freeelectron 回答时间:2017-2-13 08:58:27
Mandelbrot_Set 回答时间:2017-2-13 09:08:29
楼主给力
斜阳__ 回答时间:2017-2-13 09:21:27
mark一下
freeelectron 回答时间:2017-2-13 09:23:13
freeelectron 回答时间:2017-2-13 09:23:52

共同学习,帮我顶一下这个帖子,谢谢https://www.stmcu.org.cn/module/forum/thread-610437-1-1.html:)
toofree 回答时间:2017-2-13 11:18:38
很好,也想这么弄。
freeelectron 回答时间:2017-2-13 11:43:56
本帖最后由 freeelectron 于 2017-2-13 12:52 编辑
toofree 发表于 2017-2-13 11:18
很好,也想这么弄。

欢迎指导,帮我顶一下这个帖子,谢谢https://www.stmcu.org.cn/module/forum/thread-610437-1-1.html
123下一页

所属标签

STM32团队

意法半导体微控制器和微处理器拥有广泛的产品线,包含低成本的8位单片机和基于ARM® Cortex®-M0、M0+、M3、M4、M33、M7及A7内核并具备丰富外设选择的32位微控制器及微处理器


最新内容

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版