예제 #1
0
// -------------------------------------------------------------------------
// SUBROUTINE - ReadWet
//
// 'temp'  - The temperature from the DS1820 in F
// 'dir'   - The wind direction from 0-15
// 'revol' - The wind speed in revolutions per second
//
// Returns:  TRUE, if the reads were successfull and FALSE if they were not.
//
int ReadWet(int portnum, WeatherStruct *wet, float *temp, int *dir, double *revol)
{
   int ret = TRUE;
   unsigned long start_count=0, end_count=0, start_time, end_time;
   float current_temp;

   // read the current counter
   // use this reference to calculate wind speed later
   if (!ReadCounter(portnum, &wet->ds2423[0], 15, &start_count))
   {
      printf("\nError reading counter, verify device present:%d\n",
              (int)owVerify(portnum, FALSE));
      ret = FALSE;
   }

   // get a time stamp (mS)
   start_time = msGettick();

   // read the temperature and print in F
   if (ReadTemperature(portnum, &wet->ds1820[0],&current_temp))
      *temp = current_temp * 9 / 5 + 32;
   else
   {
      printf("\nError reading temperature, verify device present:%d\n",
              (int)owVerify(portnum, FALSE));
      ret = FALSE;
   }

   // read the wind direction
   *dir = TrueDir(portnum, wet);

   // read the counter again
   if (!ReadCounter(portnum, &wet->ds2423[0], 15, &end_count))
   {
      printf("Error reading counter, verify device present:%d\n",
              (int)owVerify(portnum, FALSE));
      ret = FALSE;
   }

   // get a time stamp (mS)
   end_time = msGettick();

   // calculate the wind speed based on the revolutions per second
   *revol = (((end_count - start_count) * 1000.0) /
              (end_time - start_time)) / 2.0;

   return ret;
}
예제 #2
0
//----------------------------------------------------------------------
//  Main Test for the DS2450 - 1-Wire Quad A/D Converter
//
int main(int argc, char **argv)
{
   char msg[45];
   int NumDevices = 0;
   int i = 0;
   int start_address = 0x8;
   int end_address = 0x11;
   float prslt[4];
   uchar ctrl[16];
   int try_overdrive=0;
   int portnum=0;

   //------------------------------------------------------
   // Introduction header
   printf("\n/---------------------------------------------\n");
   printf("  Channels A to D Application - V2.00\n"
          "  The following is a test to excersize a\n"
          "  DS2450 - 1-Wire Quad A/D Converter \n\n");

   printf("  Press any CTRL-C to stop this program.\n\n");
   printf("  Output   [Serial Number(s) ... Channel 'A' Value ... Channel 'B' Value ... \n"
                         "                             ... Channel 'C' Value ... Channel 'D' Value] \n\n");

   // check for required port name
   if (argc != 2)
   {
      printf("1-Wire Net name required on command line!\n"
             " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" "
             "(Linux DS2480),\"{1,5}\" (Win32 TMEX)\n");
      exit(1);
   }

   // attempt to acquire the 1-Wire Net
   if ((portnum = owAcquireEx(argv[1])) < 0)
   {
      OWERROR_DUMP(stdout);
      exit(1);
   }

   // success
   printf("Port opened: %s\n",argv[1]);

   // Find the device(s)
   NumDevices = FindDevices(portnum, &FamilySN[0], 0x20, MAXDEVICES);
   if (NumDevices>0)
   {
      printf("\n");
      printf("Device(s) Found: \n");

      for (i = 0; i < NumDevices; i++)
      {

         PrintSerialNum(FamilySN[i]);
         printf("\n");

         if (SetupAtoDControl(portnum, FamilySN[i], &ctrl[0], &msg[0]))
         {
            printf("A/D settings found\n %s\n", msg);
         }
         else
            printf("\n\n\n ERROR, device set up unsuccessful!\n");


         if (WriteAtoD(portnum, try_overdrive, FamilySN[i], &ctrl[0], start_address, end_address))
         {
            printf("\nA/D settings written");

         }
         else
            printf("\n\n\n ERROR, device not found!\n");
      }
   }

   // (stops on CTRL-C)
   do
   {
      // read the current channels
      for (i = 0; i < NumDevices; i++)
      {
         printf("\n\n");
         PrintSerialNum(FamilySN[i]);

         if (!DoAtoDConversion(portnum, try_overdrive, FamilySN[i]))
         {
            printf("\nError doing conversion, verify device present: %d\n",
            (int)owVerify(portnum,FALSE));
         }
         if (ReadAtoDResults(portnum, try_overdrive, FamilySN[i], &prslt[0], &ctrl[0]))
         {
            int  c = 0;
            for (c = 0; c < 4; c++)
            {
               printf("  %1.3f ", prslt[c]);
            }
         }
         else
         {
            printf("\nError reading channel, verify device present: %d\n",
            (int)owVerify(portnum,FALSE));
         }

      }
   }
   while (!key_abort());

   // release the 1-Wire Net
   owRelease(portnum);
   printf("Closing port %s.\n", argv[1]);
   exit(0);

   return 0;
}
예제 #3
0
파일: temp.c 프로젝트: rmanders/owpy
//----------------------------------------------------------------------
//  Main Test for DS1920/DS1820 temperature measurement
//
int main(int argc, char **argv)
{
   float current_temp;
   int i = 0;
   int NumDevices=0;
   int portnum = 0;

   //----------------------------------------
   // Introduction header
   printf("\n/---------------------------------------------\n");
//   printf("  Temperature application DS1920/DS1820 - Version 1.00 \n"
//          "  The following is a test to excersize a DS1920/DS1820.\n"
//          "  Temperature Find and Read from a: \n"
//          "  DS1920/DS1820 (at least 1)\n\n");

   printf("  Press any CTRL-C to stop this program.\n\n");
   printf("  Output [Serial Number(s) ........ Temp1(F)] \n\n");

   // check for required port name
   if (argc != 2)
   {
      printf("1-Wire Net name required on command line!\n"
             " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" "
             "(Linux DS2480),\"{1,5}\" (Win32 TMEX)\n");
      exit(1);
   }


   // attempt to acquire the 1-Wire Net
   if((portnum = owAcquireEx(argv[1])) < 0)
   {
      OWERROR_DUMP(stdout);
      exit(1);
   }

   // success
   printf("Port opened: %s\n",argv[1]);

   // Find the device(s)
   NumDevices = FindDevices(portnum, &FamilySN[0], 0x10, MAXDEVICES);
   if (NumDevices>0)
   {
      printf("\n");
      printf("Device(s) Found: \n");
      for (i = 0; i < NumDevices; i++)
      {
         PrintSerialNum(FamilySN[i]);
         printf("\n");
      }
      printf("\n\n");

      // (stops on CTRL-C)
      do
      {
         // read the temperature and print serial number and temperature
         for (i = 0; i < NumDevices; i++)
         {

            if (ReadTemperature(portnum, FamilySN[i],&current_temp))
            {
               PrintSerialNum(FamilySN[i]);
//               printf("     %5.1f \n", current_temp * 9 / 5 + 32);
                 printf("     %5.1f \n", current_temp );
              // converting temperature from Celsius to Fahrenheit
            }
            else
               printf("     Error reading temperature, verify device present:%d\n",
                       (int)owVerify(portnum, FALSE));
         }
         printf("\n");
      }
      while (!key_abort());
   }
   else
      printf("\n\n\nERROR, device DS1920/DS1820 not found!\n");

   // release the 1-Wire Net
   owRelease(portnum);
   printf("Closing port %s.\n", argv[1]);
   exit(0);

   return 0;
}
예제 #4
0
파일: shaib.c 프로젝트: AriZuu/OneWire
//-------------------------------------------------------------------------
// Select the current device and attempt overdrive if possible.  Usable
// for both DS1963S and DS1961S.
//
// 'portnum'     - number 0 to MAX_PORTNUM-1.  This number is provided to
//                 indicate the symbolic port number.
//
// Return: TRUE - device selected
//         FALSE - device not select
//
SMALLINT SelectSHA(int portnum)
{
   int rt,cnt=0;

#ifdef __MC68K__
   // Used in when overdrive isn't...Used owVerify for overdrive
   rt = owAccess(portnum);
#else
   //\\//\\//\\//\\//\\//\\//\\//\\//\\//
   #ifdef DEBUG_DUMP
      uchar ROM[8];
      int i, mcnt;
      char msg[255];

      owSerialNum(portnum,ROM, TRUE);
      mcnt = sprintf(msg,"\n  Device select ");
      for (i = 0; i < 8; i++)
         mcnt += sprintf(msg + mcnt, "%02X",ROM[i]);
      mcnt += sprintf(msg + mcnt,"\n");
      printf("%s",msg);
   #endif
   //\\//\\//\\//\\//\\//\\//\\//\\//\\//

   // loop to access the device and optionally do overdrive
   do
   {
      rt = owVerify(portnum,FALSE);

      // check not present
      if (rt != 1)
      {
         // if in overdrive, drop back
         if (in_overdrive[portnum&0x0FF])
         {
            // set to normal speed
            if(MODE_NORMAL == owSpeed(portnum,MODE_NORMAL))
            	in_overdrive[portnum&0x0FF] = FALSE;
         }
      }
      // present but not in overdrive
      else if (!in_overdrive[portnum&0x0FF])
      {
         // put all devices in overdrive
         if (owTouchReset(portnum))
         {
            if (owWriteByte(portnum,0x3C))
            {
               // set to overdrive speed
               if(MODE_OVERDRIVE == owSpeed(portnum,MODE_OVERDRIVE))
               		in_overdrive[portnum&0x0FF] = TRUE;
            }
         }

         rt = 0;
      }
      else
         break;
   }
   while ((rt != 1) && (cnt++ < 3));
#endif

   return rt;
}
예제 #5
0
//----------------------------------------------------------------------
//  Main Test
//
int main() //short argc, char **argv)
{
   int PortNum=1,rslt,i,j,testcnt=0,length;
   uchar TempSerialNum[8];
   uchar tran_buffer[2000], filename[10];
   char return_msg[128];
   int portnum=0;

   // check for required port name
   if (argc != 2)
   {
      printf("1-Wire Net name required on command line!\n"
             " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" "
             "(Linux DS2480),\"1\" (Win32 TMEX)\n");
      exit(1);
   }

   // attempt to acquire the 1-Wire Net
   if (!owAcquire(portnum, argv[1], return_msg))
   {  
      printf("%s",return_msg);
      exit(1);
   }

   // success
   printf("%s",return_msg);

   //----------------------------------------
   // Introduction
   printf("\n/---------------------------------------------\n");
    printf("  The following is a test excersize of the\n"
          "  1-Wire Net public domain library Version 2.00.\n\n"
          "  This test was run using with 2 DS1920's (DS1820),\n"
          "  1 DS1971 (DS2430), and 1 DS1996.\n\n");

   //----------------------------------------
   // First the devices on the 1-Wire Net
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Searching for devices on 1-Wire Net\n",testcnt++);

   // find the first device (all devices not just alarming)
   rslt = owFirst(portnum,TRUE, FALSE);
   while (rslt)
   {
      // print the Serial Number of the device just found
      PrintSerialNum(portnum);

      // find the next device
      rslt = owNext(portnum,TRUE, FALSE);
   }

   //----------------------------------------
   // now search for the part with a 0x0C family code (DS1996)
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Set to find first device with 0x0C family code\n",testcnt++);

   owFamilySearchSetup(portnum,0x0C);

   // find the first 0x0c device
   TempSerialNum[0]=0;
   while (TempSerialNum[0]!=0x0c && owNext(portnum,TRUE,FALSE)) {
     owSerialNum(portnum,TempSerialNum,TRUE);
   }
   printf("search result %d\n",TempSerialNum[0]==0x0c);

   // print the Serial Number of the device just found
   PrintSerialNum(portnum);
   
   //----------------------------------------
   // Access a device and read ram
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Access the current device and read ram\n",testcnt++);

   printf("owAccess %d\n",owAccess(portnum));

   printf("Read Ram 0xF0: %02X\n",owTouchByte(portnum,0xF0));
   printf("Address0 0x00: %02X\n",owTouchByte(portnum,0x00));
   printf("Address1 0x00: %02X\n",owTouchByte(portnum,0x00));

   printf("Page 0: ");
   for (i = 0; i < 32; i++)
      printf("%02X ",owTouchByte(portnum,0xFF));
   printf("\n");

   //----------------------------------------
   // Read ram with owBlock
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Read ram with owBlock\n",testcnt++);
   for (i = 0; i < 32; i++)
      tran_buffer[i] = 0xFF;

   printf("owBlock %d\n",owBlock(portnum,FALSE,tran_buffer,32));
   printf("Page 1: ");
   for (i = 0; i < 32; i++)
      printf("%02X ",tran_buffer[i]);
   printf("\n");

   //----------------------------------------
   // Write a packet in each page of DS1996
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Place the DS1996 into overdrive\n",testcnt++);
   printf("owOverdriveAccess %d\n",owOverdriveAccess(portnum));

   //----------------------------------------
   // Write 4 packets with owWritePacketStd 
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Write 4 packets with owWritePacketStd\n",testcnt++);
     
   for (j = 0; j < 4; j++)
   {
      for (i = 0; i < 29; i++)
	tran_buffer[i] = (uchar)i + j;

      printf("Write page %d: %d\n",j,owWritePacketStd(portnum,j,tran_buffer,29,FALSE,FALSE));

      for (i = 0; i < 29; i++)
         tran_buffer[i] = 0;
   
      length = owReadPacketStd(portnum,TRUE,j,tran_buffer);

      printf("Read page %d: %d\n",j,length);

      for (i = 0; i < length; i++)
         printf("%02X",tran_buffer[i]);
      printf("\n");
   }

   //----------------------------------------
   // Write a file to DS1996
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Format and write a file (in overdrive)\n",testcnt++);
   sprintf(filename,"DEMO");
   // set the data to write
   for (i = 0; i < 2000; i++)
      tran_buffer[i] = i % 255;
   printf("Format and write file DEMO.000 %d\n",
	  owFormatWriteFile(portnum,filename,2000,tran_buffer));

   // clear the buffer
   for (i = 0; i < 2000; i++)
      tran_buffer[i] = 0x55;
   printf("Read file DEMO.000 %d\n",owReadFile(portnum,filename,tran_buffer));
   // print the data result
   for (i = 0; i < 2000; i++)
   {
      if ((i % 0x20) == 0)
         printf("\n%03X    ",i);
      printf("%02X",tran_buffer[i]);
   }
   printf("\n");
  
   //----------------------------------------
   // Turn off overdrive
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Turn off overdrive\n",testcnt++);
   printf("Set 1-Wire Net speed to normal %d\n",owSpeed(portnum,MODE_NORMAL));

   //----------------------------------------
   // Verify a device
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Verify the current device\n",testcnt++);

   printf("owVerify (normal) %d\n",owVerify(portnum,FALSE));
   printf("owVerify (alarm)  %d\n",owVerify(portnum,TRUE));

   //----------------------------------------
   // Skip the first family code found
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Skip the first family code found\n",testcnt++);
   
   // find the next device
   printf("search result of owFirst %d\n",owFirst(portnum,TRUE, FALSE));

   // print the Serial Number of the device just found
   PrintSerialNum(portnum);

   // skip the first family type found
   owSkipFamily(portnum);
   printf("owSkipFamily called\n");

   // find the next device
   printf("search result of owNext %d\n",owNext(portnum,TRUE, FALSE));
   
   // print the Serial Number of the device just found
   PrintSerialNum(portnum);

   //----------------------------------------
   // Find first family code (DS1920) and read temperature
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Find first family code (DS1920) and read temperature\n",testcnt++);

   // find the next device
   printf("search result of owFirst %d\n",owFirst(portnum,TRUE, FALSE));

   // print the Serial Number of the device just found
   PrintSerialNum(portnum);

   // send the convert temperature command
   printf("Convert temperature command %02X\n",owTouchByte(portnum,0x44));

   // set the 1-Wire Net to strong pull-up
   printf("Set power delivery %d\n",owLevel(portnum,MODE_STRONG5));

   // sleep for 1 second
   msDelay(1000);

   // turn off the 1-Wire Net strong pull-up
   printf("Disable power delivery %d\n",owLevel(portnum,MODE_NORMAL));

   // read the DS1920 temperature value
   printf("Access the DS1920 %d\n",owAccess(portnum));
   tran_buffer[0] = 0xBE;
   tran_buffer[1] = 0xFF;
   tran_buffer[2] = 0xFF;
   printf("Block to read temperature %d\n",owBlock(portnum,FALSE,tran_buffer,3));
   // interpret the result
   printf("result: DS1920 temperature read: %d C\n", (tran_buffer[1] |
           ((int)tran_buffer[2] << 8)) / 2);
  
   //----------------------------------------
   //  Verify the current device, could also be alarming
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Verify the current device, could also be alarming\n",testcnt++);

   printf("owVerify (normal) %d\n",owVerify(portnum,FALSE));
   printf("owVerify (alarm)  %d\n",owVerify(portnum,TRUE));

   //----------------------------------------
   // Test setting the Serial Number with owSerialNum
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Test setting the Serial Number with owSerialNum\n",testcnt++);

   // set the Serial Num to 0 to 7
   for (i = 0; i < 8; i++)
      TempSerialNum[i] = (uchar)i;
   owSerialNum(portnum,TempSerialNum,FALSE);

   // read back the Serial Number 
   PrintSerialNum(portnum);

   //----------------------------------------
   //  Verify the current device (should fail, no such device)
   printf("\n/---------------------------------------------\n");
   printf("TEST%d: Verify the current device (should fail, no such device)\n",testcnt++);

   printf("owVerify (normal) %d\n",owVerify(portnum,FALSE));
   printf("owVerify (alarm)  %d\n",owVerify(portnum,TRUE));

   // release the 1-Wire Net
   owRelease(portnum,return_msg);
   printf("%s",return_msg);
   exit(0);

   return 0;
}
예제 #6
0
파일: main.c 프로젝트: stefanrauch/scu_demo
void scu_init()
{
  	uchar FamilySN[MAXDEVICES][8];
	int current_temp;
	int c_frac;
  	int i = 0;
  	int j = 0;
	int cnt = 0;
  	int NumDevices = 0;
  	SMALLINT didRead = 0;
	uchar read_buffer[32];
	uchar write_buffer[32];

	owInit();
	uart_init();
	uart_write_string("SCU\n");
 
  	//use port number for 1-wire
  	uchar portnum = ONEWIRE_PORT;
     j = 0;
     // Find the device(s)
     NumDevices  = 0;
     NumDevices += FindDevices(portnum, &FamilySN[NumDevices], 0x42, MAXDEVICES-NumDevices);
     NumDevices += FindDevices(portnum, &FamilySN[NumDevices], 0x20, MAXDEVICES-NumDevices);
     NumDevices += FindDevices(portnum, &FamilySN[NumDevices], 0x43, MAXDEVICES-NumDevices);
     if (NumDevices)
     {
        mprintf("\r\n");

        // read the temperature and print serial number and temperature
        for (i = NumDevices; i; i--)
        {
           mprintf("(%d) ", j++);
           DisplaySerialNum(FamilySN[i-1]);
           if (FamilySN[i-1][0] == 0x43) {
//		if(!Write43(portnum, FamilySN[i-1], write_buffer))
//			mprintf("write failed!\n");
	  	owLevel(portnum, MODE_NORMAL); 
		if (ReadMem43(portnum, FamilySN[i-1], read_buffer))
		{
			for(cnt = 0; cnt < 32; cnt++)
			{
				mprintf("read_buffer[%x]: %x\n",cnt, read_buffer[cnt]);
			}
		}
		continue;
	   }

	   if (FamilySN[i-1][0] == 0x42) {
             didRead = ReadTemperature42(portnum, FamilySN[i-1],&current_temp,&c_frac);
	   }
           if (didRead)
           {
             	mprintf(" %d",current_temp);
		if (c_frac)
			mprintf(".5");
		else
			mprintf(".0");
		mprintf(" deegree celsius\r\n");
	   }
           else
           {
             mprintf("  Convert failed.  Device is");
             if(!owVerify(portnum, FALSE))
                mprintf(" not");
             mprintf(" present.\r\n");
#ifdef SOCKIT_OWM_ERR_ENABLE
             while(owHasErrors())
                 mprintf("  - Error %d\r\n", owGetErrorNum());
#endif
           }
 
        }
     }
     else
        mprintf("No temperature devices found!\r\n");
	
}
예제 #7
0
// -------------------------------------------------------------------------
// SUBROUTINE - FindWeather
//
// This routine finds which weather station is attached to the 1-wire
// network and returns the serial numbers for the weather station.  If
// it is the new weather station then it is not going to have an DS2401s.
//
// 'ds1820' - This will be the serial number for the DS1820 in the station
// 'ds2423' - This will be the serial number for the DS2423 in the station
// 'dsdir'  - This will either be the DS2450 or the DS2406 serial num.
// 'ds2401' - This will be the list of DS2401s if the old weather station.
//
// Returns: TRUE if a weather station is found and the listing of the
//          devices file is found or made.  FALSE if a weather station
//          is not found or the device file is not found or can not be made.
//
int FindSetupWeather(int portnum, WeatherStruct *wet)
{
   int ret = FALSE;
   FILE *fptr;
   char filename[] = "ini.txt";
   char TempSN[190];
   uchar SNs[90];
   int i,j;
   int num;
   char msg[45];
   uchar temp[MAXDIR][8];

   temp[0][0] = 0x00;
   wet->weather_a = FALSE;
   wet->weather_b = FALSE;
   wet->found_2401 = 0;
   wet->found_2423 = FALSE;
   wet->found_1820 = FALSE;
   wet->found_dir  = FALSE;
   wet->north = 0;

   if((fptr = fopen(filename, "r")) == NULL)
   {
      printf("error opening file but try to create file\n");

      // Create ini.txt if weather station has DS2450

      // find the DS1820 temperature
      num = FindDevices(portnum, &temp[0], TEMP_FAMILY, MAXTEMPS);

      // check if not at least 1 temperature device
      if(num == 0)
         wet->found_1820 = FALSE;
      else
      {
         for(i=0;i<8;i++)
            wet->ds1820[i] = temp[0][i]; 
         wet->found_1820 = TRUE;
      }

      // find the DS2450 switch
      num = FindDevices(portnum, &temp[0], ATOD_FAMILY, MAXATOD);

      // check if not at least 1 switch device
      if(num == 0)
         wet->found_dir = FALSE;
      else
      {
         for(i=0;i<8;i++)
            wet->dsdir[i] = temp[0][i];
         wet->found_dir = TRUE;
      }

      // find the DS2423 switch
      num = FindDevices(portnum, &temp[0], COUNT_FAMILY, MAXCOUNT);

      // check if not at least 1 count device
      if(num == 0)
         wet->found_2423 = FALSE;
      else
      {
         for(i=0;i<8;i++)
            wet->ds2423[i] = temp[0][i];
         wet->found_2423 = TRUE;
      }

      // Create the output file ini.txt
      if(wet->found_1820 && wet->found_dir && wet->found_2423)
      {
         if(SetupAtoDControl(portnum, &wet->dsdir[0], &wet->ctrl[0], &msg[0]))
            if(WriteAtoD(portnum, FALSE, &wet->dsdir[0], &wet->ctrl[0], 0x08, 0x11))
            {

               if((fptr = fopen(filename, "w")) == NULL)
                  printf("error tring to create file\n");
               else
               {
                  wet->weather_b = TRUE;
                  ret = TRUE;

                  // Printing the Serial number for DS2450
                  printf("Found DS2450\n");
                  for(i=7; i>=0; i--)
                     printf("%02X", wet->dsdir[i]);
                  printf("\n");
                  for(i=7; i>=0; i--)
                     fprintf(fptr, "%02X", wet->dsdir[i]);
                  fprintf(fptr, "\n");

                  // Printing the Serial number for DS1820
                  printf("Found DS1820\n");
                  for(i=7; i>=0; i--)
                     printf("%02X", wet->ds1820[i]);
                  printf("\n");
                  for(i=7; i>=0; i--)
                     fprintf(fptr, "%02X", wet->ds1820[i]);
                  fprintf(fptr, "\n");

                  // Printing the Serial number for DS2423
                  printf("Found DS2423\n");
                  for(i=7; i>=0; i--)
                     printf("%02X", wet->ds2423[i]);
                  printf("\n");
                  for(i=7; i>=0; i--)
                     fprintf(fptr, "%02X", wet->ds2423[i]);
                  fprintf(fptr, "\n");

                  // Printing the Default Wind Direction
                  printf("The Default Wind Direction is 0\n");
                  fprintf(fptr, "0\n");
               }
            }
      }
   }
   else
   {
      num = fread(TempSN, sizeof(char), 189, fptr);

      num = ParseData(TempSN, num, &SNs[0], 90);

      if(!wet->weather_a && !wet->weather_b)
      {
         if(SNs[7] == 0x01)
            wet->weather_a = TRUE;
         else
            wet->weather_b = TRUE;
      }

      j = 0;
      while((SNs[7+8*j] == 0x01) || (SNs[7+8*j] == 0x20) || (SNs[7+8*j] == 0x12) ||
            (SNs[7+8*j] == 0x1D) || (SNs[7+8*j] == 0x10))
      {
         if(SNs[7+8*j] == 0x01)
         {
            for(i=0; i<8; i++)
               wet->ds2401[wet->found_2401][7-i] = SNs[8*j + i];

            printf("Found DS2401\n");
            for(i=7; i>=0; i--)
               printf("%02X", wet->ds2401[wet->found_2401][i]);
            printf("\n");

            wet->found_2401++;
         }

         if(SNs[7+8*j] == 0x20)
         {
            if(!wet->found_dir)
            {
               for(i=0; i<8; i++)
                  wet->dsdir[7-i] = SNs[8*j + i];

               owSerialNum(portnum, wet->dsdir, FALSE);
               if(!owVerify(portnum, FALSE))
                  ret = FALSE;

               wet->found_dir = TRUE;
               printf("Found DS2450\n");
               for(i=7; i>=0; i--)
                  printf("%02X", wet->dsdir[i]);
               printf("\n");

               if(!SetupAtoDControl(portnum, wet->dsdir, &wet->ctrl[0], &msg[0]))
                  return FALSE;
               if(!WriteAtoD(portnum, FALSE, wet->dsdir, &wet->ctrl[0], 0x08, 0x11))
                  return FALSE;

            }
         }

         if(SNs[7+8*j] == 0x12)
         {
            if(!wet->found_dir)
            {
               for(i=0; i<8; i++)
                  wet->dsdir[7-i] = SNs[8*j + i];

               owSerialNum(portnum, wet->dsdir, FALSE);
               if(!owVerify(portnum, FALSE))
                  ret = FALSE;

               wet->found_dir = TRUE;
               printf("Found DS2406\n");
               for(i=7; i>=0; i--)
                  printf("%02X", wet->dsdir[i]);
               printf("\n");
            }
         }

         if(SNs[7+8*j] == 0x1D)
         {
            for(i=0; i<8; i++)
               wet->ds2423[7-i] = SNs[8*j + i];

            owSerialNum(portnum, wet->ds2423, FALSE);
            if(!owVerify(portnum, FALSE))
               ret = FALSE;

            wet->found_2423 = TRUE;
            printf("Found DS2423\n");
            for(i=7; i>=0; i--)
               printf("%02X", wet->ds2423[i]);
            printf("\n");
         }

         if(SNs[7+8*j] == 0x10)
         {
            for(i=0; i<8; i++)
               wet->ds1820[7-i] = SNs[8*j + i];

            owSerialNum(portnum, wet->ds1820, FALSE);
            if(!owVerify(portnum, FALSE))
               ret = FALSE;

            wet->found_1820 = TRUE;
            printf("Found DS1820\n");
            for(i=7; i>=0; i--)
               printf("%02X", wet->ds1820[i]);
            printf("\n");
         }

         j++;
      }

      if(wet->weather_a)
      {
         if((TempSN[0xBB] <= 0x40) && (TempSN[0xBB] >= 0x30))
            if((TempSN[0xBC] <= 0x40) && (TempSN[0xBC] >= 0x30))
               wet->north = (TempSN[0xBB]-0x30)*10 + (TempSN[0xBC]-0x30);
            else
               wet->north = TempSN[0xBB] - 0x30;
         else
            wet->north = 0;

         if(wet->found_dir && wet->found_1820 && wet->found_2423 &&
           (wet->found_2401 == 8))
            ret = TRUE;
      }
      else if(wet->weather_b)
      {
         if((TempSN[0x33] <= 0x40) && (TempSN[0x33] >= 0x30))
            if((TempSN[0x34] <= 0x40) && (TempSN[0x34] >= 0x30))
               wet->north = (TempSN[0x33]-0x30)*10 + (TempSN[0x34]-0x30);
            else
               wet->north = TempSN[0x33] - 0x30;
         else
            wet->north = 0;

         if(wet->found_dir && wet->found_1820 && wet->found_2423)
            ret = TRUE;
      }

   }

   if(wet->weather_a || wet->weather_b)
   {
      if(fptr != 0)
      {
         fclose(fptr);
      }
   }

   return ret;
}
예제 #8
0
// -------------------------------------------------------------------------
// SUBROUTINE - GetDir
//
// This routine looks at the DS2401 serial numbers or the DS2450 volts to
// determine which direction the wind is blowing.
//
// Returns: The direction in a 0-15 order and 16 for no DS2401s found
//
int GetDir(int portnum, WeatherStruct *wet)
{
   SwitchProps st;
   uchar temp_dir[MAXDIR][8];
   int i,j;
   int numdir = 0;
   int firstmatch = 0, secondmatch = 0;
   int firstindex = 0;
   int found = TRUE;
   int ret = 16;
   float prslt[4];

   temp_dir[0][0] = 0x00;

   if(wet->weather_a)
   {
      st.Chan_A = FALSE;
      st.Chan_B = TRUE;
      if(SetSwitch12(portnum, &wet->dsdir[0], st))
      {
         numdir = FindDevices(portnum, &temp_dir[0], DIR_FAMILY, MAXDIR);
      }
      else
         printf("Error on setting up the switch\n");

      if(numdir == 0)
         ret = 16;

      if(numdir == 2)
      {
         for(i=0; i<8; i++)
         {
            for(j=0; j<8; j++)
               if(temp_dir[0][j] != wet->ds2401[i][j])
               {
                  found = FALSE;
                  break;
               }
               else
                  found = TRUE;

            if(found)
            {
               firstindex = i;
               firstmatch = conv[i];
               found = FALSE;
               break;
            }
         }

         for(i=0; i<8; i++)
         {
            if(i != firstindex)
            {
               for(j=0; j<8; j++)
               {
                  if(temp_dir[1][j] != wet->ds2401[i][j])
                  {
                     found = FALSE;
                     break;
                  }
                  else
                     found = TRUE;
               }
            }
            if(found)
            {
               secondmatch = conv[i];
               break;
            }
         }

         if(((firstmatch == 0) || (secondmatch == 0)) &&
            ((firstmatch == 14) || (secondmatch == 14)))
            ret = 15;
         else
            ret = (firstmatch+secondmatch)/2;
      }

      if(numdir == 1)
      {
         for(i=0; i<8; i++)
         {
            found = TRUE;

            for(j=0; j<8; j++)
               if(temp_dir[0][j] != wet->ds2401[i][j])
                  found = FALSE;

            if(found)
            {
               ret = conv[i];
               break;
            }
         }

      }

      st.Chan_A = FALSE;
      st.Chan_B = FALSE;
      if(!SetSwitch12(portnum, &wet->dsdir[0], st))
      {
         msDelay(10);
         if(!SetSwitch12(portnum, &wet->dsdir[0], st))
         {
            printf("Failed to close channel B\n");
         }
      }
   }
   else if(wet->weather_b)
   {
      // read wind direction for the DS2450 weather station
      if(DoAtoDConversion(portnum, FALSE, &wet->dsdir[0]))
      {
         if(ReadAtoDResults(portnum, FALSE, &wet->dsdir[0], &prslt[0], &wet->ctrl[0]))
         {
            for(i=0; i<16; i++)
            {
               if( ((prslt[0] <= (float)conv_table[i][0]+0.25) &&
                    (prslt[0] >= (float)conv_table[i][0]-0.25)) &&
                   ((prslt[1] <= (float)conv_table[i][1]+0.25) &&
                    (prslt[1] >= (float)conv_table[i][1]-0.25)) &&
                   ((prslt[2] <= (float)conv_table[i][2]+0.25) &&
                    (prslt[2] >= (float)conv_table[i][2]-0.25)) &&
                   ((prslt[3] <= (float)conv_table[i][3]+0.25) &&
                    (prslt[3] >= (float)conv_table[i][3]-0.25)) )
               {
                  ret = i;
                  break;
               }
            }
         }
         else
         {
            printf("\n");
            printf("\nError reading channel, verify device present: %d\n",
            (int)owVerify(portnum, FALSE));
         }
      }
   }

   return ret;
}
예제 #9
0
// -------------------------------------------------------------------------
// SUBROUTINE - SetupWet
//
// This routine sets up a weather station given the serial numbers for the
// weather station.
//
// Returns: TRUE, if the weather station was setup and FALSE if it was not.
//
int SetupWet(int portnum, WeatherStruct *wet, int nor)
{
   int i,j;
   int ret = FALSE;
   char msg[45];

   wet->north = nor;

   if((wet->ds2401 == NULL) || wet->weather_b)
   {
      wet->weather_b = TRUE;
      wet->weather_a = FALSE;

      // Setting up the DS2450
      owSerialNum(portnum, wet->dsdir, FALSE);
      if(!owVerify(portnum, FALSE))
      {
         printf("The DS2450 was not found.\n");
         return FALSE;
      }
      else
      {
         // Printing the Serial number for DS2450
         printf("Set DS2450\n");
         for(i=7; i>=0; i--)
            printf("%02X", wet->dsdir[i]);
         printf("\n");
      }

      // Setting up the DS1820
      owSerialNum(portnum, wet->ds1820, FALSE);
      if(!owVerify(portnum, FALSE))
      {
         printf("The DS1820 was not found.\n");
         return FALSE;
      }
      else
      {
         // Printing the Serial number for DS1820
         printf("Set DS1820\n");
         for(i=7; i>=0; i--)
            printf("%02X", wet->ds1820[i]);
         printf("\n");
      }

      // Setting up the DS2423
      owSerialNum(portnum, wet->ds2423, FALSE);
      if(!owVerify(portnum, FALSE))
      {
         printf("The DS2423 was not found.\n");
         return FALSE;
      }
      else
      {
         // Printing the Serial number for DS2423
         printf("Set DS2423\n");
         for(i=7; i>=0; i--)
            printf("%02X", wet->ds2423[i]);
         printf("\n");
      }

      if(SetupAtoDControl(portnum, &wet->dsdir[0], &wet->ctrl[0], &msg[0]))
         if(WriteAtoD(portnum, FALSE, &wet->dsdir[0], &wet->ctrl[0], 0x08, 0x11))
            ret = TRUE;

   }
   else
   {
      wet->weather_a = TRUE;
      wet->weather_b = FALSE;

      printf("Set DS2401\n");
      for(i=0; i<8; i++)
      {
         // Printing the Serial number for DS2401
         for(j=7; j>=0; j--)
            printf("%02X", wet->ds2401[i][j]);
         printf("\n");
      }

      // Setting up the DS2423
      owSerialNum(portnum, wet->ds2423, FALSE);
      if(!owVerify(portnum, FALSE))
      {
         printf("The DS2423 was not found.\n");
         return FALSE;
      }
      else
      {
         // Printing the Serial number for DS2423
         printf("Set DS2423\n");
         for(i=7; i>=0; i--)
            printf("%02X", wet->ds2423[i]);
         printf("\n");
      }

      // Setting up the DS1820
      owSerialNum(portnum, wet->ds1820, FALSE);
      if(!owVerify(portnum, FALSE))
      {
         printf("The DS1820 was not found.\n");
         return FALSE;
      }
      else
      {
         // Printing the Serial number for DS1820
         printf("Set DS1820\n");
         for(i=7; i>=0; i--)
            printf("%02X", wet->ds1820[i]);
         printf("\n");
      }

      // Setting up the DS2406
      owSerialNum(portnum, wet->dsdir, FALSE);
      if(!owVerify(portnum, FALSE))
      {
         printf("The DS2406 was not found.\n");
         return FALSE;
      }
      else
      {
         // Printing the Serial number for DS2406
         printf("Set DS2450\n");
         for(i=7; i>=0; i--)
            printf("%02X", wet->dsdir[i]);
         printf("\n");
      }

      ret = TRUE;
   }

   return ret;
}
예제 #10
0
//----------------------------------------------------------------------
//  Read a specified number of pages in overdrive
//
// 'portnum'  - number 0 to MAX_PORTNUM-1.  This number is provided to
//              indicate the symbolic port number.
//
int ReadPages(int portnum, int start_pg, int num_pgs, int *last_pg, uchar *finalbuf)
{
   int skip_overaccess = 0, skip_access = 0;
   uchar pkt[60];
   int len,i;
   uchar  SerialNumber[8];
   ushort lastcrc16;

   // read the rom number 
   owSerialNum(portnum,SerialNumber,TRUE);

   // verify device is in overdrive
   if (current_speed[portnum] == MODE_OVERDRIVE)
   {
      if (owVerify(portnum,FALSE)) 
         skip_overaccess = 1;
   }

   if (!skip_overaccess)
   {
      if (owOverdriveAccess(portnum))
         current_speed[portnum] = MODE_OVERDRIVE;
      else
         current_speed[portnum] = MODE_NORMAL;
   }

   // loop while there is pages to read
   do
   {
      // create a packet to read a page
      len = 0;
      setcrc16(portnum,0);
      // optional skip access on subsequent pages 
      if (!skip_access)
      {  
         // match
         pkt[len++] = 0x55; 
         // rom number
         for (i = 0; i < 8; i++)
            pkt[len++] = SerialNumber[i];
         // read memory with crc command 
         pkt[len] = 0xA5; 
         docrc16(portnum,pkt[len++]);         
         // address
         pkt[len] = (uchar)((*last_pg << 5) & 0xFF);
         docrc16(portnum,pkt[len++]);         
         pkt[len] = (uchar)(*last_pg >> 3); 
         docrc16(portnum,pkt[len++]);         
      }

      // set 32 reads for data and 2 for crc
      for (i = 0; i < 34; i++)
         pkt[len++] = 0xFF; 
         
      // send the bytes
      if (owBlock(portnum,!skip_access,pkt,len))
      {
         // calucate the CRC over the last 34 bytes
         for (i = 0; i < 34; i++)
            lastcrc16 = docrc16(portnum,pkt[len - 34 + i]);

         // check crc
         if (lastcrc16 == 0xB001)
         {
            // copy the data into the buffer
#ifdef LetsCrashTheCompiler
	   for (i = 0; i < 32; i++)
	     finalbuf[i + (*last_pg - start_pg) * 32] = pkt[len - 34 + i];
#endif
	   {
	     ushort k;
	     for (i = 0; i < 32; i++) {
	       k=i + (*last_pg - start_pg) * 32;
	       finalbuf[k] = pkt[len - 34 + i];
	     }
	   }
            // change number of pages 
            *last_pg = *last_pg + 1;

            // now skip access 
            skip_access = TRUE;
         }
         else
            return FALSE;
      }
      else
         return FALSE;
   }