免费注册 查看新帖 |

Chinaunix

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

如何关闭USB设备缓存?如何连续读写U盘同一数据块?高手快来? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-11-29 18:09 |只看该作者 |倒序浏览
我写了一个小程序,功能是在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;
}

论坛徽章:
0
2 [报告]
发表于 2007-11-30 12:07 |只看该作者

回复 #1 Rick_Wang 的帖子

如何连续读U盘同一地址下的数据块呢?哪位高手能帮帮忙?先感谢你了。

论坛徽章:
0
3 [报告]
发表于 2007-12-05 17:29 |只看该作者
这个问题我已经解决了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP