|

楼主 |
发表于 2024-5-11 23:44:48
|
显示全部楼层
十分感谢硬汉哥的回答,我按照您说的这样,开辟了char *sBuf_HexToStr1[BUF_SIZE]这样一个指针数组,按照我的理解呢就是sBuf_HexToStr1存放我转将数据转换后的字符串,然后用fwrite写入sBuf_HexToStr1,不知道这样对不对,我这样写入之后,电脑文件打开显示还是空白,也不知道咋回事,下面是我测试存储的代码,麻烦您看看有没有问题,十分感谢
//void HexToStr(char *buf[],uint16_t data[],int datasize)
//{
// int i;
// uint16_t volt;
// for(i = 0;i < datasize;i++)
// {
// volt = data;
// sprintf(buf,"%2f",(float)volt*5000/32768);
// }
//}
#define TEST_FILE_LEN (8*1024) /* 用于测试的文件长度 */
#define BUF_SIZE (1*1024) /* 每次读写SD卡的最大数据长度 */
FRESULT res; // FatFs[函数]
FIL fil; // 文件对象
char *sBuf_HexToStr1[BUF_SIZE];
static void WriteFileTest(void);
char FsReadBuf[1024];
char FsWriteBuf[1024] = {"FatFS Write Demo \r\n www.armfly.com \r\n"};
uint16_t g_TestBuf[BUF_SIZE];
void HexToStr(char *buf[],uint16_t data[],int datasize);
int main(void)
{
u32 total,free;
static int tempLocate;
int i;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
delay_init(168); //初始化延时函数
uart_init(921600); //初始化串口波特率为115200
LED_Init(); //初始化LED
while(SD_Init())//检测不到SD卡
{
printf("SD Card Error!\r\n");
delay_ms(500);
printf("Please Check! \r\n");
delay_ms(500);
LED0=!LED0;//DS0闪烁
}
exfuns_init(); //为fatfs相关变量申请内存
f_mount(fs[0],"0:",1); //挂载SD卡
while(exf_getfree("0",&total,&free)) //得到SD卡的总容量和剩余容量
{
printf("SD Card Fatfs Error!\r\n");
delay_ms(200);
LED0=!LED0;//DS0闪烁
}
printf("总容量: %d MB ,剩余容量:%d MB\r\n",total>>10,free>>10);
// bsp_InitAD7606(); /* 配置AD7606所用的GPIO */
// AD_RANGE_5V(); //量程
// ad7606_StartConv(); /* 启动采样,避免第1组数据全0的问题 */
// ad7606_StartRecord(10000);
// printf("启动采集\r\n");// 写入文件失败
WriteFileTest();
while(1)
{
}
}
static const char * FR_Table[]=
{
"FR_OK:成功", /* (0) Succeeded */
"FR_DISK_ERR:底层硬件错误", /* (1) A hard error occurred in the low level disk I/O layer */
"FR_INT_ERR:断言失败", /* (2) Assertion failed */
"FR_NOT_READY:物理驱动没有工作", /* (3) The physical drive cannot work */
"FR_NO_FILE:文件不存在", /* (4) Could not find the file */
"FR_NO_PATH:路径不存在", /* (5) Could not find the path */
"FR_INVALID_NAME:无效文件名", /* (6) The path name format is invalid */
"FR_DENIED:由于禁止访问或者目录已满访问被拒绝", /* (7) Access denied due to prohibited access or directory full */
"FR_EXIST:文件已经存在", /* (8) Access denied due to prohibited access */
"FR_INVALID_OBJECT:文件或者目录对象无效", /* (9) The file/directory object is invalid */
"FR_WRITE_PROTECTED:物理驱动被写保护", /* (10) The physical drive is write protected */
"FR_INVALID_DRIVE:逻辑驱动号无效", /* (11) The logical drive number is invalid */
"FR_NOT_ENABLED:卷中无工作区", /* (12) The volume has no work area */
"FR_NO_FILESYSTEM:没有有效的FAT卷", /* (13) There is no valid FAT volume */
"FR_MKFS_ABORTED:由于参数错误f_mkfs()被终止", /* (14) The f_mkfs() aborted due to any parameter error */
"FR_TIMEOUT:在规定的时间内无法获得访问卷的许可", /* (15) Could not get a grant to access the volume within defined period */
"FR_LOCKED:由于文件共享策略操作被拒绝", /* (16) The operation is rejected according to the file sharing policy */
"FR_NOT_ENOUGH_CORE:无法分配长文件名工作区", /* (17) LFN working buffer could not be allocated */
"FR_TOO_MANY_OPEN_FILES:当前打开的文件数大于_FS_SHARE", /* (18) Number of open files > _FS_SHARE */
"FR_INVALID_PARAMETER:参数无效" /* (19) Given parameter is invalid */
};
static void WriteFileTest(void)
{
FRESULT result;
char path[64];
uint32_t bw;
uint32_t i,k;
uint16_t volt;
uint8_t err = 0;
static uint8_t s_ucTestSn = 0;
// char *sBuf_HexToStr1[4096];
for (i = 0; i < BUF_SIZE; i++)
{
sBuf_HexToStr1= mymalloc(SRAMIN, 20);
}
printf("sBuf_HexToStr1=%d字节\r\n",sizeof(sBuf_HexToStr1));
for (i = 0; i < sizeof(g_TestBuf); i++)
{
g_TestBuf = i*8 ;
// printf("%d\r\n",g_TestBuf);
}
// HexToStr(sBuf_HexToStr1,g_TestBuf,BUF_SIZE);
for(i = 0;i < BUF_SIZE;i++)
{
volt = g_TestBuf;
sprintf(sBuf_HexToStr1,"%0.1f",(float)volt*5000/32768);
printf("(%s)\r\n", sBuf_HexToStr1);
}
printf("(%d)\r\n", sizeof(sBuf_HexToStr1));
/* 挂载文件系统 */
result = f_mount(fs[0], "0:", 0); /* Mount a logical drive */
if (result != FR_OK)
{
printf("挂载文件系统失败 (%s)\r\n", FR_Table[result]);
}
/* 打开文件 */
sprintf(path, "0:/Speed%02d.txt", s_ucTestSn++); /* 每写1次,序号递增 */
result = f_open(&fil, path, FA_CREATE_ALWAYS | FA_WRITE);
/* 写一串数据 */
// printf("开始写文件%s %dKB ...\r\n", path, TEST_FILE_LEN / 1024);
printf("开始写文件%s %dKB ...\r\n", path, sizeof(sBuf_HexToStr1) / 1024);
// for (i = 0; i < /*TEST_FILE_LEN /*/ BUF_SIZE; i++)
// {
// sprintf(sd_buf,"%4d",g_TestBuf);
// f_write(&fil, sd_buf, 5, &bw);
// f_write(&fil, "\r\n", 2, &bw);
// }
// printf("写入完成");
for (i = 0; i < TEST_FILE_LEN/BUF_SIZE; i++)
{
result = f_write(&fil, sBuf_HexToStr1, sizeof(sBuf_HexToStr1), &bw);
printf("一次写入%d字节\r\n",bw);
if (result == FR_OK)
{
if (((i + 1) % 8) == 0)
{
printf(".");
}
}
else
{
err = 1;
printf("%s文件写失败\r\n", path);
break;
}
}
f_close(&fil); /* 关闭文件*/
// /* 卸载文件系统 */
f_mount(NULL, "0:", 0);
for (i = 0; i < BUF_SIZE; i++)
{
myfree(SRAMIN, sBuf_HexToStr1);
}
//
}
//void HexToStr(char *buf[],uint16_t data[],int datasize)
//{
// int i;
// uint16_t volt;
// for(i = 0;i < datasize;i++)
// {
// volt = data;
// sprintf(buf,"%2f",(float)volt*5000/32768);
// }
//} |
|