CH376ByteRead( PUINT8 buf, UINT16 ReqCount, PUINT16 RealCount )中 RealCount是干嘛用的

版主,CH376ByteRead( PUINT8 buf, UINT16 ReqCount, PUINT16 RealCount )中 RealCount是干嘛用的,我一般就是直接将RealCount设为NULL,我现在大批量读数据出现问题,这个值有影响没,谢谢

RealCount表示真正读取的数据长度,例如一个文件只有10个字节,而 ReqCount设置为30个字节,则调用字节读取函数则RealCount为10个字节,而非30个字节,大批量读取数据出现问题可能和这个有关系,建议加上这个判断在测试下。


哦哦,我看见这个变量是个指针,那实际调用时怎么赋值呢


送一个变量进去,如果读取函数成功,则看下RealCount返回数据和你要读取的数据数量是否一致即可,真正读取到的数据位RealCount 的数据长度。而不是ReqCount数据。


谢谢啊,我今天测试了下,每次读70个字节,我定义了一个560字节的buffer,发现读到490个的时候,RealCount的值变为了140,其他时候为70,怎么回事啊,这是调用的语句CH376ByteRead(pu8Temp1, 70, &ThisLen);


那说明你有一次数据没有读取就进行下一次读取了,所以你按照实际的数据取数据看数据有没有丢失?


版主,那该怎么改呢,直接ThisLen置为NULL吗


根据你的ThisLen值取数据啊!!


版主,我之前反馈说我大批量将SD卡中的数据转存到U盘中出错的问题,现在还存在,我现在每次转存70个字节,转存前面490个字节的时候都是对的,但大于490的下一次转存时我发现ThisLen自己变成了140,读出的数据就会出错我现在把代码发给你看看,跳了好久了,麻烦看看,谢谢~ #include "Ch376INC.h" #include "FileSys.h" #include "FSMC.h" #include "Data.h" #include "systick.h" #include "stm32f10x_gpio.h" #include #include

u8 RSM1[980]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x31,0x32, 0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x31,0x32, 0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x31,0x32, 0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x31,0x32, 0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x31,0x32, 0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x31,0x32, 0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x31,0x32, 0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x31,0x32, 0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x31,0x32, 0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x31,0x32, 0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x31,0x32, 0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x31,0x32, 0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x31,0x32, 0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x31,0x32, 0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x31,0x32, 0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x31,0x32, 0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39, 0x31,0x32,0x33,0x34,0x35,0x36,0x37}; u8 RSM[560]={0}; u8 WCHH[560]={0};

//u8 WCHH[90]; u8 u8Result; u8 u8Length; u32 SD_capacity[5]; u32 *pu8Temp=SD_capacity; u8 *pu8Temp1 = WCHH; u32 total_memory; u32 memory; u32 u32FileLen = 0; u32 u32DataCnt = 0; u16* ThisLen; char SrcName[64]; char TarName[64];

//UINT8 idata buf[64]; char ch376_buf[64];

//描述: 延时函数 //名称: delay //输入参数: 无 //输出参数: 无 //返回值: 无 //说明: 无

#define HRES GPIO_SetBits(GPIOB, GPIO_Pin_12); //CH376复位信号高电平有效 #define LRES GPIO_ResetBits(GPIOB, GPIO_Pin_12); #define CH375_INT_WIRE GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)

void delay(u32 i) { while (i > 0) { i--; } }

