免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4890 | 回复: 6
打印 上一主题 下一主题

如何连续读U盘的物理扇区,而不是从缓存读??? 悬赏高手!!! [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-12-03 09:14 |只看该作者 |倒序浏览
2可用积分
如何关闭USB设备缓存?如何连续读写U盘同一数据块?高手快来?


我写了一个小程序,功能是在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;
}

[ 本帖最后由 Rick_Wang 于 2007-12-3 09:23 编辑 ]

最佳答案

查看完整内容

open file with flag O_DIRECT or set O_DIRECT flag for a file already opened by using the F_SETFL command of the fcntl( ).

论坛徽章:
0
2 [报告]
发表于 2007-12-03 09:14 |只看该作者
open file with flag O_DIRECT or set O_DIRECT flag for a file already opened by using the F_SETFL command of the fcntl( ).

论坛徽章:
0
3 [报告]
发表于 2007-12-03 15:52 |只看该作者

回复 #2 dragchan 的帖子

你好,我用你说的方法修改了我的程序,使用open("/dev/sdb", O_RDWR | O_DIRECT);
但是编译时出现错误,错误说O_DIRECT没有声明,找不到。
请问高手,要include什么头文件?不胜感激!

论坛徽章:
0
4 [报告]
发表于 2007-12-03 18:10 |只看该作者
原帖由 Rick_Wang 于 2007-12-3 15:52 发表
你好,我用你说的方法修改了我的程序,使用open("/dev/sdb", O_RDWR | O_DIRECT);
但是编译时出现错误,错误说O_DIRECT没有声明,找不到。
请问高手,要include什么头文件?不胜感激!


我编译时错误如下:
[root@localhost home]# gcc testnew8.c -o sd
testnew8.c: 在函数 ‘main’ 中:
testnew8.c:24: 错误:‘O_DIRECT’ 未声明 (在此函数内第一次使用)
testnew8.c:24: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其
testnew8.c:24: 错误:所在的函数内只报告一次。)


请问高手,这如何解决啊?

论坛徽章:
0
5 [报告]
发表于 2007-12-03 21:21 |只看该作者

回复 #4 Rick_Wang 的帖子

asm/fcntl.h  ?

论坛徽章:
0
6 [报告]
发表于 2007-12-04 10:02 |只看该作者
我已经解决了这个问题了,现在来告诉大家。
具体做法如下:
#define _GNU_SOURCE
#include <fcntl.h>
这样编译时就通过了。

论坛徽章:
0
7 [报告]
发表于 2007-12-04 10:04 |只看该作者
在此,很感谢dragchan,是你给了我解决问题的方向。:wink:
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP