#include #include #include "/home/jeblack3/libusblinux300/ownet.h" void echoBlock (int portnum, unsigned char *buf, SMALLINT len, char op[7], int power); int main (int argc, char *argv[]) { char portname[9] = "DS2490-1"; unsigned char SN[8], SNbuf[9], convertT, scratchpad[10]; int portnum, i, n, byte, level; struct timespec delay; delay.tv_sec = 1; delay.tv_nsec = 0; // Get serial number from command-line argument if (argc < 2) { printf ("ERROR: Expected: therm-read serial-number\n"); return 0; } for (i = 0; i < 8; i++) { n = sscanf (argv[1] + 2 * i, "%02x", &byte); SN[7 - i] = (unsigned char) byte; if (n != 1) break; } if (i < 8) { printf ("ERROR: Invalid serial number\n"); return 0; } // Acquire adapter portnum = owAcquireEx (portname); if (portnum < 0) { printf ("ERROR: Adapter not found\n"); return 0; } while (1) { // Reset devices if ( !owTouchReset(portnum) ) { printf ("ERROR: No one-wire devices present on network\n"); return 0; } // Send serial number SNbuf[0] = 0x55; for (i = 0; i < 8; i++) { SNbuf[i + 1] = SN[i]; } echoBlock (portnum, SNbuf, 9, "MATCH1", FALSE); // Send convert temperature command, begin power delivery convertT = 0x44; echoBlock (portnum, &convertT, 1, "CONV T", TRUE); // Sleep 1 second printf ("Sleep \n"); if ( nanosleep(&delay, NULL) != 0) { printf ("ERROR: Sleep interrupted\n"); } printf ("\n"); // End power delivery level = owLevel(portnum,MODE_NORMAL); if ( level != MODE_NORMAL) { printf ("ERROR: Could not switch to normal mode\n\n"); } // Reset devices if ( !owTouchReset(portnum) ) { printf ("ERROR: No one-wire devices present on network\n"); return 0; } // Send serial number SNbuf[0] = 0x55; for (i = 0; i < 8; i++) { SNbuf[i + 1] = SN[i]; } echoBlock (portnum, SNbuf, 9, "MATCH2", FALSE); // Read scratchpad scratchpad[0] = 0xBE; for (i = 1; i < 10; i++) { scratchpad[i] = 0xFF; } echoBlock (portnum, scratchpad, 10, "READ ", FALSE); } } void echoBlock (int portnum, unsigned char *buf, SMALLINT len, char op[7], int power) { int i, level; time_t time_seconds; struct tm *time_calendar; char *time_string; time_seconds = time (NULL); printf ("%s Secs1: %10lu\n", op, time_seconds); time_calendar = localtime (&time_seconds); time_string = asctime (time_calendar); printf ("%s Time1: %s", op, time_string); printf ("%s Sent: ", op); for (i = 0; i < len; i++) { printf ("%02X ", buf[i]); } printf ("\n"); owBlock (portnum, FALSE, buf, len); if (power) { // Begin power delivery level = owLevel(portnum,MODE_STRONG5); if ( level != MODE_STRONG5) { printf ("ERROR: Could not switch to strong pullup mode\n\n"); } } printf ("%s Recvd: ", op); for (i = 0; i < len; i++) { printf ("%02X ", buf[i]); } printf ("\n"); time_seconds = time (NULL); printf ("%s Secs2: %10lu\n", op, time_seconds); time_calendar = localtime (&time_seconds); time_string = asctime (time_calendar); printf ("%s Time2: %s", op, time_string); printf ("\n"); return; }