85 lines
1.8 KiB
C
Executable File
85 lines
1.8 KiB
C
Executable File
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
#include <sys/select.h>
|
|
#include <pthread.h>
|
|
|
|
#define N 2
|
|
|
|
float getTempByDev(char *devName);
|
|
void *run(void *args);
|
|
|
|
float results[N];
|
|
pthread_t tids[N];
|
|
char *names[] = {"/dev/ds18b20-223","/dev/ds18b20-224"};
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
int i = 0;
|
|
for (i = 0; i < N; i++)
|
|
{
|
|
pthread_create(&tids[i], NULL, run, (void*)i);
|
|
}
|
|
for (i = 0; i < N; i++)
|
|
{
|
|
pthread_join(tids[i], NULL);
|
|
}
|
|
for (i = 0; i < N; i++)
|
|
{
|
|
if (i > 0)
|
|
{
|
|
printf("#");
|
|
}
|
|
printf("%.3f", results[i]);
|
|
}
|
|
printf("\n");
|
|
return 0;
|
|
}
|
|
|
|
void *run(void *args)
|
|
{
|
|
int ret=(int)args;
|
|
float res = getTempByDev(names[ret]);
|
|
results[ret] = res;
|
|
}
|
|
|
|
float getTempByDev(char *devName)
|
|
{
|
|
int fd;
|
|
unsigned char result[2] = {0};
|
|
unsigned int hightBitValue = 0;
|
|
unsigned int lowBitValue = 0;
|
|
float p = 0.0625;
|
|
float value = 1024;
|
|
short count = 0;
|
|
|
|
fd = open(devName, 0);
|
|
if (fd >= 0)
|
|
{
|
|
do
|
|
{
|
|
count++;
|
|
int i = read(fd, &result, sizeof(&result));
|
|
if (i >= 0)
|
|
{
|
|
hightBitValue = result[1];
|
|
lowBitValue = result[0];
|
|
hightBitValue <<= 8;
|
|
hightBitValue = hightBitValue + lowBitValue;
|
|
if ((result[1] & 0xf8))
|
|
{
|
|
hightBitValue = ~hightBitValue + 1;
|
|
value = hightBitValue * p * -1;
|
|
}
|
|
else
|
|
{
|
|
value = hightBitValue * p;
|
|
}
|
|
}
|
|
}
|
|
while ((value < -55 || value > 125) && count < 5);
|
|
close(fd);
|
|
}
|
|
return value;
|
|
}
|