1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- #include <fcntl.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/select.h>
- float getTempByDev(char *devName);
- int main(int argc, char *argv[])
- {
- if (argc == 2)
- {
- char * ch = argv[1];
- // int gpio = atoi(ch);
- char * name = "/dev/ds18b20-";
- char devName[16];
- strcpy(devName, name);
- strcat(devName, ch);
- float t223 = getTempByDev(devName);
- printf("%.4f", t223);
- }
- else
- {
- printf("-1");
- }
- return 0;
- }
- 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.0f;
- fd = open(devName, 0);
- if (fd >= 0)
- {
- int i = read(fd, &result, sizeof(&result));
- if (i >= 0)
- {
- // printf("%xH-%xH\n", result[1], result[0]);
- hightBitValue = result[1];
- lowBitValue = result[0];
- hightBitValue <<= 8;
- hightBitValue = hightBitValue + lowBitValue;
- if ((result[1] & 0xf8))
- {
- // printf("aaa\n");
- hightBitValue = ~hightBitValue + 1;
- value = hightBitValue * p * -1;
- }
- else
- {
- value = hightBitValue * p;
- }
- }
- close(fd);
- }
- return value;
- }
|