单片机 实现 串口 命令行 cmd shell 效果

参考代码:

[原创] [STM32L496] 使用FreeRTOS任务通知实现命令行解释器
http://www.stmcu.org.cn/module/forum/thread-615444-1-1.html

E:\Nu_LB_Nuc140\work\Nu_LB_NUC140_SPI_Flash_FATFS\project\KEIL
方便自己查看

NUC100 FATFS TEST!

SPI FLASH Open success

rc=0

>?

dd [<lba>] - Dump sector

di - Initialize disk


bd <ofs> - Dump working buffer

be <ofs> [<data>] ... - Edit working buffer

br <pd#> <sect> [<num>] - Read disk into working buffer

bw <pd#> <sect> [<num>] - Write working buffer into disk

bf <val> - Fill working buffer


fi - Force initialized the logical drive

fs - Show volume status

fl [<path>] - Show a directory

fo <mode> <file> - Open a file

fc - Close the file

fe <ofs> - Move fp in normal seek

fd <len> - Read and dump the file

fr <len> - Read the file

fw <len> <val> - Write to the file

fn <object name> <new name> - Rename an object

fu <object name> - Unlink an object

fv - Truncate the file at current fp

fk <dir name> - Create a directory

fa <atrr> <mask> <object name> - Change object attribute

ft <year> <month> <day> <hour> <min> <sec> <object name> - Change timestamp of an object

fx <src file> <dst file> - Copy a file

fg <path> - Change current directory

fj <ld#> - Change current drive

fm <ld#> <rule> <cluster size> - Create file system


>


>

应用需求
  1. 串口接收中断用于接收字符流,每收到一个字符就保存在buff里面,
    如果接收到回车换行符,表明已经接收到一条完整命令
  2. 遇到 退格键 怎么办
atoi()的实现

atoi()和itoa()函数详解以及C语言实现
atoi()函数
atoi()原型: int atoi(const char *str );

函数功能:把字符串转换成整型数。

参数str:要进行转换的字符串

返回值:每个函数返回 int 值,此值由将输入字符作为数字解析而生成。 如果该输入无法转换为该类型的值,则atoi的返回值为 0。

注意:使用该函数时要注意atoi返回的是int类型,注意输入str的范围不要超出int类型的范围。

一小段代码演示一下该函数的使用:

下面来用C语言进行实现该函数:

#include <stdio.h>                                                                                                                            
#include <stdbool.h>
  
int my_atoi(const char *src)
{
      int s = 0;
      bool isMinus = false;
  
      while(*src == ' ')  //跳过空白符
      {
          src++; 
      }
  
      if(*src == '+' || *src == '-')
      {
          if(*src == '-')
          {
              isMinus = true;
          }
          src++;
      }
      else if(*src < '0' || *src > '9')  //如果第一位既不是符号也不是数字,直接返回异常值
      {
          s = 2147483647;
          return s;
      }
  
      while(*src != '\0' && *src >= '0' && *src <= '9')
      {
          s = s * 10 + *src - '0';
          src++;
      }
      return s * (isMinus ? -1 : 1);
 }

 int main()
 {
      int num;
  
      char *str = "a123456";
      num = my_atoi(str);
      printf("atoi num is: %d \r\n", num);
  
      return 0;
 }   
参考设计

/*--------------------------------------------------------------------------*/
/* Monitor                                                                  */

/*----------------------------------------------*/
/* Get a value of the string                    */
/*----------------------------------------------*/
/*	"123 -5   0x3ff 0b1111 0377  w "
	    ^                           1st call returns 123 and next ptr
	       ^                        2nd call returns -5 and next ptr
                   ^                3rd call returns 1023 and next ptr
                          ^         4th call returns 15 and next ptr
                               ^    5th call returns 255 and next ptr
                                  ^ 6th call fails and returns 0
*/

int xatoi (			/* 0:Failed, 1:Successful */
	TCHAR **str,	/* Pointer to pointer to the string */
	long *res		/* Pointer to a valiable to store the value */
)
{
	unsigned long val;
	unsigned char r, s = 0;
	TCHAR c;


	*res = 0;
	while ((c = **str) == ' ') (*str)++;	/* Skip leading spaces */

	if (c == '-') {		/* negative? */
		s = 1;
		c = *(++(*str));
	}

	if (c == '0') {
		c = *(++(*str));
		switch (c) {
		case 'x':		/* hexdecimal */
			r = 16; c = *(++(*str));
			break;
		case 'b':		/* binary */
			r = 2; c = *(++(*str));
			break;
		default:
			if (c <= ' ') return 1;	/* single zero */
			if (c < '0' || c > '9') return 0;	/* invalid char */
			r = 8;		/* octal */
		}
	} else {
		if (c < '0' || c > '9') return 0;	/* EOL or invalid char */
		r = 10;			/* decimal */
	}

	val = 0;
	while (c > ' ') {
		if (c >= 'a') c -= 0x20;
		c -= '0';
		if (c >= 17) {
			c -= 7;
			if (c <= 9) return 0;	/* invalid char */
		}
		if (c >= r) return 0;		/* invalid char for current radix */
		val = val * r + c;
		c = *(++(*str));
	}
	if (s) val = 0 - val;			/* apply sign if needed */

	*res = val;
	return 1;
}

地址

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值