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.youkuaiyun.com/download/dmjkun/10914228