Exemplo n.º 1
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;
}
Exemplo n.º 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;
}
Exemplo n.º 3
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;
}