- 论坛徽章:
- 0
|
我写了一个小程序,功能是在Linux(fedora 7)操作系统下,通过USB读卡器,循环读、写SD卡上的512字
节的数据块。但经过试验发现,只能读、写前几次,以后就不会从SD卡上读、写。好像是从系统的缓存中
读、写。对写操作,我加了sync()函数,现在已经实现了写同步,每次都能写到SD卡。但对读操作,还不
能实现每次都从SD卡读数据,小弟不知道该如何实现?用底层函数ioctl()怎么样?或者可以修改USB解决
这个问题吗?各位大侠,有解决这个问题的方法吗,给小弟指点一下吧,不胜感激!!!
下面是我的测试程序代码:
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
int fd;
int num, loop, tt;
unsigned char writeBuf[512];
int offset = 0x41200;
fd = open("/dev/sdb", O_RDWR);
if(fd == -1)
{
printf("can't open the device by device mode!\n");
return -1;
}
else
{
printf("come to write usb-sd\n");
//write usb-sd
for(loop = 20000; loop > 0; --loop){
memset(writeBuf, 0x22, 512);
tt = lseek(fd, offset, SEEK_SET);
printf("lseek %#x\n", tt);
num = write(fd, writeBuf, sizeof(writeBuf));
if(num == 512)
{
printf("write success!\n");
printf("write %d\n", num);
num = 0;
}else if(num != -1){
printf("lost buffer!\n");
close(fd);
return -1;
}else if(num == -1){
printf("write failed!\n");
close(fd);
return -1;
}
sync();
//sleep(1);
printf("come to read usb-sd\n");
//read usb-sd
memset(writeBuf, 0x88, 512);
tt = lseek(fd, offset, SEEK_SET);
printf("lseek %#x\n", tt);
num = read(fd, writeBuf, sizeof(writeBuf));
if(num == 512){
printf("read %d\n", num);
}else if(num == 0){
printf("read failure!\n");
return -1;
}
//checkout
for(i = 0; i < num; i++){
if((i % 16) == 0) printf("\r\n");
printf("%#x ", writeBuf);
if(i == 511) printf("\r\n");
}
//sleep(1);
}
}
close(fd);
return 0;
} |
|