UINT8 USBTestwrite( void ) { UINT8 ii; UINT8 uTemp, jj; UINT8 *pFileTemp; int dwTemp1, dwTemp2; char *pTemp = "/TestUSB.DAT";
printf(" Create USB File = \"%s\" \n", pTemp); strcpy( (char *)mCmdParam.Create.mPathName, pTemp ); /* 新文件名,在根目录下 */ ii = CH375FileCreate( ); /* 新建文件并打开,如果文件已经存在则先删除后再新建 */ if ( ii != ERR_SUCCESS ) { printf(" Create File fail \n"); return 0x03; } printf(" Write File Now \n"); mCmdParam.ByteWrite.mByteCount = 64; for( ii = 0; ii < 64; ii++ ) mCmdParam.ByteWrite.mByteBuffer[ii] = ii; ii = CH375ByteWrite ( ); { if ( ii != ERR_SUCCESS ) { printf("CH375ByteWrite fail \n"); return 0x05; } } printf(" close File Now \n"); mCmdParam.Modify.mFileSize = 64; mCmdParam.Modify.mFileDate = 0xffff; mCmdParam.Modify.mFileTime = 0xffff; mCmdParam.Modify.mFileAttr = 0xff; CH375FileModify( );
#if 1 mCmdParam.Close.mUpdateLen = 1; /* 自动计算文件长度,以字节为单位写文件,建议让程序库关闭文件以便自动更新文件长度 */ #endif ii = CH375FileClose( ); if ( ii != ERR_SUCCESS ) { printf(" Close File fail \n"); return 0x06; } strcpy( (char *)mCmdParam.Open.mPathName, "/TestUSB.DAT" ); /* Key文件名固定 */ uTemp = CH375FileOpen( ); /* 打开key文件 */ if ( uTemp != ERR_SUCCESS ) /* 打开key文件失败 */ { printf("/n Error: Open File Error,uTemp= %x\n ",uTemp); return 0x01; } printf("/n Open File OK\n ");
dwTemp1 = MAX_FILE_LENG;/*上传的最大文件长度为2M*/ dwTemp2 = 0;/*存放读取的文件长度*/ pFileTemp = &FILE_DATA_BUF[0];/*Key文件存放缓冲区*/
dwTemp1 = FILE_DATA_BUF_LEN; while ( dwTemp1 ) { if ( dwTemp1 > MAX_BYTE_IO ) uTemp = MAX_BYTE_IO; /* 剩余数据较多,限制单次读写的长度不能超过 sizeof( mCmdParam.ByteRead.mByteBuffer ) */ else uTemp = dwTemp1;/* 最后剩余的字节数 */ mCmdParam.ByteRead.mByteCount = uTemp; /* 请求读出几十字节数据 */ jj = CH375ByteRead( ); /* 以字节为单位读取数据块,单次读写的长度不能超过MAX_BYTE_IO,第二次调用时接着刚才的向后读 */ if ( jj != ERR_SUCCESS ) { CH375FileClose( ); printf("CH375ByteRead return Error %d\n", jj); return 0x02; } dwTemp1 -= mCmdParam.ByteRead.mByteCount; /* 计数,减去当前实际已经读出的字符数 */ dwTemp2 += mCmdParam.ByteRead.mByteCount; for ( jj = 0; jj != mCmdParam.ByteRead.mByteCount; jj++ ) *pFileTemp++ = mCmdParam.ByteRead.mByteBuffer[jj]; /* 将文件转到缓冲区,请注意:此地方可以做进一步的优化 */
printf( "Reading File, len: 0x%x\n", mCmdParam.ByteRead.mByteCount ); for ( jj = 0; jj != mCmdParam.ByteRead.mByteCount; jj++ ) printf( "File: 0x%x\n", mCmdParam.ByteRead.mByteBuffer[jj] ); if ( mCmdParam.ByteRead.mByteCount < uTemp ) /* 实际读出的字符数少于要求读出的字符数,说明已经到文件的结尾 */ { printf( "Read File %s Over,the length is 0x%x\n", "/ZYDQ_KEY.DAT", dwTemp2 ); break; } } CH375FileClose( );/*关闭源文件*/ return 0; }