From: simon <simon.guinot@laposte.net>
To: krzaq <krzakers@gmail.com>,
linux-C-programming <linux-c-programming@vger.kernel.org>
Subject: Re: open() and read() a file from sysfs
Date: Tue, 01 Mar 2005 02:17:54 +0100 [thread overview]
Message-ID: <4223C2C2.9070101@laposte.net> (raw)
In-Reply-To: <2ab8d39a050228061825dda7fe@mail.gmail.com>
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
next prev parent reply other threads:[~2005-03-01 1:17 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-02-28 14:18 open() and read() a file from sysfs krzaq
2005-03-01 1:17 ` simon [this message]
2005-03-01 8:58 ` krzaq
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=4223C2C2.9070101@laposte.net \
--to=simon.guinot@laposte.net \
--cc=krzakers@gmail.com \
--cc=linux-c-programming@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).