linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* open() and read() a file from sysfs
@ 2005-02-28 14:18 krzaq
  2005-03-01  1:17 ` simon
  0 siblings, 1 reply; 3+ messages in thread
From: krzaq @ 2005-02-28 14:18 UTC (permalink / raw)
  To: linux-c-programming

Hi all!

I have a basic question with file opening.
I would like to make use of the data accessed from i2c sensors through sysfs.

Here's the code:

fd = open(/sys/bus/i2c/.../temp2_input,O_RDONLY);

while(1) {
  char[64] buf;
  read(fd,buf,siezof(buf));
  ...
  do_something
  ...
  sleep(5);
}

The thing is I always get the same reading in each read() :(.
When I do:
# cat /sys/bus/i2c.../temp2_input
I see that the temperature is changing, but my code still displays the
same reading.

So what's the deal? Why is it cached?
Am I supposed to open() and close() the file inside 'while' loop??
What's the proper way to do it?

-- 
Regards
Karol Krzak

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: open() and read() a file from sysfs
  2005-02-28 14:18 open() and read() a file from sysfs krzaq
@ 2005-03-01  1:17 ` simon
  2005-03-01  8:58   ` krzaq
  0 siblings, 1 reply; 3+ messages in thread
From: simon @ 2005-03-01  1:17 UTC (permalink / raw)
  To: krzaq, linux-C-programming

hello

krzaq wrote:
> Hi all!
> 
> I have a basic question with file opening.
> I would like to make use of the data accessed from i2c sensors through sysfs.
> 
> Here's the code:
> 
> fd = open(/sys/bus/i2c/.../temp2_input,O_RDONLY);
> 
> while(1) {
>   char[64] buf;
>   read(fd,buf,siezof(buf));
>   ...
>   do_something
>   ...
>   sleep(5);
> }
> 
> The thing is I always get the same reading in each read() :(.
> When I do:
> # cat /sys/bus/i2c.../temp2_input
> I see that the temperature is changing, but my code still displays the
> same reading.
ok... i suggest you to read the return value of read...
in your code, you can't see if an error append...
reinit your buffer with memset can be a good idea...
in your programm, we can imagine a first correct read, a second who 
return an error... you don't detect it... and you read an unchanged 
buffer...
ok that's not your only problem...

try something like that :

char buf[SIZE];
int fd;

if ((fd = open(/sys/bus/i2c/.../temp2_input,O_RDONLY)) == -1)
{
	perror ("open ()");
	return (-1);
}
while (1)
{
	memset (buf, 0, SIZE);
	if (read (fd, buf, SIZE) != SIZE)
	{
		fprintf (stderr, "bad read size... bla bla\n");
		return (-1);
	}
	...
	do what you want
	...
	lseek (buf, 0, SEEK_SET);
}

lseek is possible as files in /sys are specials... open, read and write 
methods are directly handle by drivers...
don't expect to do that with classic files

simon

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: open() and read() a file from sysfs
  2005-03-01  1:17 ` simon
@ 2005-03-01  8:58   ` krzaq
  0 siblings, 0 replies; 3+ messages in thread
From: krzaq @ 2005-03-01  8:58 UTC (permalink / raw)
  To: linux-c-programming

On Tue, 01 Mar 2005 02:17:54 +0100, simon <simon.guinot@laposte.net> wrote:
> hello
> 
> krzaq wrote:
> > Hi all!
> >
> > I have a basic question with file opening.
> > I would like to make use of the data accessed from i2c sensors through sysfs.
> >
> > Here's the code:
> >
> > fd = open(/sys/bus/i2c/.../temp2_input,O_RDONLY);
> >
> > while(1) {
> >   char[64] buf;
> >   read(fd,buf,siezof(buf));
> >   ...
> >   do_something
> >   ...
> >   sleep(5);
> > }
> >
> > The thing is I always get the same reading in each read() :(.
> > When I do:
> > # cat /sys/bus/i2c.../temp2_input
> > I see that the temperature is changing, but my code still displays the
> > same reading.
> ok... i suggest you to read the return value of read...
> in your code, you can't see if an error append...
> reinit your buffer with memset can be a good idea...
> in your programm, we can imagine a first correct read, a second who
> return an error... you don't detect it... and you read an unchanged
> buffer...
> ok that's not your only problem...
This was kinda  'pseudo code'. The real program checks all errors
including what read()
returns and OFCOURSE it has lseek() (sorry about the confusion).
Anyway the code runs without errors, but still it doesn't get updated values.

Now for the fun part. 
There are two softwares displaying system information (including i2c reads):
- torsmo
- gkrellm

I looked at the code of both. Torsmo open()'s i2c devices and then
sequentially read()'s
them lseek()'ing to the begining (exaclty what I was trying to do). 
With linux - 2.6.10 it doesn't update the values!
It's been reported as a bug on sf homepage of this project.

Gkrellm, which is a much more advanced project, displays updated
readings. After poking around with source I saw, that they are
re-open()ing each file (sensor) they want to read() :).

Since I do not have 2.6.9 anymore, I will have to download it and
check if read()'ing and
lseek()'ing on one opened file is working there.

-- 
Regards
Karol Krzak

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-03-01  8:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-02-28 14:18 open() and read() a file from sysfs krzaq
2005-03-01  1:17 ` simon
2005-03-01  8:58   ` krzaq

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).