FreeRTOS移植Xmodem通过命令行上传下载文件

Xmodem协议可以很方便通过串口与PC串口终端进行数据交换,主要修改_outbyte与_inbyte两个函数

static void _outbyte(int c)
{
        uart_console_send((uint8_t*)&c,1);
}
static int _inbyte(unsigned short timeout) // msec timeout
{
        unsigned char c;
		uint16_t i;
		for( i=0;i<timeout; i++ ){
			
			if( uart_console_recv_time(&c,0) != 0 ){
				break;
			}
			vTaskDelay( 1 );
		}
		if( i==timeout )
			return -2;

        return c;
}

两个函数需修改调用一次发送或接收一个字节的串口函数

FreeRTOS CLI接口下载函数

static BaseType_t console_user_download_execute( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString ){
	
	( void ) pcCommandString;
	( void ) xWriteBufferLen;
	configASSERT( pcWriteBuffer );
	
	if( login_flag==0 ){
		
		strcpy(pcWriteBuffer,"请先登录\r\n");
		return pdFALSE;
	}
	
	const char *pcParameter;
	BaseType_t xParameterStringLength, xReturn;
	char para[2][20];
	uint32_t size;
	for( uint8_t i=1; i<=2; i++ ){
		pcParameter = FreeRTOS_CLIGetParameter(pcCommandString,i,&xParameterStringLength);

		if( pcParameter != NULL ){
			
			uint8_t len;
			len = xParameterStringLength>20?20:xParameterStringLength;
			strncpy( para[i-1], pcParameter, len );
			para[i-1][len] = 0;
			if( i==2 )
				size = atoi(para[1]);
		}
	}

	if( pcParameter != NULL ){
		uint32_t recv_len;
		
		recv_len = xmodem_receive_file(para[0],size);
		
		*pcWriteBuffer = 0;
		
		sprintf(pcWriteBuffer,"下载文件成功,大小:%d\r\n",recv_len);
	}
	else{
		strcpy(pcWriteBuffer,"参数错误");
	}
	
	return pdFALSE;
}

static const CLI_Command_Definition_t console_user_download ={
	
	"download",
	"\r\ndownload:	download <filename> <size> ---- 从PC端接收文件,注意:将会删除同名文件,文件名,接收大小",
	console_user_download_execute,
	2
};

终端中键入download filename filesize即可将文件从PC端传输到设备端

FreeRTOS CLI接口上传函数

static BaseType_t console_user_upload_execute( char *pcWriteBuffer, size_t xWriteBufferLen, const char *pcCommandString ){
	
	( void ) pcCommandString;
	( void ) xWriteBufferLen;
	configASSERT( pcWriteBuffer );
	
	if( login_flag==0 ){
		
		strcpy(pcWriteBuffer,"请先登录\r\n");
		return pdFALSE;
	}
	
	const char *pcParameter;
	BaseType_t xParameterStringLength, xReturn;
	char buf[20];
	
	pcParameter = FreeRTOS_CLIGetParameter(pcCommandString,1,&xParameterStringLength);

	if( pcParameter != NULL ){
		uint8_t len;
		len = xParameterStringLength>20?20:xParameterStringLength;
		strncpy( buf, pcParameter, len );
		buf[len] = 0;
		int32_t size;
		
		size = xmodem_transmit_file(buf);
		
		if( size>0 )
			sprintf(pcWriteBuffer,"上传文件成功,大小:%d\r\n",size);
		else
			sprintf(pcWriteBuffer,"上传文件失败\r\n");
	}
	else{
		strcpy(pcWriteBuffer,"参数错误");
	}
	
	return pdFALSE;
}

static const CLI_Command_Definition_t console_user_upload ={
	
	"upload",
	"\r\nupload:		upload <filename> ---- 上传文件到PC端",
	console_user_upload_execute,
	1
};

终端中键入upload filename 即可将文件从设备端传输到PC端
源码下载地址:https://download.csdn.net/download/dmjkun/10914228

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

纵向深耕

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值