LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-14-2008, 11:18 PM   #1
yuanlaidewo
LQ Newbie
 
Registered: Jul 2008
Posts: 1

Rep: Reputation: 0
Read/Write data to SD card physical address by USB Reader in Fedora 7


Source code :
#define _GNU_SOURCE
#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,num,loop,tt,i;
unsigned char writeBuf[512] = {0};
int offset = 0x50000;

//打开
fd = open("/dev/sda1",O_RDWR | O_DIRECT);
//fd = open("/media/SD CARD/system.txt",O_RDWR);

if(fd==-1)
{
printf("can't open the device by device mode !\n");
return -1;
}

//设置
//fcntl(fd,F_SETFL,O_DIRECT);


//写入数据内容
memcpy(writeBuf,"\x55\xAA\xAA\x55",4);

//写
tt = lseek(fd,offset,SEEK_SET);
printf("\n Write lseek %#x\n",tt);
num = write(fd,writeBuf,512);

if(num==512)
{
printf("\n write success!\n");
num = 0;
}
if(num==-1)
{
printf("write failed!\n");
close(fd);
return -1;
}

//同步,等待
sync();
sleep(1);
memset(writeBuf,0x00,512);

//读
tt = lseek(fd,offset,SEEK_SET);
printf("\n Read lseek %#x \n",tt);
num = read(fd,writeBuf,512);

if(num==512)
{
printf("\n read success!\n");
}
else
{
printf("read failed!\n");
close(fd);
return -1;
}

//打印响应数据
for(i = 0; i < 512; i++)
{
if((i % 32) == 0)
printf("\r\n");
printf("%3x ", writeBuf);
if(i == 512)
printf("\r\n\n");
}

close(fd);

return 0;
}

the question is :
if open the device by O_RDWR | O_DIRECT mode ,it can't write data to device,but if open device by O_RDWR mode ,write/read function excute sucess,but it same like the data is not real written to the device .it same like write to the cache ,and read from the cache.
how can i write data directly to the device and read data from device directly ,not cache.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
USB HardDrive mounts read-only. Is there anyway to make it read-write? Fritz_Katz Linux - Hardware 7 09-13-2007 07:50 PM
is there any shell command to read and write data from parallel and serial port? baosheng Linux - Hardware 2 01-13-2007 09:35 PM
How can I read from my multimedia card reader ceeleelewis Linux - Laptop and Netbook 5 08-29-2006 07:31 PM
How i can read/write data from USB PIC16? njel Programming 0 07-14-2004 11:00 AM
USB CF Card Reader/Write not a valid block device cromwell Linux - Hardware 10 01-06-2004 11:11 PM


All times are GMT -5. The time now is 04:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration