How to read a 1-wire sensor with C on a Raspberry Pi

Kevin Cozens kevin-4dS5u2o1hCn3fQ9qLvQP4Q at public.gmane.org
Fri Jan 17 01:54:09 UTC 2014


On 14-01-16 07:55 PM, Jamon Camisso wrote:
> Second, I'm not sure how to extract the t=17625 value - it is always at
> position len-6 (\0 line termination needs to be accounted for). It is
> always the same length which is great. But:
>
> Question 2) Is there a better way than strncpy?

FYI, in your original (as pasted), you declared temp_raw as a char array 
with space for five characters but your strncpy was reading in up to 6 
characters. The temp_raw buffer should have been big enough to hold the 
maximum number of characters. strncpy could read up to 6 characters so 
temp_raw should be declared with 7 minimum (up to 6 from strncpy, plus space 
for a terminating NUL).

Avoiding strncpy:

   float raw_temp;
   sscanf(&line[len-6], "%f", &raw_temp);
   float temp_f = floorf((raw_temp/1000 + 0.05)*100)/100;

-- 
Cheers!

Kevin.

http://www.ve3syb.ca/           |"Nerds make the shiny things that distract
Owner of Elecraft K2 #2172      | the mouth-breathers, and that's why we're
                                 | powerful!"
#include <disclaimer/favourite> |             --Chris Hardwick
--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list