コード例 #1
0
int read_recorded_data_USB504( usb_dev_handle *udev, configurationBlock *cblock, usb504_data *data_504 )
{
  __u8 request_data[3] = {0x03, 0xff, 0xff};  // Request Recorded Data
  __u8 acknowledge[3] =  {0x00, 0x00, 0x00};  // Acknowledge string
  int packet_size = 512;
  int memory_size = 0xfe00;
  __u8 rdata[0xfe00];  // max data size
  int num_packets;
  unsigned int size = 0;
  int ret;
  int i;
  struct tm ltime;
  time_t currentTime;

  time(&currentTime);
  localtime_r(&currentTime, &ltime);  // set daylight savings field

  stop_logging_USB500(udev, cblock);  // must stop logging before reading the data.
  unlock_device_USB500(udev);

  ret = usb_bulk_write(udev, USB_ENDPOINT_OUT | 2,  (char *)request_data, 3, USB500_WAIT_WRITE);
  if (ret < 0) {
    perror("Error in requesting configuration (Bulk Write)");
  }
  do {
    ret = usb_bulk_read(udev, USB_ENDPOINT_IN|2, (char *) acknowledge, 3, USB500_WAIT_READ);
    if ( ret < 0 ) {
      perror("Error in acknowledging read data from USB 500");
    }
  } while (acknowledge[0] != 0x2);

  size = ((acknowledge[2] << 8) | acknowledge[1]);
  if (size != memory_size) {
    printf("Memory Error mismatch. size = %#x  should be %#x\n", size, memory_size);
    return -1;
  }

  num_packets = memory_size / packet_size;
  for ( i = 0; i < num_packets; i++ ) {
    do {
      ret = usb_bulk_read(udev, USB_ENDPOINT_IN | 2, (char *) &rdata[i*packet_size], packet_size, USB500_WAIT_READ);
      if (ret < 0) {
	perror("Error reading data.  Retrying.");
      }
    } while (ret != packet_size);
  }

  lock_device_USB500(udev);

  ltime.tm_sec = cblock->startTimeSeconds;
  ltime.tm_min = cblock->startTimeMinutes;
  ltime.tm_hour = cblock->startTimeHours;
  ltime.tm_mday = cblock->startTimeDay;
  ltime.tm_mon = cblock->startTimeMonth - 1;
  ltime.tm_year = cblock->startTimeYear + 100;
  data_504[0].time = mktime(&ltime);  // get local time stamp

  for (i = 0; i < cblock->sampleCount; i++) {
      data_504[i].time = data_504[0].time + i*cblock->sampleRate;
      data_504[i].current = cblock->calibrationMValue*rdata[i] + cblock->calibrationCValue;
      data_504[i].current *= cblock->capScalingFactor;
  }

  start_logging_USB500(udev, cblock); 
  return cblock->sampleCount;
}
コード例 #2
0
int main( void )
{
  usb_dev_handle *udev = NULL;
  configurationBlock cb;
  time_t currentTime;
  struct tm ltime;
  float t, logew, dewPoint;
  int i, j;
  usb501_data data_501[USB500_MAX_VALUES];
  usb502_data data_502[USB500_MAX_VALUES];
  usb503_data data_503[USB500_MAX_VALUES];
  usb504_data data_504[USB500_MAX_VALUES];  
  char dates[40];
  char name[16];
  char ans[80];
  int type;

  udev = usb_device_find_USB500(USB500_VID, USB500_PID);
  if (udev) {
    printf("Success, found a USB 500!\n");
    printf("Size of configuration block: %d\n", (int) sizeof(cb));
  } else {
    printf("Failure, did not find a USB 500!\n");
    return 0;
  }
  printf("\n\n");

  while (1) {
    printf("1. Status\n");
    printf("2. Configure USB 500 device for logging\n");
    printf("3. Download data\n");
    printf("4. Repair SRAM\n");
    printf("5. Exit\n");
    printf("Please select from 1-5:  \n");

    scanf("%s", ans);
    switch(ans[0]) {
      case '1':
        type = read_configuration_block_USB500(udev, &cb);
	printf("\n\n");
	switch(type) {
	  case 1:  printf("Device:\t\t\t USB-501 Temperature Logger (version 1.6 and earlier)\n"); break;
	  case 2:  printf("Device:\t\t\t USB-501 Temperature Logger (version 1.7 and later)\n"); break;
	  case 3:  printf("Device:\t\t\t USB-502 Humidity and Temperature Logger\n"); break;
	  case 4:  printf("Device:\t\t\t USB-503 Voltage Logger\n"); break;
    	  case 5:  printf("Device:\t\t\t USB-504 Current Logger\n"); break;
	  case 6:  printf("Device:\t\t\t USB-503B Voltage Logger\n"); break;
    	  case 7:  printf("Device:\t\t\t USB-504B Current Logger\n"); break;
          default: printf("Device:\t\t\t Unkown\n"); break;
	}
	printf("Device Name:\t\t %s\n", cb.name);
	if (cb.flagBits & LOGGING_STATE) {
          if (cb.startTimeOffset == 0) {
	    printf("Status:\t\t\t Logging\n");
	  } else {
	    printf("Status:\t\t\t Delayed Start\n");
            printf("Delay:\t\t\t %ld\n", cb.startTimeOffset);
	  }
	} else {
  	  printf("Status:\t\t\t Off\n");
	}

        ltime.tm_sec = cb.startTimeSeconds;
        ltime.tm_min = cb.startTimeMinutes;
        ltime.tm_hour = cb.startTimeHours;
        ltime.tm_mday = cb.startTimeDay;
        ltime.tm_mon = cb.startTimeMonth - 1;
        ltime.tm_year = cb.startTimeYear + 100;
        strftime(dates, 79, "%m/%d/%Y", &ltime);
	printf("Start Date:\t\t %s\n", dates);
        strftime(dates, 79, "%r", &ltime);
	printf("Start Time:\t\t %s\n", dates);

	printf("Number of Readings:\t %-d\n", cb.sampleCount);
	printf("Sample Interval:\t %d seconds\n", cb.sampleRate);
	if (type < 4) {            // USB-501 or USB-502
  	  if (cb.inputType == 0) { // Celsius
	    printf("Scale:\t\t\t Celsius");
	  } else {
	    printf("Scale:\t\t\t Fahrenheit");
	  }
	  if (cb.type == 3) {
	    printf(", %%rh\n");
	  } else {
	    printf("\n");
	  }
	} else {                   // USB-503 or USB-504
	  if (cb.inputType == 0) {
	    printf("Calibration:\t\t Standard Calibration.\n");
	  } else {
	    printf("Calibration:\t\t Custom Calibration.\n");
	  }
	  if (cb.flagBits2 & 0x1) {
	    printf("LEDs:\t\t\t Disabled to save battery power.\n");
	  } else {
	    printf("LEDs:\t\t\t Enabled.\n");
	  }
	  if (cb.flagBits2 & 0x2) {
	    printf("Scale:\t\t\t Negative gradient scaling.\n");
	  } else {
	    printf("Scale:\t\t\t Normal positive gradient scaling.\n");
	  }
	}
	if (cb.flagBits & HIGH_ALARM_STATE) {
	  printf("High Alarm:\t\t Enabled\n");
	} else {
	  printf("High Alarm:\t\t Disabled\n");	  
	}
	if (cb.flagBits & LOW_ALARM_STATE) {
	  printf("Low Alarm:\t\t Enabled\n");
	} else {
	  printf("Low Alarm:\t\t Disabled\n");	  
	}
	printf("Serial Number:\t\t %ld\n", cb.serialNumber);
	strncpy(name, cb.version, 4);
	printf("Firmware Version:\t %s\n", name);
	if (type >= 4) {
	  printf("Measurement Unit:\t %s.\n", cb.displayUnitText);
	  printf("User Calibration Input 1: %s.\n", cb.calibrationInput1Text);
  	  printf("User Calibration Output 1: %s.\n", cb.calibrationInput1Text);
  	  printf("User Calibration Input 2: %s.\n", cb.calibrationOutput2Text);
    	  printf("User Calibration Output 2: %s.\n", cb.calibrationOutput2Text);
	  printf("Scaling Factor:\t\t %f\n", cb.capScalingFactor);
	  printf("High Alarm Level Text: %s.\n", cb.highAlarmLevelText);
	  printf("Low Alarm Level Text: %s.\n", cb.lowAlarmLevelText);
	  printf("Default Range Description Text: %s.\n", cb.defaultRangeDescriptionText);
	  printf("Default Input Unit Text: %s.\n", cb.defaultInputUnitText);
	  printf("Default Display Unit: %s.\n", cb.defaultDisplayUnit);
	  printf("Default Calibration Input 1 Text: %s.\n", cb.defaultCalibrationInput1Text);
	  printf("Default Calibration Output 1 Text: %s.\n", cb.defaultCalibrationOutput1Text);
	  printf("Default Calibration Input 2 Text: %s.\n", cb.defaultCalibrationInput2Text);
	  printf("Default Calibration Output 2 Text: %s.\n", cb.defaultCalibrationOutput2Text);
	  printf("Default High Alarm Level Text: %s.\n", cb.defaultHighAlarmLevelText);
	  printf("Default Low Alarm Level Text: %s.\n", cb.defaultLowAlarmLevelText);
	}
	printf("\n\n");
	break;
      case '2':
        stop_logging_USB500(udev, &cb);
        /* initialize the parameter block */

        // sanity check
        printf("cb.type = %d\n", cb.type);
        printf("serial number = %ld\n", cb.serialNumber);
	cb.command = 0x0;
	printf("Device name = \"%s\": ",cb.name);
	tcflush(0, TCIOFLUSH);
	getchar();
	fgets(name, 15, stdin);
	if (name[0] != '\n') {
  	  cb.name[15] = '\0';  // null terminate
	  j = 0;
	  for (i = 0; i < strlen(cb.name); i++) {
	    switch (name[i]) {
	    case '\r':
	    case '\n':
	    case '%':
	    case '&':
	    case '*':
	    case ',':
	    case '.':
	    case '/':
	    case ':':
	    case '<':
	    case '>':
	    case '?':
	    case '\\':
	    case '(':
	    case ')':
	      break;
	    default:
	      cb.name[j++] = name[i];
	    }
	  }
	}
        if (type < 4) {
  	  printf("0 = Celsius, 1 = Fahrenheit: ");
	  scanf("%hhd", &cb.inputType);
	  if (cb.inputType == 0) { // Celsius
	    cb.calibrationMValue = 0.5;
	    cb.calibrationCValue = -40;
	  } else {                // Fahrenheit
	    cb.inputType = 1;
	    cb.calibrationMValue = 1.0;
	    cb.calibrationCValue = -40;
	  }
	}

	printf("Enter sample rate [seconds/sample].  Must be > 10:  ");
	scanf("%hd", &cb.sampleRate);
	if (cb.sampleRate < 10) cb.sampleRate = 10;
        write_configuration_block_USB500(udev, &cb);

        /* uncomment to set alarms */
        set_alarm(udev, &cb);
	
        time(&currentTime);
	printf("Setting Device time to: %s and start logging\n", ctime(&currentTime));
        start_logging_USB500(udev, &cb); 
	break;

      case '3':
        type = read_configuration_block_USB500(udev, &cb);
        switch(type) {
          case 1:
          case 2:
            read_recorded_data_USB501(udev, &cb, data_501);
   	    printf("\n\nModel: USB-501\n");
	    if (cb.inputType == 0) {
	      printf("Sample         Date/Time            Temperature (C) \n");
	    } else {
	      printf("Sample         Date/Time            Temperature (F) \n");
	    }
	    for (i = 0; i < cb.sampleCount; i++) {
              localtime_r(&data_501[i].time, &ltime);
              strftime(dates, 79, "%D %r", &ltime);
              printf("%5d    %s           %.2f  \n", i+1, dates, data_501[i].temperature);
	    }
	    printf("\n\n");
            printf("Save to file? [y/n] ");
            scanf("%s", ans);
	    if (ans[0] == 'y') write_recorded_data_USB501(&cb, data_501);
	    break;
          case 3:
            read_recorded_data_USB502(udev, &cb, data_502);
	    printf("\n\nModel: USB-502\n");
	    if (cb.inputType == 0) {
	      printf("Sample         Date/Time            Temperature (C)       Humidity    Dew point (C)\n");
  	    } else {
	      printf("Sample         Date/Time            Temperature (F)       Humidity    Dew point (F)\n");
	    }
	    for (i = 0; i < cb.sampleCount; i++) {
	      localtime_r(&data_502[i].time, &ltime);
	      if (cb.inputType == 0) {
	        t = data_502[i].temperature;
	      } else {
	        t = fahr2celsius(data_502[i].temperature);
	      }
	      logew = (0.66077 +(7.5*t/(237.3 + t))) + (log10(data_502[i].humidity) - 2.0);
	      dewPoint = ((0.66077 - logew)*237.3) / (logew - 8.16077);
	      if (cb.inputType == 1) dewPoint = celsius2fahr(dewPoint);
	      strftime(dates, 79, "%D %r", &ltime);
	      printf("%5d    %s           %.2f               %.1f        %.1f\n",
		     i+1, dates, data_502[i].temperature, data_502[i].humidity, dewPoint);
	    }	
	    printf("\n\n");
	    printf("Save to file? [y/n] ");
	    scanf("%s", ans);
	    if (ans[0] == 'y') write_recorded_data_USB502(&cb, data_502);
	    break;
          case 4:
          case 6:
            read_recorded_data_USB503(udev, &cb, data_503);
	    printf("\n\nModel: USB-503\n");
	    printf("Sample         Date/Time            Voltage (V) \n");
	    for (i = 0; i < cb.sampleCount; i++) {
              localtime_r(&data_503[i].time, &ltime);
              strftime(dates, 79, "%D %r", &ltime);
              printf("%5d    %s           %.2f  \n", i+1, dates, data_503[i].voltage);
	    }
	    printf("\n\n");
            printf("Save to file? [y/n] ");
            scanf("%s", ans);
	    if (ans[0] == 'y') write_recorded_data_USB503(&cb, data_503);
	    break;
          case 5:
          case 7:
	    read_recorded_data_USB504(udev, &cb, data_504);
	    printf("Sample         Date/Time            Current (mA)\n");
	    for (i = 0; i < cb.sampleCount; i++) {
              localtime_r(&data_504[i].time, &ltime);
              strftime(dates, 79, "%D %r", &ltime);
              printf("%5d    %s           %.2f  \n", i+1, dates, data_504[i].current);
	    }
	    printf("\n\n");
            printf("Save to file? [y/n] ");
            scanf("%s", ans);
	    if (ans[0] == 'y') write_recorded_data_USB504(&cb, data_504);
	    break;
	}
        break;

      case '4':
        printf("******* WARNING*******\n");
        printf("Only proceed if your device is not working.\n");
        printf("Do you want to proceed? [y/n]");
        scanf("%s", ans);
        if (ans[0] == 'n') break;
        /* get the serial number and software version */
        read_configuration_block_USB500(udev, &cb);
        printf("    1. USB-501 version 1.6 and earlier.\n");
        printf("    2. USB-501 version 1.7 and later.\n");
        printf("    3. USB-502\n");
        printf("Enter your Device type [1-3]: \n");
        scanf("%hhd", &cb.type);
        strncpy(cb.name, "EasyLog USB", 15);
	// strncpy(cb.version, "v2.0", 4);
        cb.inputType = 1;
        cb.command = 0x0;
        cb.calibrationMValue = 1.0;
	cb.calibrationCValue = -40;
        cb.sampleRate = 60;
        cb.flagBits = 0x0;
        time(&currentTime);
        localtime_r(&currentTime, &ltime);	
        cb.startTimeHours = ltime.tm_hour;
        cb.startTimeMinutes = ltime.tm_min;
	cb.startTimeSeconds = ltime.tm_sec;
	cb.startTimeDay = ltime.tm_mday;
	cb.startTimeMonth = ltime.tm_mon + 1;    // months from 1-12
	cb.startTimeYear = ltime.tm_year - 100;  // years start from 2000 not 1900.
        cb.alarm1.channel1.highAlarm = 0x0;
        cb.alarm1.channel1.lowAlarm = 0x0;
        write_configuration_block_USB500(udev, &cb);
	break;
      case '5':
	printf("Remove unit from USB port.\n");
	cleanup_USB500(udev);
        return 0;
        break;
    }
  }
}
コード例 #3
0
ファイル: test-usb500.c プロジェクト: chrismerck/mcc-libusb
int read_recorded_data_USB502( libusb_device_handle *udev, configurationBlock *cblock, usb502_data *data_502 )
{
  int packet_size = 512;
  int memory_size = 0x8000;
  int num_packets;
  unsigned int size = 0;
  int ret;
  int transferred;
  int i;
  struct tm ltime;
  time_t currentTime;
  uint8_t request_data[3] = {0x03, 0xff, 0xff};  // Request Recorded Data
  uint8_t acknowledge[3] =  {0x00, 0x00, 0x00};  // Acknowledge string
  uint8_t rdata[0x8000];  // max data size

  time(&currentTime);
  localtime_r(&currentTime, &ltime);  // set daylight savings field

  stop_logging_USB500(udev, cblock);  // must stop logging before reading the data.
  unlock_device_USB500(udev);

  ret = libusb_bulk_transfer(udev, LIBUSB_ENDPOINT_OUT | 2,  (unsigned char *)request_data, 3, &transferred, USB500_WAIT_WRITE);
  if (ret < 0) {
    perror("Error in requesting configuration (Bulk Write)");
  }
  do {
    ret = libusb_bulk_transfer(udev, LIBUSB_ENDPOINT_IN|2, (unsigned char *) acknowledge, 3, &transferred, USB500_WAIT_READ);
    if (ret < 0) {
      perror("Error in acknowledging read data from USB 500");
    }
  } while (acknowledge[0] != 0x2);

  size = ((acknowledge[2] << 8) | acknowledge[1]);
  if (size != memory_size) {
    printf("Memory Error mismatch. size = %#x  should be %#x\n", size, memory_size);
    return -1;
  }

  num_packets = memory_size / packet_size;
  for ( i = 0; i < num_packets; i++ ) {
    do {
      ret = libusb_bulk_transfer(udev, LIBUSB_ENDPOINT_IN | 2, (unsigned char *) &rdata[i*packet_size], 
				 packet_size, &transferred, USB500_WAIT_READ);
      if (ret < 0) {
	perror("Error reading data.  Retrying.");
      }
    } while (transferred != packet_size);
  }

  lock_device_USB500(udev);

  ltime.tm_sec = cblock->startTimeSeconds;
  ltime.tm_min = cblock->startTimeMinutes;
  ltime.tm_hour = cblock->startTimeHours;
  ltime.tm_mday = cblock->startTimeDay;
  ltime.tm_mon = cblock->startTimeMonth - 1;
  ltime.tm_year = cblock->startTimeYear + 100;
  data_502[0].time = mktime(&ltime);  // get local time stamp

  for (i = 0; i < cblock->sampleCount; i++) {
    data_502[i].time = data_502[0].time + i*cblock->sampleRate;
    data_502[i].temperature = cblock->calibrationMValue*rdata[2*i] + cblock->calibrationCValue;  // temperature first byte
    data_502[i].humidity = 0.5*rdata[2*i+1];                                                     // humidity second byte
  }
  start_logging_USB500(udev, cblock); 
  return cblock->sampleCount;
}