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

Jamon Camisso jamon.camisso-H217xnMUJC0sA/PxXw9srA at public.gmane.org
Fri Jan 17 02:37:27 UTC 2014


On 16/01/14 08:54 PM, Kevin Cozens wrote:
> 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;

Ah interesting, do I understand this correctly as: sscanf returns the
indicated 6 bytes of the contents of the string 'line' into the memory
address of the float raw_temp?

Thanks, I like it better, if only because it's another method I'd never
have figured out on my own and I need to get better with pointers ;)

Cheers, Jamon
--
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