/******************************************************************************* * 函数名:mStopIfError * 输 入: iError, 错误代码 * 输 出: * 功能说明:检查操作状态,如果错误则显示错误代码并停机, * 应该替换为实际的处理措施,例如显示错误信息,等待用户确认后重试等 */ void mStopIfError(uint8_t iError) { if (iError == USB_INT_SUCCESS) { return; /* 操作成功 */ } //printf("Error: %02X\n", iError ); /* 显示错误 */ while(1) { /* LED闪烁 */ // GPIO_ResetBits(GPIOF, GPIO_Pin_6); // mDelaymS( 200 ); // GPIO_SetBits(GPIOF, GPIO_Pin_6); // mDelaymS( 200 ); } } //描述: 查询INT引脚电平 //名称: Query376Interrupt //输入参数: 无 //输出参数: 无 //返回值: u8 -- FALSE 没有中断 // TRUE 有中断 //说明: 无 u8 Query376Interrupt(void) { //u8 temp=0; //temp=GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_15); //delay_ms(10); //return TRUE; return (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13) ? FALSE : TRUE); //0表示没有中断 非0表示有中断 }

//描述: 初始化Ch376芯片 //名称: mInitCH376Host //输入参数: 无 //输出参数: 无 //返回值: u8 -- 初始化结果 // USB_INT_SUCCESS(0x14) 初始化成功 // ERR_USB_UNKNOWN(0xFA) 初始化失败 //说明: 无

u8 mInitCH376initial(void) { u8 u8Result; FSMC_SRAM_Init(); /* 接口硬件初始化 */ GPIO_SetBits(GPIOE, GPIO_Pin_5); GPIO_ResetBits(GPIOE, GPIO_Pin_6); delay_ms(500); HRES; delay_ms(500); LRES; delay_ms(200); xWriteCH376Cmd(CMD11_CHECK_EXIST); /* 06 测试单片机与CH376之间的通讯接口 */ Write376Data(0x55); //手册上为发送55,返回0AA为正确 delay_ms(2); u8Result = Read376Data(); if (u8Result != 0xAA ) //AA { return ERR_USB_UNKNOWN; /* 通讯接口不正常,可能原因有:接口连接异常,其它设备影响(片选不唯一),串口波特率,一直在复位,晶振不工作 */ } }

u8 mInitCH376Host(void) //数据写完需要关闭文件,否则文本中数据无法更新 { u8 u8Result; FSMC_SRAM_Init(); /* 接口硬件初始化 */ GPIO_SetBits(GPIOE, GPIO_Pin_5); GPIO_ResetBits(GPIOE, GPIO_Pin_6); delay_ms(300); HRES; delay_ms(500); LRES; delay_ms(200); xWriteCH376Cmd(CMD11_CHECK_EXIST); /* 06 测试单片机与CH376之间的通讯接口 */ Write376Data(0x55); //手册上为发送55,返回0AA为正确 delay_ms(2); u8Result = Read376Data(); if (u8Result != 0xAA ) //AA { return ERR_USB_UNKNOWN; /* 通讯接口不正常,可能原因有:接口连接异常,其它设备影响(片选不唯一),串口波特率,一直在复位,晶振不工作 */ } //xWriteCH376Cmd(CMD11_SET_USB_MODE); /* 15 设备USB工作模式 */ //Write376Data(0x06); //切换到USB主机模式 xWriteCH376Cmd(CMD11_SET_USB_MODE); /* 15 SD工作模式 */ Write376Data(0x03); //S //CH376_WRITE_REGISTER(CMD11_SET_USB_MODE,0x06); delay_ms(2); u8Result = Read376Data();

if (u8Result == CMD_RET_SUCCESS) { return USB_INT_SUCCESS; } else { return(ERR_USB_UNKNOWN); /* 设置模式错误 */ }

}

void SD_CH376_read_capacity(void) { u8Result = mInitCH376Host();

if (u8Result == USB_INT_SUCCESS) { u8Result = CH376DiskMount(); // 初始化磁盘并测试磁盘是否就绪 if (u8Result != USB_INT_SUCCESS) { while (1); }

else { u8Length=CH376DiskCapacity(&total_memory); CH376DiskQuery(&memory); } } }

uint8_t s; void SD_CH376_USB_Function(void) { u8Result = mInitCH376Host();

if (u8Result == USB_INT_SUCCESS) { u8Result = CH376DiskMount(); // 初始化磁盘并测试磁盘是否就绪 if (u8Result != USB_INT_SUCCESS) { while (1); }

else { u8Result=CH376FileCreate("/WCHH.TXT"); if (u8Result == 0x14) { u8Length=CH376DiskCapacity(&total_memory); CH376DiskQuery(&memory); //u8Result=CH376FileOpen("/WCHH.TXT"); u8Result=CH376FileOpen("/WCHH.TXT"); if (u8Result == USB_INT_SUCCESS) { // 获取文件大小 u32FileLen = CH376GetFileSize();

while (u32DataCnt < u32FileLen) { CH376ByteWrite(RSM1, strlen(RSM1), 0); // u32DataCnt += 1; } //CH376FileClose(1); CH376FileClose(1); u8Result=CH376FileOpen("/WCHH.TXT"); if (u8Result == USB_INT_SUCCESS) { u32DataCnt=0; u32FileLen = CH376GetFileSize(); for(u32DataCnt=0;u32DataCnt<490;u32DataCnt+=70) { CH376ByteRead(pu8Temp1, 70, ThisLen); // CH376ByteWrite((uint8_t *)buf, strlen(buf), NULL ); pu8Temp1+=70; } //if(u32DataCnt>=(u32FileLen-2)) strcpy(RSM, WCHH ); /* 目标文件名 将WCHH中的数据复制到RSM中*/ CH376FileClose(1); // ReqCount设置为45、70时读写正确 //CH376ByteRead(RSM1, 90, 0); } } } xWriteCH376Cmd(CMD11_SET_USB_MODE); /* 15 USB工作模式 */ Write376Data(0x06); //S delay_ms(2); u8Result = Read376Data(); if (u8Result == CMD_RET_SUCCESS) { while (CH376DiskConnect() != USB_INT_SUCCESS) //检查设备是否连接 { delay_ms(500); }

u8Result = CH376DiskMount(); // 初始化磁盘并测试磁盘是否就绪 if (u8Result == USB_INT_SUCCESS) { CH376DiskQuery(&memory); u8Result=CH376FileCreate("/HUA.TXT"); u8Result=CH376FileOpen("/HUA.TXT"); if (u8Result == USB_INT_SUCCESS) { // 获取文件大小 u32DataCnt=0; u32FileLen = CH376GetFileSize(); while (u32DataCnt < u32FileLen) { CH376ByteWrite(RSM, sizeof(RSM), 0); // u32DataCnt += 1; } CH376FileClose(1); } } } //} } } else { while (1); } }

/* 向索引端口写入索引地址 */ //#define Write374Index( a ) { CH374_IDX_PORT = a; } void Write376Index(u8 mIndex ) /* 向CH374写索引地址 */ { //CH376->CH376_REG = mIndex ; CH376_CMD_PORT= mIndex ; delay_ms(2); }

/* 向数据端口写入数据,索引地址自动加1 */ //#define Write374Data( d ) { CH374_DAT_PORT = d & 0xFF; } void Write376Data( u8 mData ) { //CH376->CH376_RAM = mData ; CH376_DAT_PORT = mData ; delay_ms(2); }

/* 从数据端口读出数据,索引地址自动加1 */ //#define Read374Data( ) ( (UINT8)CH374_DAT_PORT ) u8 Read376Data( void ) { //return (CH376->CH376_RAM); delay_ms(2); return (CH376_DAT_PORT); }

/* 从索引端口读出数据,索引地址不变,适用于[读出->修改->写回]操作 */ //#define Read374Data0( ) ( (UINT8)CH374_IDX_PORT ) u8 Read376Data0( void ) { //return( CH376->CH376_RAM ); return (CH376_DAT_PORT); }

/* 从指定寄存器读取数据 */ u8 CH376_READ_REGISTER(u8 mAddr) { //CH376->CH376_REG = mAddr; CH376_CMD_PORT= mAddr ; delay_ms(2); return Read376Data(); }

/* 向指定寄存器写入数据 */ void CH376_WRITE_REGISTER(u8 mAddr, u8 mData) { CH376_CMD_PORT= mAddr ; delay_ms(2); CH376_DAT_PORT=mData; delay_ms(2);

}

void xWriteCH376Cmd(u8 u8Cmd) { u8 i; CH376_CMD_PORT= u8Cmd ; delay_ms(2); for (i=100; i!=0; --i) { delay_ms(1); if ((xReadCH376Status() & PARA_STATE_BUSY ) == 0 ) //CH376_CMD_PORT & PARA_STATE_BUSY(10) { break; } } }

//描述: 向CH376写数据 //名称: xWriteCH376Data //输入参数: u8Data -- 数据 //输出参数: 无 //返回值: 无 //说明: 无 void xWriteCH376Data(u8 u8Data) { //CH376->CH376_RAM =u8Data; CH376_DAT_PORT=u8Data; //DelayMs(2); delay_ms(2); }

//描述: 从CH376读数据 //名称: xReadCH376Data //输入参数: 无 //输出参数: 无 //返回值: u8 -- 总线数据 //说明: 无 u8 xReadCH376Data(void) { u8 u16Temp =CH376_DAT_PORT; delay_ms(2); return u16Temp; }


只有登录才能回复,可以选择微信账号登录