LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 08-15-2019, 07:41 AM   #1
igok99
LQ Newbie
 
Registered: Jan 2019
Posts: 5

Rep: Reputation: Disabled
reading from hwmon temperature sensor 103


hi,
i'm trying to write a driver which reads from "/sys/class/hwmon/hwmon0/temp1_input" on user space.
The first read is successful, but then from the second read I get only zero values.
Reading from the FD works only when closing and reopening the FD again.

is there a way/example how to read from hwmon without reopening & closing each time the FD?

thanks.

Last edited by igok99; 08-15-2019 at 07:43 AM.
 
Old 08-15-2019, 09:16 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
is there a way to post your code?
 
Old 08-15-2019, 09:33 AM   #3
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
Code:
while true
do
 cat  /sys/class/hwmon/hwmon0/temp1_input
sleep 1
done
??

gkrellm has a means to get and keep track of temps, open source, maybe take a peak at how they do it?

Last edited by BW-userx; 08-15-2019 at 10:01 AM.
 
Old 08-17-2019, 02:03 PM   #4
igok99
LQ Newbie
 
Registered: Jan 2019
Posts: 5

Original Poster
Rep: Reputation: Disabled
this is my code:
fdHwmon0 = open("/sys/class/hwmon/hwmon0/temp1_input", O_RDONLY);
read(fdHwmon0, buf, 6);
close(fdHwmon0);

i'm trying to avoid opening and closing on every read.

Last edited by igok99; 08-17-2019 at 02:06 PM.
 
Old 08-17-2019, 02:12 PM   #5
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342

Rep: Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242Reputation: 2242
again, I suggest looking at other apps that tap into the temps of systems and see how they are doing it so you can get a better understanding of what needs to be done, and how.
see post #3

do a little research
 
Old 08-17-2019, 02:17 PM   #6
igok99
LQ Newbie
 
Registered: Jan 2019
Posts: 5

Original Poster
Rep: Reputation: Disabled
ok thanks!
 
Old 08-18-2019, 02:43 AM   #7
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
Quote:
Originally Posted by igok99 View Post
i'm trying to avoid opening and closing on every read.
Why? I think this is not a real file, but something generated during open (dynamically). So there is no other way. But probably I missed something.
 
Old 08-18-2019, 06:06 AM   #8
igok99
LQ Newbie
 
Registered: Jan 2019
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
Why? I think this is not a real file, but something generated during open (dynamically). So there is no other way. But probably I missed something.
So there is no other way- no other way but to open and close the fd on every read? why?
 
Old 08-18-2019, 07:56 AM   #9
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
From 'man 5 sysfs'
Quote:
The sysfs filesystem is a pseudo-filesystem which provides an interface to kernel data structures. (More precisely, the files and directories in sysfs provide a view of the kobject structures defined internally within the kernel.) The files under sysfs provide information about devices, kernel modules, filesystems, and other kernel components.
and from the kernel source file /Documentation/filesystems/sysfs.txt mentioned in the SEE ALSO section of the man page
Quote:
Attributes can be exported for kobjects in the form of regular files in the filesystem. Sysfs forwards file I/O operations to methods defined for the attributes, providing a means to read and write kernel attributes.
In line with the *nix philosophy that everything is a file, the information is returned on reading the file. The file needs to be read again to get updated data.
 
Old 08-18-2019, 08:06 AM   #10
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,912

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quite likely to read again you would have to seek to the beginning of the file.

The lm_sensors does open and close the file, but it appears to only read it once and then exits.
 
Old 08-18-2019, 10:44 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,840

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
yes, you can try to seek, but if you want to understand it you need to read the documentation starting for example here: https://www.mjmwired.net/kernel/Docu.../sysfs-api.txt (or the source code)
 
Old 08-18-2019, 11:17 AM   #12
rtmistler
Moderator
 
Registered: Mar 2011
Location: USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu, Debian
Posts: 9,882
Blog Entries: 13

Rep: Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930Reputation: 4930
Using pan64's reference, I'd instead not try to read a /sys file and instead try to register a callback or obtain the actual structure from the driver, which contains the relevant information you need.
 
Old 08-18-2019, 07:18 PM   #13
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,371

Rep: Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749Reputation: 2749
From the kernel source file /Documentation/filesystems/sysfs.txt with emphasis added by me.
Quote:
sysfs allocates a buffer of size (PAGE_SIZE) and passes it to the
method. Sysfs will call the method exactly once for each read or
write. This forces the following behavior on the method
implementations:

- On read(2), the show() method should fill the entire buffer.
Recall that an attribute should only be exporting one value, or an
array of similar values, so this shouldn't be that expensive.

This allows userspace to do partial reads and forward seeks
arbitrarily over the entire file at will. If userspace seeks back to
zero or does a pread(2) with an offset of '0' the show() method will
be called again, rearmed, to fill the buffer.

 
1 members found this post helpful.
Old 08-26-2019, 03:38 PM   #14
igok99
LQ Newbie
 
Registered: Jan 2019
Posts: 5

Original Poster
Rep: Reputation: Disabled
pread did the job..

thanks for help!
 
  


Reply



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
[SOLVED] hwmon sysfs usb temperature driver flux242 Linux - Kernel 2 02-20-2012 02:08 AM
[SOLVED] TOSHIBA Hard Drive Impact Sensor (3D sensor) and Linux josephj Linux - Laptop and Netbook 4 11-06-2010 06:39 PM
the third temperature sensor for lm_sensors aus9 Linux - Hardware 0 12-29-2004 08:13 PM
My fan and temperature sensor dont get detected!! _UnPrEdictAbLe_ Linux - Hardware 25 08-19-2004 06:21 AM
Temperature Sensor Reading PhuckFonix Linux - Hardware 2 08-14-2004 02:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:11 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
Open Source Consulting | Domain Registration