前言
本章测试sd卡的读写速度,编写代码实现书籍信息录入。
sd卡读写测试
通过dmesg查看kernel log
不支持F2FS
支持 fat32
【命令】fdisk -l
读写测试
写入:dd if=/dev/zero of=/run/media/mmcblk1p1/test bs=1M count=500
conv=fsync
读取:dd if=/run/media/mmcblk1p1/test of=/dev/null bs=1M
读取txt文档
书籍录入
串口屏内输入书籍信息,存储进sd卡的txt文件中,在设备上读取文件。
sd卡路径:/run/media/mmcblk1p1
【命令】cat /run/media/mmcblk1p1/b.txt
代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "stdint.h"
#include "stddef.h"
#define SIZE 100
#define SIZE_R 100
#define FILE_NAME "/run/media/mmcblk1p1/b.txt"
#define C_ALOGE_PRINTF printf
int L_end=0;
int Current_cursor = 0;
uint8_t book_name[20];
uint8_t book_num[10];
uint8_t book_addr[20];
uint8_t book_msg[100];
int f_write(uint8_t* buff, FILE * file )
{
if( EOF == fputs( buff, file ))
{
C_ALOGE_PRINTF("can not fput one line.\n");
C_ALOGE_PRINTF("fputs wrong \n");
return 0;
}
else
{
if(buff[0]!='\n')
C_ALOGE_PRINTF("line: [%s]\n",buff);
return 1;
}
C_ALOGE_PRINTF("%s error\n",__func__);
return -1;
}
int f_read(uint8_t* buff, int len, FILE * file )
{
if( NULL == fgets( buff, SIZE, file ))
{
C_ALOGE_PRINTF("can not get one line.\n");
C_ALOGE_PRINTF(" fgets wrong ");
return 0;
}
else
{
C_ALOGE_PRINTF("line: [%s]\n",buff);
return 1;
}
C_ALOGE_PRINTF("%s error\n",__func__);
return -1;
}
int l_read(uint8_t* buff, FILE * file)
{
fscanf(file,"%[^\n]", buff);
C_ALOGE_PRINTF("%s: [%s]\n\n",__func__,buff);
return 0;
}
int f_endline(FILE * file )
{
L_end=fseek(file, 0, SEEK_END);
if(L_end == 0)
{
C_ALOGE_PRINTF("%s error\n",__func__);
return -1;
}
else
C_ALOGE_PRINTF("file size is %d",L_end);
return 0;
}
int bookmsg(uint8_t *name, uint8_t *num, uint8_t *addr)
{
memset( book_msg, '\0', sizeof(book_msg));
strcpy(book_msg,name);
strcat(book_msg," , ");
strcat(book_msg,num);
strcat(book_msg," , ");
strcat(book_msg,addr);
C_ALOGE_PRINTF("name=[%s], num=[%s], addr=[%s], msg=[%s]\n",name,num,addr,book_msg);
return 0;
}
int save_bookmsg(FILE * file)
{
int size_msg=strlen(book_msg);
C_ALOGE_PRINTF("bookmsg size is [%d]\n",size_msg);
if( 0 != fseek( file , Current_cursor , SEEK_SET ))
{
perror(" fseek wrong : \n");
C_ALOGE_PRINTF(" fseek wrong \n");
}
f_write(book_msg, file);
f_write("\n", file);
C_ALOGE_PRINTF("\n");
return 0;
}
int main()
{
FILE *f_mem = NULL;
int i = 0;
static int line = 0;
uint8_t buff[SIZE] = {0};
f_mem = fopen( FILE_NAME , "rt+");
if(NULL == f_mem)
{
perror(" fopen wrong ");
C_ALOGE_PRINTF(" fopen wrong ");
}
else
C_ALOGE_PRINTF("Success to open file!\n\n");
memset(buff, '\0', SIZE);
bookmsg("书名","ISBN号","地址");
save_bookmsg(f_mem);
Current_cursor = ftell( f_mem );
C_ALOGE_PRINTF("current cursor=%d\n",Current_cursor);
if( 0 != fseek( f_mem , 0 , SEEK_SET ))
{
perror(" fseek wrong : \n");
C_ALOGE_PRINTF(" fseek wrong \n");
}
l_read(buff,f_mem);
bookmsg("这里是中国","67890","北5柜");
save_bookmsg(f_mem);
if((fclose(f_mem)) == 0)
{
C_ALOGE_PRINTF("Success to close file!\n");
}
else
C_ALOGE_PRINTF("!!! fclose wrong \n");
f_mem = NULL;
return 0;
}
这是里只是文件存储的代码,串口传输的放在csdn上了。
小结
sd卡读写功能正常,可以实现串口屏录入书籍信息存储到sd卡文件中这一功能。