Example #1
0
int main (int argc, char **argv)
{
  libusb_device_handle *udev = NULL;
  struct tm calDate;

  float table_DE_AIN[NGAINS][NCHAN_DE][2];
  float table_SE_AIN[NCHAN_SE][2];

  int ch;
  int i, j, k, m;
  uint8_t options;
  char serial[9];
  uint8_t channel, channels;
  uint8_t range;
  uint8_t ranges[4] = {0, 0, 0, 0};
  uint16_t value;
  uint32_t count;
  double frequency, voltage;
  int ret;
  uint16_t dataAIn[8*512];  // holds 16 bit unsigned analog input data
  uint16_t data;
  int nchan, repeats;

  udev = NULL;

  ret = libusb_init(NULL);
  if (ret < 0) {
    perror("usb_device_find_USB_MCC: Failed to initialize libusb");
    exit(1);
  }

  if ((udev = usb_device_find_USB_MCC(BTH1208LS_PID, NULL))) {
    printf("Success, found a BTH 1208LS!\n");
  } else {
    printf("Failure, did not find a BTH 1208LS!\n");
    return 0;
  }

  // some initialization
  //print out the wMaxPacketSize.  Should be 64
  printf("wMaxPacketSize = %d\n", usb_get_max_packet_size(udev,0));

  usbBuildGainTable_DE_BTH1208LS(udev, table_DE_AIN);
  usbBuildGainTable_SE_BTH1208LS(udev, table_SE_AIN);
  for (i = 0; i < NGAINS; i++ ) {
    for (j = 0; j < NCHAN_DE; j++) {
      printf("Calibration Table: Range = %d Channel = %d Slope = %f   Offset = %f\n", 
	     i, j, table_DE_AIN[i][j][0], table_DE_AIN[i][j][1]);
    }
  }
  printf("\n");
  for (i = 0; i < NCHAN_SE; i++ ) {
    printf("Calibration Single Ended Table: Channel = %d Slope = %f   Offset = %f\n", 
	   i, table_SE_AIN[i][0], table_SE_AIN[i][1]);
  }

  usbCalDate_BTH1208LS(udev, &calDate);
  printf("\n");
  printf("MFG Calibration date = %s\n", asctime(&calDate));

  while(1) {
    printf("\nBTH 1208LS Testing\n");
    printf("----------------\n");
    printf("Hit 'b' to blink\n");
    printf("Hit 'c' to test counter\n");
    printf("Hit 'd' to test digitial IO\n");
    printf("Hit 'i' to test Analog Input\n");
    printf("Hit 'I' to test Analog Input Scan\n");
    printf("Hit 'o' to test Analog Output\n");
    printf("Hit 'x' to test Analog Input Scan (Multi-channel)\n");
    printf("Hit 'r' to reset the device\n");
    printf("Hit 's' to get serial number\n");
    printf("Hit 'S' to get Status\n");
    printf("Hit 'e' to exit\n");

    while((ch = getchar()) == '\0' || ch == '\n');
    switch(ch) {
      case 'b': /* test to see if LED blinks */
        printf("Enter number or times to blink: ");
        scanf("%hhd", &options);
        usbBlinkLED_BTH1208LS(udev, options);
	break;
      case 'e':
        cleanup_BTH1208LS(udev);
        return 0;
      case 'c':
        usbCounterReset_BTH1208LS(udev);
        printf("Connect AO 0 to CTR.\n");
        toContinue();
        for (i = 0; i < 100; i++) {                   // toggle
    	  usbAOut_BTH1208LS(udev, 0, 4095);
	  usbAOut_BTH1208LS(udev, 0, 0);
        }
	usbCounter_BTH1208LS(udev, &count);
        printf("Count = %d.  Should read 100.\n", count);
        break;
      case 'i':
	printf("Input channel DE [0-3]: ");
	scanf("%hhd", &channel);
        printf("Input range [0-7]: ");
	scanf("%hhd", &range);
	for (i = 0; i < 20; i++) {
	  usbAIn_BTH1208LS(udev, channel, DIFFERENTIAL, range, &value);
	  value = rint(value*table_DE_AIN[range][channel][0] + table_DE_AIN[range][channel][1]);
  	  printf("Range %d  Channel %d   Sample[%d] = %#x Volts = %lf\n",
		   range, channel,  i, value, volts_BTH1208LS(value, range));
	  usleep(50000);	  
	}
        break;
      case 'I':
	printf("Testing BTH-1208lS Analog Input Scan.\n");
	usbAInScanStop_BTH1208LS(udev);
        printf("Enter number of scans (less than 512): ");
        scanf("%d", &count);
	printf("Input channel 0-3: ");
        scanf("%hhd", &channel);
        printf("Enter sampling frequency [Hz]: ");
	scanf("%lf", &frequency);
        printf("Enter Range [0-7]: ");
        scanf("%hhd", &range);
        ranges[channel] = range;
	if (frequency > 100.) {
	  options = DIFFERENTIAL_MODE;
	} else {
	  options = DIFFERENTIAL_MODE | IMMEDIATE_TRANSFER_MODE;
	}
	usbAInScanStop_BTH1208LS(udev);
	usbAInScanClearFIFO_BTH1208LS(udev);
        usbAInScanConfig_BTH1208LS(udev, ranges);
	memset(dataAIn, 0x0, sizeof(dataAIn));
	sleep(1);
	usbAInScanConfigR_BTH1208LS(udev, ranges);
	for (i = 0; i < 4; i++) {
	  printf("Channel %d     range %d\n", i, ranges[i]);
	}
	usbAInScanStart_BTH1208LS(udev, count, 0x0, frequency, (0x1<<channel), options);
	ret = usbAInScanRead_BTH1208LS(udev, count, dataAIn, options);
	printf("Number samples read = %d\n", ret/2);
	for (i = 0; i < count; i++) {
	  dataAIn[i] = rint(dataAIn[i]*table_DE_AIN[range][channel][0] + table_DE_AIN[range][channel][1]);
          printf("Range %d Channel %d  Sample[%d] = %#x Volts = %lf\n", range, channel,
		 i, dataAIn[i], volts_BTH1208LS(dataAIn[i], range));
	}
        break;
      case 'x':
        printf("Testing BTH-1208LS Multi-Channel Analog Input Scan.\n");
        usbAInScanStop_BTH1208LS(udev);
        printf("Enter number of channels (1-4) :");
        scanf("%d", &nchan);
        printf("Enter number of scans (less than 512): ");
        scanf("%d", &count);
        printf("Enter number of repeats: ");
        scanf("%d", &repeats);
	printf("Enter sampling frequency: ");
        scanf("%lf", &frequency);
        // Build bitmap for the first nchan in channels.
        channels = 0;
        for (i = 0; i < nchan; i++) {
	  channels |= (1 << i);
	}
        frequency = 10000.;
	options = DIFFERENTIAL_MODE;
        // Always use BP_20V to make it easy (BP_20V is 0...)
        memset(ranges, BP_20V, sizeof(ranges));
        usbAInScanConfig_BTH1208LS(udev, ranges);
        // Run a loop for the specified number of repeats and
        // show the results...
        for (m = 0; m < repeats; m++) {
	  printf("\n\n---------------------------------------");
	  printf("\nrepeat: %d\n", m);
	  usbAInScanStart_BTH1208LS(udev, count*nchan, 0x0, frequency, channels, options);
	  ret = usbAInScanRead_BTH1208LS(udev, count*nchan,  dataAIn, options);
	  printf("Number samples read = %d\n", ret/2);
	  if (ret != nchan*count*2) { /* if (ret != count*2) */
	    printf("***ERROR***  ret = %d   count = %d  nchan = %d\n", ret, count, nchan);
	    continue;
	  } 
	  for (i = 0; i < count/nchan; i++) {
	    printf("%6d", i);
	    for (j = 0; j < nchan; j++)	{
	      k = i*nchan + j;
	      data = rint(dataAIn[k]*table_DE_AIN[range][j][0] + table_DE_AIN[range][j][1]);
	      printf(", %8.4f", volts_BTH1208LS(data, range));
	    } /* for (j - 0; j < 8, j++) */
	    printf("\n");
	  } /* for (i = 0; i < count; i++) */
	} /* for (m = 0; m < repeats; m++) */
	printf("\n\n---------------------------------------");
	break;
      case 'r':
        usbReset_BTH1208LS(udev);
        break;
      case 'o':
        printf("Test Analog Output\n");
        printf("Enter Channel [0-1] ");
        scanf("%hhd", &channel);
        printf("Enter voltage [0-2.5V]: ");
	scanf("%lf", &voltage);
        value = voltage * 4095 / 2.5;
	usbAOut_BTH1208LS(udev, channel, value);
	value = usbAOutR_BTH1208LS(udev, channel);
	printf("Analog Output Voltage = %f V\n", volts_BTH1208LS(value, UP_2_5V));
        break;
      case 's':
        usbGetSerialNumber_BTH1208LS(udev, serial);
        printf("Serial number = %s\n", serial);
        break;
      case 'S':
        printf("Status = %#x\n", usbStatus_BTH1208LS(udev));
	break;
      default:
        break;
    }
  }
}
Example #2
0
int main(int argc, char**argv)
{
  struct version_t version;
  struct tm date_base, date_exp;
  struct networkDeviceInfo_t network;
  struct in_addr ip_addr;

  DeviceInfo_TC32 device_info;
  uint8_t dioIn[2];
  uint32_t dioOut;
  int i;
  float temperature, CJC_temp;
  uint8_t channel;
  uint8_t tc_type;
  int ch;
  uint8_t options;
  uint8_t buf[32];
  in_addr_t address;

 start:
  device_info.device.connectCode = 0x0;   // default connect code
  device_info.device.frameID = 0;         // zero out the frameID

  if (argc == 2) {
    printf("E-TC32 IP address = %s\n", argv[1]);
    device_info.device.Address.sin_family = AF_INET;
    device_info.device.Address.sin_port = htons(COMMAND_PORT);
    device_info.device.Address.sin_addr.s_addr = INADDR_ANY;
    if (inet_aton(argv[1], &device_info.device.Address.sin_addr) == 0) {
      printf("Improper destination address.\n");
      return -1;
    }
  } else if (discoverDevice(&device_info.device, ETC32_PID) <= 0) {
    printf("No device found.\n");
    return -1;
  }

  if ((device_info.device.sock = openDevice(inet_addr(inet_ntoa(device_info.device.Address.sin_addr)),
					    device_info.device.connectCode)) < 0) {
    printf("Error opening socket\n");
    return -1;
  }

  device_info.units = UNITS_CELSIUS;
  device_info.wait = 0x0;
  for (i = 0; i< 64; i++) {
    device_info.config_values[i] = 0x0;   // disable all channels
  }

  while(1) {
    printf("\nE-TC32 Testing\n");
    printf("----------------\n");
    printf("Hit 'b' to blink\n");
    printf("Hit 'C' for A/D system calibration.\n");
    printf("Hit 'd' to test digitial IO.\n");
    printf("Hit 'e' to exit\n");
    printf("Hit 'j' for CJC compensation\n");
    printf("Hit 'r' to reset the device\n");
    printf("Hit 'n' to get networking information\n");
    printf("Hit 's' to get Status\n");
    printf("hit 'R' to read System Memory Map\n");
    printf("hit 'W' to write System Memory Map\n");
    printf("Hit 't' for temperature.\n");
    printf("Hit 'T' for multiple temperature readings.\n");
    printf("Hit 'v' for version and calibration date.\n");

    while((ch = getchar()) == '\0' || ch == '\n');
    switch(ch) {
      case 'b': /* test to see if LED blinks */
        printf("Enter number or times to blink: ");
        scanf("%hhd", &options);
	BlinkLED_E_TC32(&device_info, options);
        break;
      case 'C':
	printf("Performing A/D system offset calibration.\n");
	ADCal_E_TC32(&device_info);
	break;
      case 'd': /* test DIO */
	printf("\nTesting Digital I/O...\n");
	printf("connect pins DIO input [0-7] <--> DIO Output[0-7]\n");
	do {
	  printf("Enter a  number [0-0xff] : " );
	  scanf("%x", &dioOut);
	  DOutW_E_TC32(&device_info, BASE, dioOut);
	  DIn_E_TC32(&device_info, dioIn);
	  printf("The number you entered = %#x   Value read = %#x\n\n", dioOut, dioIn[0]);
	  for (i = 0; i < 8; i++) {
	    printf("Bit %d = %d\n", i, (dioIn[0]>>i) & 0x1);
	  }
        } while (toContinue());
        break;
      case 'j':
        for (channel = 0; channel < 32; channel++) {
	  CJC_E_TC32(&device_info, channel, &CJC_temp);
	  printf("Channel = %d   Temperature = %.2f C  %.2f F\n", channel, CJC_temp, CJC_temp*9./5. + 32.);
	}
        break;
      case 't':
        printf("Enter channel number [0-31]: ");
	scanf("%hhd", &channel);
	printf("Input Thermocouple type [J,K,R,S,T,N,E,B]: ");
	while((ch = getchar()) == '\0' || ch == '\n');
	switch(ch) {
	  case 'J': 
  	  case 'j':
	    tc_type = TC_TYPE_J; break;
  	  case 'K': 
  	  case 'k': 
	    tc_type = TC_TYPE_K; break;
	  case 'R':
	  case 'r':
	    tc_type = TC_TYPE_R; break;
  	  case 'S':
	  case 's':
	    tc_type = TC_TYPE_S; break;
   	  case 'T':
	  case 't':
	    tc_type = TC_TYPE_T; break;
     	  case 'N':
	  case 'n':
	    tc_type = TC_TYPE_N; break;
       	  case 'E':
  	  case 'e':
	    tc_type = TC_TYPE_E; break;
	  case 'B':
 	  case 'b':
	    tc_type = TC_TYPE_B; break;
	  default: tc_type = TC_TYPE_J; break;
	}
	for (i = 0; i < 64; i++) {
	  device_info.config_values[i] = CHAN_DISABLE;
	}
	device_info.config_values[channel] = tc_type;
	TinConfigW_E_TC32(&device_info);
	for (i = 0; i < 20; i++) {
	  Tin_E_TC32(&device_info, channel, UNITS_CELSIUS, 1, &temperature);
	  printf("Channel = %d   Temperature = %.2f C  %.2F\n", channel, temperature, temperature*9./5. + 32.);
	  sleep(1);
	}
        break;
      case 'r': 
	Reset_E_TC32(&device_info);
	sleep(2);
	goto start;
	break;
      case 's':
        Status_E_TC32(&device_info);
	if (device_info.status == 0) {
	  printf("No EXP detected.\n");
	} else {
	  printf("EXP detected.\n");
	}
	TinStatus_E_TC32(&device_info);
	printf("TinStatus = %#x\n", device_info.Tin_status[0]);
	OTDStatus_E_TC32(&device_info);
	printf("OTDStatus = %#x\n", device_info.OTD_status[0]);
	break;
      case 'T':
	printf("Read Multiple Thermocouple channels\n");
	printf("Input Thermocouple type [J,K,R,S,T,N,E,B]: ");
	while((ch = getchar()) == '\0' || ch == '\n');
	switch(ch) {
	  case 'J': 
  	  case 'j':
	    tc_type = TC_TYPE_J; break;
  	  case 'K': 
  	  case 'k': 
	    tc_type = TC_TYPE_K; break;
	  case 'R':
	  case 'r':
	    tc_type = TC_TYPE_R; break;
  	  case 'S':
	  case 's':
	    tc_type = TC_TYPE_S; break;
   	  case 'T':
	  case 't':
	    tc_type = TC_TYPE_T; break;
     	  case 'N':
	  case 'n':
	    tc_type = TC_TYPE_N; break;
       	  case 'E':
  	  case 'e':
	    tc_type = TC_TYPE_E; break;
	  case 'B':
 	  case 'b':
	    tc_type = TC_TYPE_B; break;
	  default: tc_type = TC_TYPE_J; break;
	}
	for (i = 0; i < 31; i++) {
	  device_info.config_values[i] = tc_type;
	}
	TinConfigW_E_TC32(&device_info);
	// Just read the even channels
	device_info.channel_mask[0] = 0x55555555;
	device_info.wait = 1;
	device_info.units = UNITS_CELSIUS;
	TinMultiple_E_TC32(&device_info);
	for (i = 0; i < 16; i++) {
	  printf("Channel[%d] Temperature = %f\n", 2*i, device_info.Tin_values[i]);
	}
	// Turn off the LED on the front pannel.
	device_info.config_measure[0] = 1;  //disable the OTD which turns off the LED.
	MeasureConfigW_E_TC32(&device_info);
	device_info.config_measure[0] = 0;  //enable the OTD;
	MeasureConfigW_E_TC32(&device_info);
        break;
      case 'v':
	Version_E_TC32(&device_info, &version);
	printf("Communications micro firmware version = %#x\n", version.version_comms);
	printf("Communcations mico bootloader firmware version = %#x\n", version.boot_version_comms);
	printf("Base measurement micro firmware version = %#x\n", version.version_base);
	printf("Base measurement micro bootloader firmware version = %#x\n", version.boot_version_base);
	printf("EXP measurement micro firmware version = %#x\n", version.version_exp);
	printf("EXP measurement micro bootloader firmware version = %#x\n\n", version.boot_version_exp);
	CalDateR_E_TC32(&device_info, &date_base, &date_exp);
	printf("Calibration date = %s\n", asctime(&date_base));
	break;
      case 'e':
	close(device_info.device.sock);
	printf("Success!\n");
        return(0);
        break;
      case 'n':
	NetworkConfig_E_TC32(&device_info, &network);
	ip_addr.s_addr = network.ip_address;
	printf("Device IP address:  %s\n", inet_ntoa(ip_addr));
	ip_addr.s_addr = network.subnet_mask;
	printf("Device subnet mask: %s\n", inet_ntoa(ip_addr));
	ip_addr.s_addr =  network.gateway_address;
	printf("Device gateway address: %s\n", inet_ntoa(ip_addr));
	break;
      case 'R':
	printf("Reading Settings Memory.\n");
	for (i = 0x0; i < 0x1f; i++) {
	  SettingsMemoryR_E_TC32(&device_info, i, 1, buf);
	  printf("address: %#x      value: %d\n", i, buf[0]);
	}
	break;
      case 'W':
	printf("Writing Settings Memory.\n");
	printf("Note: The could be dangerous if you add invalid data!!!\n");

	printf("Enter new default IP address (eg. 192.168.0.101): ");
	scanf("%s", buf);
	address = inet_addr((char *) buf);
	SettingsMemoryW_E_TC32(&device_info, 0x2, 4, (uint8_t*) &address);

	printf("Enter new default netmask (eg. 255.255.255.0): ");
	scanf("%s", buf);
	address = inet_addr((char *) buf);
	SettingsMemoryW_E_TC32(&device_info, 0x6, 4, (uint8_t*) &address);

	printf("Enter new default gateway (eg. 192.168.0.1): ");
	scanf("%s", buf);
	address = inet_addr((char *) buf);
	SettingsMemoryW_E_TC32(&device_info, 0xa, 4, (uint8_t*) &address);

	printf("Enter new Network options (0-3): ");
	scanf("%hhd", buf);
	SettingsMemoryW_E_TC32(&device_info, 0x0, 1, buf);

	printf("Reset or powercycle for changes to take effect.\n");
	
        break;

    }
  }
}
Example #3
0
int main (int argc, char **argv)
{
  int flag;
  libusb_device_handle *udev = NULL;
  struct tm calDate;
  int ch;
  int temp;
  int transferred;
  int i, j;
  int tc_type;
  uint8_t input;
  uint8_t channel, gain, rate, mode, flags;
  char serial[9];
  uint16_t version[4];
  float cjc[2];
  int queue_index;
  int data;
  int ret;
  double table_AIN[NGAINS_2408][2];
  double table_AO[NCHAN_AO_2408][2];
  float table_CJCGrad[nCJCGrad_2408];
  AInScanQueue scanQueue;
  int usb2408_2AO = FALSE;
  double voltage;
  double temperature;
  double frequency;
  double sine[512];
  int16_t sdata[512];  // holds 16 bit signed analog output data
  int32_t idata[512];  // holds 24 bit signed analog input data
  uint8_t status;
  uint16_t depth;
  uint16_t count;

  udev = NULL;

  ret = libusb_init(NULL);
  if (ret < 0) {
    perror("usb_device_find_USB_MCC: Failed to initialize libusb");
    exit(1);
  }

  if ((udev = usb_device_find_USB_MCC(USB2408_PID, NULL))) {
    printf("Success, found a USB 2408!\n");
  } else if ((udev = usb_device_find_USB_MCC(USB2408_2AO_PID, NULL))) {
    printf("Success, found a USB 2408_2AO!\n");
    usb2408_2AO = TRUE;
  } else {
    printf("Failure, did not find a USB 2408 or 2408_2AO!\n");
    return 0;
  }

  //print out the wMaxPacketSize.  Should be 64
  printf("wMaxPacketSize = %d\n", usb_get_max_packet_size(udev,0));

  usbBuildGainTable_USB2408(udev, table_AIN);
  for (i = 0; i < NGAINS_2408; i++) {
    printf("Gain: %d    Slope = %lf    Offset = %lf\n", i, table_AIN[i][0], table_AIN[i][1]);
  }

  printf("\n");
  usbBuildCJCGradientTable_USB2408(udev, table_CJCGrad);
  for (i = 0; i < nCJCGrad_2408; i++) {
    printf("Ch: %d    CJC gradient = %lf\n", i, table_CJCGrad[i]);
  }

  if (usb2408_2AO) {
    usbBuildGainTable_USB2408_2AO(udev, table_AO);
    printf("\n");
    for (i = 0; i < NCHAN_AO_2408; i++) {
      printf("VDAC%d:    Slope = %lf    Offset = %lf\n", i, table_AO[i][0], table_AO[i][1]);
    }
  }

  usbCalDate_USB2408(udev, &calDate);
  printf("\n");
  printf("MFG Calibration date = %s\n", asctime(&calDate));

  while(1) {
    printf("\nUSB 2408 Testing\n");
    printf("----------------\n");
    printf("Hit 'b' to blink\n");
    printf("Hit 'c' to test counter\n");
    printf("Hit 'C' to test continuous sampling at 1000 Hz.\n");
    printf("Hit 'd' to test digitial IO\n");
    printf("Hit 'i' to test Analog Input\n");
    printf("Hit 'I' to test Analog Input Scan\n");
    printf("Hit 'j' read CJC sensors.\n");
    printf("Hit 'o' to test Analog Output\n");
    printf("Hit 'O' to test Analog Output Scan\n");
    printf("Hit 'r' to reset the device\n");
    printf("Hit 's' to get serial number\n");
    printf("Hit 'S' to get Status\n");
    printf("Hit 't' to get TC temperature\n");
    printf("Hit 'v' to get version numbers\n");
    printf("Hit 'e' to exit\n");

    while((ch = getchar()) == '\0' || ch == '\n');

    switch(ch) {
      case 'b': /* test to see if led blinks */
        printf("Enter number or times to blink: ");
	scanf("%d", &temp);
        usbBlink_USB2408(udev, temp);
        break;
      case 'c':
        usbCounterInit_USB2408(udev, COUNTER0);
        printf("Connect DO0 to CTR0\n");
        toContinue();
        for (i = 0; i < 100; i++) {
	  usbDOut_USB2408(udev, 0x0, 0);
	  usbDOut_USB2408(udev, 0xff, 0);
        }
        printf("Count = %d.  Should read 100.\n", usbCounter_USB2408(udev, COUNTER0));
        break;
      case 'C':
	printf("Testing USB-2408 Analog Input Scan in Continuous mode 8 channels\n");
	printf("Hit any key to exit\n");
	usbAInScanStop_USB2408(udev);
	count = 0;        // for continuous mode
	rate = HZ1000;
	mode = DIFFERENTIAL;
	scanQueue.count = 8;
	for (i = 0; i < 8; i++ ) {
	  scanQueue.queue[i].channel = i;
	  scanQueue.queue[i].mode = mode;
	  scanQueue.queue[i].range = BP_10V;
	  scanQueue.queue[i].rate = rate;
	}
	usbAInScanQueueWrite_USB2408(udev, &scanQueue);
	
	for (i = 0; i < 512; i++) {
	  idata[i] = 0xdeadbeef;
	}
	usbAInScanStart_USB2408(udev, 100, count, 15);
	
	flag = fcntl(fileno(stdin), F_GETFL);
	fcntl(0, F_SETFL, flag | O_NONBLOCK);
        j = 0;
	do {
          ret = libusb_bulk_transfer(udev, LIBUSB_ENDPOINT_IN|1, (unsigned char *) idata, 512*4, &transferred, 1000);
	  if (ret < 0) {
	    perror(" Continuous scan error in libusb_bulk_transfer");
	  }
	  for (i = 0; i < 512; i++) {
	    queue_index = idata[i] >> 24;                         // MSB of data contains the queue index;
	    gain = scanQueue.queue[queue_index].range;
	    channel = scanQueue.queue[queue_index].channel;
	    data = int24ToInt(idata[i]);                          // convert from signed 24 to signed 32
	    data = data*table_AIN[gain][0] + table_AIN[gain][1];  // correct for non-linearities in A/D
	    printf("Sample %d Index %d Channel %d   gain = %d raw = %#x  voltage = %f\n",
		   i, queue_index, channel, gain, idata[i], volts_USB2408(gain, data));
	  }
	  // printf("bulk transfer = %d\n", j); // Each transfer contains 512 samples or 64 scans
	  j++;
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
        usbAInScanStop_USB2408(udev);
	usbReset_USB2408(udev);
        libusb_close(udev);
        sleep(2); // let things settle down.
        break;
      case 'd':
        printf("\nTesting Digital I/O....\n");
        do {
  	  printf("Enter a number [0-0xff] : " );
  	  scanf("%x", &temp);
	  usbDOut_USB2408(udev, (uint8_t)temp, 0);
	  input = usbDOutR_USB2408(udev, 0);
	  printf("The number you entered = %#x\n\n",input);
  	  input = usbDIn_USB2408(udev, 0);
  	  printf("The number you entered = %#x\n\n",input);
	} while (toContinue());
	break;
      case 'e':
        cleanup_USB2408(udev);
        return 0;
      case 'i':
        printf("Input channel [0-7]: ");
        scanf("%hhd", &channel);
	printf("Gain Range for channel %d:  1 = 10V  2 = 5V  2 = 2.5V Differential: ", channel);
	while((ch = getchar()) == '\0' || ch == '\n');
	switch(ch) {
  	  case '1': gain = BP_10V; break;
	  case '2': gain = BP_5V; break;
	  case '3': gain = BP_2_5V; break;
	  default:  gain = BP_10V; break;
	}
	rate = HZ1000;
	mode = DIFFERENTIAL;
	for (i = 0; i < 20; i++) {
  	  data = usbAIn_USB2408(udev, channel, mode, gain, rate, &flags);
	  data = data*table_AIN[gain][0] + table_AIN[gain][1];
	  printf("Channel %d  Sample[%d] = %#x  Volts = %lf\n", channel, i, data,
		 volts_USB2408(gain, data));
	  usleep(50000);
	}
	break;
      case 'I':
        printf("Testing USB-2408 Analog Input Scan\n");
	usbAInScanStop_USB2408(udev);
	printf("Input channel [0-7]: ");
        scanf("%hhd", &channel);
	printf("Input the number of scans (1-512): ");
	scanf("%hd", &count);
	printf("Gain Range for channel %d: 1 = 10V  2 = 5V  3 = 2.5V Differential: ", channel);
	while((ch = getchar()) == '\0' || ch == '\n');
	switch(ch) {
  	  case '1': gain = BP_10V; break;
	  case '2': gain = BP_5V; break;
	  case '3': gain = BP_2_5V; break;
	  default:  gain = BP_10V; break;
	}
	rate = HZ1000;
	mode = DIFFERENTIAL;
	scanQueue.count = 1;
	scanQueue.queue[0].channel = channel;
	scanQueue.queue[0].mode = mode;
	scanQueue.queue[0].range = gain;
	scanQueue.queue[0].rate = rate;

	for (i = 0; i < 512; i++) {
	  idata[i] = 0xdeadbeef;
	}

	usbAInScanQueueWrite_USB2408(udev, &scanQueue);
	usbAInScanStart_USB2408(udev, 900, count, 15);
	usbAInScanRead_USB2408(udev, count, 1, idata);
	usbAInScanStop_USB2408(udev);

	usbAInScanQueueRead_USB2408(udev, &scanQueue);
	for (i = 0; i < count; i++) {
	  queue_index = idata[i] >> 24;                         // MSB of data contains the queue index;
	  gain = scanQueue.queue[queue_index].range;
	  channel = scanQueue.queue[queue_index].channel;
	  data = int24ToInt(idata[i]);                          // convert from signed 24 to signed 32
  	  data = data*table_AIN[gain][0] + table_AIN[gain][1];  // correct for non-linearities in A/D
	  printf("Sample %d Index %d Channel %d   gain = %d raw = %#x  voltage = %f\n",
		 i, queue_index, channel, gain, idata[i], volts_USB2408(gain, data));
	}
	break; 

      case 'j':
	usbCJC_USB2408(udev, cjc);
	for (i = 0; i < 2; i++) {
	  printf("CJC sensor[%d] = %f degree C.\n", i, cjc[i]);
	}
	break;
      case 'O':
        if (!usb2408_2AO) {
	  printf("Analog output only on the USB-2408-2AO model.\n");
	  break;
	}
        channel = 0;
	printf("Test of Analog Output Scan.\n");
	printf("Hook scope up to VDAC 0\n");
	printf("Enter desired frequency of sine wave [Hz]: ");
	scanf("%lf", &frequency);
	for (i = 0; i < 512; i ++) {
	  sine[i] = 10*sin(2.*M_PI*i/128.);
	}
	voltsTos16_USB2408_2AO(sine, sdata, 512, table_AO[channel]);
	usbAOutScanStop_USB2408_2AO(udev);
	usbAOutScanStart_USB2408_2AO(udev, 128.*frequency, 0, (1 << channel));
	printf("Hit \'s <CR>\' to stop ");
	flag = fcntl(fileno(stdin), F_GETFL);
	fcntl(0, F_SETFL, flag | O_NONBLOCK);
	do {
	  libusb_bulk_transfer(udev, LIBUSB_ENDPOINT_OUT|1, (unsigned char *) sdata, sizeof(sdata), &transferred, 1000);
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
	usbAOutScanStop_USB2408_2AO(udev);
	break;
      case 'o':
	if (!usb2408_2AO) {
	  printf("Analog output only on the USB-2408-2AO model.\n");
	  break;
	}
        printf("output value on VDAC 0\n");
        do {
	  printf("Enter output voltage [-10 to 10]: ");
	  scanf("%lf", &voltage);
	  usbAOut_USB2408_2AO(udev, 0, voltage, table_AO);
        } while (toContinue());
	break;
      case 'r':
	usbReset_USB2408(udev);
	return 0;
	break;
      case 's':
        usbGetSerialNumber_USB2408(udev, serial);
        printf("Serial number = %s\n", serial);
        break;
      case 'S':
        printf("Status = %#x\n", usbStatus_USB2408(udev));
	status = usbAInScanStatus_USB2408(udev, &depth);
        printf("Analog In status = %#x, depth = %d\n", status, depth);

        break;
      case 't':
	printf("Input channel [0-7]: ");
        scanf("%hhd", &channel);
	printf("Input Thermocouple type [J,K,R,S,T,N,E,B]: ");
	while((ch = getchar()) == '\0' || ch == '\n');
	switch(ch) {
	  case 'J': 
  	  case 'j':
	    tc_type = TYPE_J; break;
  	  case 'K': 
  	  case 'k': 
	    tc_type = TYPE_K; break;
	  case 'R':
	  case 'r':
	    tc_type = TYPE_R; break;
  	  case 'S':
	  case 's':
	    tc_type = TYPE_S; break;
   	  case 'T':
	  case 't':
	    tc_type = TYPE_T; break;
     	  case 'N':
	  case 'n':
	    tc_type = TYPE_N; break;
       	  case 'E':
  	  case 'e':
	    tc_type = TYPE_E; break;
	  case 'B':
 	  case 'b':
	    tc_type = TYPE_B; break;
	  default: tc_type = TYPE_J; break;
	}
	temperature = tc_temperature_USB2408(udev, tc_type, channel);
	printf("Temperature = %.3f C  %.3f F\n", temperature, temperature*9./5. + 32.);
        break;
      case 'v':
        usbGetVersion_USB2408(udev, version);
	printf("USB micro firmware version = %x.%2.2x\n", version[0]/0x100, version[0]%0x100);
	printf("USB update firmware version = %x.%2.2x\n", version[1]/0x100, version[1]%0x100);
	printf("isolated micro firmware version = %x.%2.2x\n", version[2]/0x100, version[2]%0x100);
	printf("isolated update firmware version = %x.%2.2x\n", version[3]/0x100, version[3]%0x100);
	break;
      default:
        break;
    }
  }
}
Example #4
0
int main(int argc, char **argv) {

  int ch;
  uint8_t bIReg, bOReg;
  float temperature;
  int flag;
  char type;
  int ret;
  hid_device *hid = 0x0;  // Composite device with 1 interface.
  wchar_t serial[64];
  wchar_t wstr[MAX_STR];

  ret = hid_init();
  if (ret < 0) {
    fprintf(stderr, "hid_init failed with return code %d\n", ret);
    return -1;
  }

  if ((hid = hid_open(MCC_VID, USB_TC_PID, NULL)) > 0) {
    printf("USB-TC Device is found!\n");
  } else {
    fprintf(stderr, "USB-TC not found.\n");
    exit(1);
  }

  /* config mask 0x01 means all inputs */
  usbDConfigPort_USBTC(hid, DIO_DIR_OUT);
  usbDOut_USBTC(hid, 0x0);

  while(1) {
    printf("\nUSB TC Testing\n");
    printf("----------------\n");
    printf("Hit 'b' to blink LED\n");
    printf("Hit 'c' to calibrate\n");
    printf("Hit 'd' to test DIO\n");
    printf("Hit 'e' to exit\n");
    printf("Hit 'f' for burnout status\n");
    printf("Hit 'g' to get serial number\n");
    printf("Hit 'i' for information\n");
    printf("Hit 'r' to reset\n");
    printf("Hit 'p' read the CJC\n");
    printf("Hit 's' to get status\n");
    printf("Hit 't' to measure temperature\n");

    while((ch = getchar()) == '\0' ||
      ch == '\n');

    switch(tolower(ch)) {
      case 'b': /* test to see if led blinks */
        usbBlink_USBTC(hid);
        break;
      case 'c': /* calibration */
        usbCalibrate_USBTC(hid);
        break;
      case 'f':  /* Get status of thermocouple burnout detection */
	 printf("Burnout status = %#x\n", usbGetBurnoutStatus_USBTC(hid, 0xf));
	 break;
      case 'g':
	hid_get_serial_number_string(hid, serial, 64);
        printf("Serial Number = %ls\n", serial);
        break;
      case 'i':
        // Read the Manufacuter String
        ret = hid_get_manufacturer_string(hid, wstr, MAX_STR);
        printf("Manufacturer String: %ls\n", wstr);
        // Read the Product String
        ret = hid_get_product_string(hid, wstr, MAX_STR);
        printf("Product String: %ls\n", wstr);
        // Read the Serial Number String
        ret = hid_get_serial_number_string(hid, wstr, MAX_STR);
        printf("Serial Number String: %ls\n", wstr);
        break;            
      case 'p':  /* read the CJC */
        usbTin_USBTC(hid, CJC0, 0, &temperature);
	printf("CJC 0 = %.2f degress Celsius or %.2f degrees Fahrenheit.\n", temperature,
	        celsius2fahr(temperature));
        usbTin_USBTC(hid, CJC1, 0, &temperature);
	printf("CJC 1 = %.2f degress Celsius or %.2f degrees Fahrenheit.\n", temperature,
	        celsius2fahr(temperature));
        break;
      case 'd': /* test to see if led blinks */
        printf("conect DIO0 - DIO4\n");
	printf("conect DIO1 - DIO5\n");
	printf("conect DIO2 - DIO6\n");
	printf("conect DIO3 - DIO7\n");
	usbDConfigBit_USBTC(hid, 0,  DIO_DIR_OUT);
	usbDConfigBit_USBTC(hid, 1,  DIO_DIR_OUT);
	usbDConfigBit_USBTC(hid, 2,  DIO_DIR_OUT);
	usbDConfigBit_USBTC(hid, 3,  DIO_DIR_OUT);
	usbDConfigBit_USBTC(hid, 4,  DIO_DIR_IN);
	usbDConfigBit_USBTC(hid, 5,  DIO_DIR_IN);
	usbDConfigBit_USBTC(hid, 6,  DIO_DIR_IN);
	usbDConfigBit_USBTC(hid, 7,  DIO_DIR_IN);
	do {
  	  printf("Enter value [0-f]: ");
	  scanf("%hhx", &bIReg);
	  bIReg &= 0xf;
  	  usbDOut_USBTC(hid, bIReg);
	  usbDIn_USBTC(hid, &bOReg);
	  printf("value = %#hhx\n", bOReg);
	} while (toContinue());
	break;
      case 's':
        printf("Status = %#x\n", usbGetStatus_USBTC(hid));
	break;
      case 'r':
        usbReset_USBTC(hid);
        return 0;
	break;
      case 'e':
	hid_close(hid);
        hid_exit();
        return 0;
	break;
      case 't':
        printf("Connect thermocouple to channel 0\n");
	printf(" Select Thermocouple Type [JKSRBETN]: ");
	scanf("%s", &type);
	switch(type) {
	case 'J':
	  bIReg = TYPE_J;
	  printf("Type J Thermocouple Selected: \n");
	  break;
	case 'K':
	  bIReg = TYPE_K;
  	  printf("Type K Thermocouple Selected: \n");
	  break;
	case 'T':
	  bIReg = TYPE_T;
    	  printf("Type T Thermocouple Selected: \n");
	  break;
	case 'E':
	  bIReg = TYPE_E;
    	  printf("Type E Thermocouple Selected: \n");
	  break;
	case 'R':
	  bIReg = TYPE_R;
      	  printf("Type R Thermocouple Selected: \n");
	  break;
	case 'S':
	  bIReg = TYPE_S;
       	  printf("Type S Thermocouple Selected: \n");
	  break;
	case 'B':
	  bIReg = TYPE_B;
       	  printf("Type B Thermocouple Selected: \n");
	  break;
	case 'N':
	  bIReg = TYPE_N;
       	  printf("Type N Thermocouple Selected: \n");
	  break;
        default:
	  printf("Unknown or unsupported thermocopule type.\n");
	  break;
	}
        usbSetItem_USBTC(hid, ADC_0, CH_0_TC, bIReg);
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
	do {
          usbTin_USBTC(hid, CH0, 0, &temperature);
  	  printf("%.2f degress Celsius or %.2f degrees Fahrenheit.\n", temperature,
		 celsius2fahr(temperature));
  	  sleep(1);
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
	break;
      default:
        break;
    }
  }
}
Example #5
0
int main (int argc, char **argv) {
  
  int fd[4];
  int flag;
  signed short svalue;
  __u8 input, channel, gain, options;
  __u16 value;
  __u16 out_data[1024];
  signed short in_data[1024];
  int count;
  int temp, i, j;
  int ch;
  int nInterfaces = 0;
  float freq;
  time_t startTime, endTime;

  nInterfaces = PMD_Find(MCC_VID, USB1408FS_PID, fd);

  if ( nInterfaces <= 0 ) {
    fprintf(stderr, "USB 1408FS not found.\n");
    exit(1);
  } else
    printf("USB 1408FS Device is found! Number of Interfaces = %d\n",
	   nInterfaces);

  /* config mask 0x01 means all inputs */
  usbDConfigPort_USB1408FS(fd[0], DIO_PORTA, DIO_DIR_OUT);
  usbDConfigPort_USB1408FS(fd[0], DIO_PORTB, DIO_DIR_IN);
  usbDOut_USB1408FS(fd[0], DIO_PORTA, 0);
  usbDOut_USB1408FS(fd[0], DIO_PORTA, 0);

  while(1) {
    printf("\nUSB 1408FS Testing\n");
    printf("----------------\n");
    printf("Hit 'a' to test analog output scan\n");    
    printf("Hit 'b' to blink LED\n");
    printf("Hit 'c' to test counter\n");
    printf("Hit 'e' to exit\n");
    printf("Hit 'd' to test digital I/O\n");
    printf("Hit 'g' to test analog input scan (differential) \n");
    printf("Hit 'j' to test analog input scan (single ended).\n");
    printf("Hit 'i' to test analog input (differential)\n");
    printf("Hit 'h' to test analog input (single ended)\n");
    printf("Hit 'o' to test analog output\n");
    printf("Hit 'r' to reset\n");
    printf("Hit 's' to get status\n");

    while((ch = getchar()) == '\0' || ch == '\n');

    switch(tolower(ch)) {
      case 'b': /* test to see if led blinks */
        usbBlink_USB1408FS(fd[0]);
        break;
      case 'c':
        printf("connect pin 20 and 21\n");
        usbInitCounter_USB1408FS(fd[0]);
        sleep(1);
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
        do {
          usbDOut_USB1408FS(fd[0], DIO_PORTA, 1);
	  sleep(1);
          usbDOut_USB1408FS(fd[0], DIO_PORTA, 0);
	  printf("Counter = %d\n",usbReadCounter_USB1408FS(fd[0]));
        } while (!isalpha(getchar()));
        fcntl(fileno(stdin), F_SETFL, flag);
        break;
      case 'd':
        printf("\nTesting Digital I/O....\n");
        printf("connect pins 21 through 28 <=> 32 through 39\n");
        do {
          printf("Enter a byte number [0-0xff]: " );
          scanf("%x", &temp);
          usbDOut_USB1408FS(fd[0], DIO_PORTA, (__u8)temp);
          usbDIn_USB1408FS(fd[0], DIO_PORTB, &input);
          printf("The number you entered = %#x\n",input);
        } while (toContinue());
        break;
      case 'o': /* test the analog output */
        printf("Testing the analog output...\n");
        printf("Enter channel [0-1] => (pin 13-14):");
        scanf("%d", &temp);
        channel = (__u8) temp;
        for ( i = 0; i < 3; i++ ) {
          for ( value = 0; value < 0xfff; value++ ) {
	    usbAOut_USB1408FS(fd[0], channel, value);
	  }
        }
        break;
      case 'a': /* test Analog Output Scan */
        printf("Enter desired frequency [Hz]: ");
        scanf("%f", &freq);
        for ( j = 0; j <  freq; j++ ) {
	  for (i = 0; i < 1024; i++) {
	    out_data[i] = i%2 ? 0 : 0xfff;
	  }
	  usbAOutScan_USB1408FS(fd, 0, 0, 1024, &freq, out_data);
	}
	break;
      case 'g':
        printf("Enter desired frequency [Hz]: ");
        scanf("%f", &freq);
        printf("Enter number of samples [1-1024]: ");
        scanf("%d", &count);
        printf("\t\t1. +/- 20.V\n");
        printf("\t\t2. +/- 10.V\n");
        printf("\t\t3. +/- 5.V\n");
        printf("\t\t4. +/- 4.V\n");
        printf("\t\t5. +/- 2.5V\n");
        printf("\t\t6. +/- 2.0V\n");
        printf("\t\t7. +/- 1.25V\n");
        printf("\t\t8. +/- 1.0V\n");
        printf("Select gain: [1-8]\n");
        scanf("%d", &temp);
        switch(temp) {
          case 1: gain = BP_20_00V;
            break;
          case 2: gain = BP_10_00V;
            break;
          case 3: gain = BP_5_00V;
            break;
          case 4: gain = BP_4_00V;
            break;
          case 5: gain = BP_2_50V;
            break;
          case 6: gain = BP_2_00V;
            break;
          case 7: gain = BP_1_25V;
            break;
          case 8: gain = BP_1_00V;
            break;
          default:
            break;
	}
        svalue = usbAIn_USB1408FS(fd[0], 0, gain);
	options = AIN_EXECUTION;
        for ( i = 0; i < 1024; i++ ) {  // load data with known value
	  in_data[i] = 0xbeef;
	}
        usbAInScan_USB1408FS(fd, 0, 0, count, &freq, options, in_data);
	for ( i = 0; i < count; i++ ) {
	  printf("data[%d] = %#hx  %.2fV\n", i, in_data[i], volts_1408FS(gain, in_data[i]));
	}
	break;
      case 'h':
        printf("Testing Analog Input Single Ended Mode\n");
        printf("Select channel [0-7]: ");
        scanf("%d", &temp);
	channel = (__u8) (temp);
	gain =  SE_10_00V;
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
        do {
          sleep(1);
	  svalue = usbAIn_USB1408FS(fd[0], channel, gain);
	  printf("Channel: %d: value = %#hx, %.2fV\n",
		 channel, svalue, volts_1408FS_SE(svalue));
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
	break;
      case 'j':
        printf("Test of scan mode (single ended).\n");
        printf("Enter desired frequency [Hz]: ");
        scanf("%f", &freq);
        printf("Enter number of samples [1-1024]: ");
        scanf("%d", &count);
	options = AIN_EXECUTION | AIN_GAIN_QUEUE;
        for ( i = 0; i < 1024; i++ ) {  // load data with known value
	  in_data[i] = 0xbeef;
	}
        usbAInScan_USB1408FS_SE(fd, 0, 0, count, &freq, options, in_data);
	for ( i = 0; i < count; i++ ) {
	  printf("data[%d] = %#hx  %.2fV\n", i, in_data[i], volts_1408FS_SE(in_data[i]));
	}
	break;
      case 'i':
        printf("Connect pin 1 - pin 21  and pin 2 - pin 3\n");
        printf("Select channel [0-3]: ");
        scanf("%d", &temp);
        if ( temp < 0 || temp > 3 ) break;
        channel = (__u8) temp;
        printf("\t\t1. +/- 20.V\n");
        printf("\t\t2. +/- 10.V\n");
        printf("\t\t3. +/- 5.V\n");
        printf("\t\t4. +/- 4.V\n");
        printf("\t\t5. +/- 2.5V\n");
        printf("\t\t6. +/- 2.0V\n");
        printf("\t\t7. +/- 1.25V\n");
        printf("\t\t8. +/- 1.0V\n");
        printf("Select gain: [1-8]\n");
        scanf("%d", &temp);
        switch(temp) {
          case 1: gain = BP_20_00V;
            break;
          case 2: gain = BP_10_00V;
            break;
          case 3: gain = BP_5_00V;
            break;
          case 4: gain = BP_4_00V;
            break;
          case 5: gain = BP_2_50V;
            break;
          case 6: gain = BP_2_00V;
            break;
          case 7: gain = BP_1_25V;
            break;
          case 8: gain = BP_1_00V;
            break;
          default:
            break;
	}
        printf("Hit s <CR> to stop.\n ");
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
        do {
          usbDOut_USB1408FS(fd[0], DIO_PORTA, 0);
	  sleep(1);
	  svalue = usbAIn_USB1408FS(fd[0], channel, gain);
	  printf("Channel: %d: value = %#hx, %.2fV\n",
		 channel, svalue, volts_1408FS(gain, svalue));
          usbDOut_USB1408FS(fd[0], DIO_PORTA, 0x1);
	  sleep(1);
	  svalue = usbAIn_USB1408FS(fd[0], channel, gain);
	  printf("Channel: %d: value = %#hx, %.2fV\n",
		 channel, svalue, volts_1408FS(gain, svalue));
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
        printf("Doing a timing test.  Please wait ...\n");
        time(&startTime);
        for (count = 0; count < 500; count++) {
	  svalue = usbAIn_USB1408FS(fd[0], channel, gain);
	}
        time(&endTime);
        printf("Sampling speed is %ld Hz.\n", 500/(endTime - startTime));
	break;
      case 's':
        printf("Status = %#x\n", usbGetStatus_USB1408FS(fd[0]));
	break;
      case 'r':
        usbReset_USB1408FS(fd[0]);
        return 0;
	break;
      case 'e':
        close(fd[0]);
        close(fd[1]);
        close(fd[2]);
        close(fd[3]);
        return 0;
	break;
      default:
        break;
    }
  }
}
int main (int argc, char **argv)
{
  int flag;
  signed short svalue;
  __u8 input, channel, gain, options;
  __u8 gains[8], channels[8];
  __u16 value;
  __u16 out_data[512];
  signed short in_data[1024];
  int count;
  int temp, i, j;
  int ch;
  float freq;
  time_t startTime, endTime;
  getAllValues allValue;

  HIDInterface*  hid[4];  // Composite device with 4 interfaces.
  hid_return ret;
  int interface;
  
  // Debug information.  Delete when not needed    
  //  hid_set_debug(HID_DEBUG_ALL);
  //  hid_set_debug_stream(stderr);
  //  hid_set_usb_debug(2);

  ret = hid_init();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_init failed with return code %d\n", ret);
    return -1;
  }

  for ( i = 0; i <= 3; i++ ) {
    if ((interface = PMD_Find_Interface(&hid[i], i, USB1408FS_PID)) < 0 ) {
      fprintf(stderr, "USB 1408FS not found.\n");
      exit(1);
    } else {
      printf("USB 1408FS Device is found! Interface = %d\n", interface);
    }
  }

  /* config mask 0x01 means all inputs */
  usbDConfigPort_USB1408FS(hid[0], DIO_PORTA, DIO_DIR_OUT);
  usbDConfigPort_USB1408FS(hid[0], DIO_PORTB, DIO_DIR_IN);
  usbDOut_USB1408FS(hid[0], DIO_PORTA, 0);
  usbDOut_USB1408FS(hid[0], DIO_PORTA, 0);

  while(1) {
    printf("\nUSB 1408FS Testing\n");
    printf("----------------\n");
    printf("Hit 'a' to test analog output scan.\n");    
    printf("Hit 'b' to blink LED.\n");
    printf("Hit 'c' to test counter.\n");
    printf("Hit 'd' to test digital I/O.\n");
    printf("Hit 'e' to exit.\n");
    printf("Hit 'f' to get all values.\n");
    printf("Hit 'g' to test analog input scan (differential).\n");
    printf("Hit 'h' to test analog input (single ended).\n");
    printf("Hit 'i' to test analog input (differential mode).\n");
    printf("Hit 'j' to test analog input scan (single ended).\n");
    printf("Hit 'o' to test analog output.\n");
    printf("Hit 'r' to reset.\n");
    printf("Hit 's' to get status.\n");

    while((ch = getchar()) == '\0' || ch == '\n');

    switch(tolower(ch)) {
      case 'b': /* test to see if led blinks */
        usbBlink_USB1408FS(hid[0]);
        break;
      case 'c':
        printf("connect pin 20 and 21\n");
        usbInitCounter_USB1408FS(hid[0]);
        sleep(1);
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
        do {
          usbDOut_USB1408FS(hid[0], DIO_PORTA, 1);
	  sleep(1);
          usbDOut_USB1408FS(hid[0], DIO_PORTA, 0);
	  printf("Counter = %d\n",usbReadCounter_USB1408FS(hid[0]));
        } while (!isalpha(getchar()));
        fcntl(fileno(stdin), F_SETFL, flag);
        break;
      case 'd':
        printf("\nTesting Digital I/O....\n");
        printf("connect pins 21 through 28 <=> 32 through 39\n");
        do {
          printf("Enter a byte number [0-0xff]: " );
          scanf("%x", &temp);
          usbDOut_USB1408FS(hid[0], DIO_PORTA, (__u8)temp);
          usbDIn_USB1408FS(hid[0], DIO_PORTB, &input);
          printf("The number you entered = %#x\n",input);
        } while (toContinue());
        break;
      case 'o': /* test the analog output */
        printf("Testing the analog output...\n");
        printf("Enter channel [0-1] => (pin 13-14):");
        scanf("%d", &temp);
        channel = (__u8) temp;
        printf("Enter a value: ");
        scanf("%hx", &value);
        usbAOut_USB1408FS(hid[0], channel, value);
        break;
      case 'a': /* test Analog Output Scan */
        printf("Enter desired frequency [Hz]: ");
        scanf("%f", &freq);
        for ( j = 0; j <  2; j++ ) {
	  for (i = 0; i < 512; i++) {
	    out_data[i] = i%2 ? 0 : 0xfff;
	  }
	  usbAOutScan_USB1408FS(hid, 0, 0, 512, &freq, out_data, 1);
	}
	break;
      case 'g':
        printf("Enter desired frequency [Hz]: ");
        scanf("%f", &freq);
        printf("Enter number of samples [1-1024]: ");
        scanf("%d", &count);
        printf("\t\t1. +/- 20.V\n");
        printf("\t\t2. +/- 10.V\n");
        printf("\t\t3. +/- 5.V\n");
        printf("\t\t4. +/- 4.V\n");
        printf("\t\t5. +/- 2.5V\n");
        printf("\t\t6. +/- 2.0V\n");
        printf("\t\t7. +/- 1.25V\n");
        printf("\t\t8. +/- 1.0V\n");
        printf("Select gain: [1-8]\n");
        scanf("%d", &temp);
        switch(temp) {
          case 1: gain = BP_20_00V;
            break;
          case 2: gain = BP_10_00V;
            break;
          case 3: gain = BP_5_00V;
            break;
          case 4: gain = BP_4_00V;
            break;
          case 5: gain = BP_2_50V;
            break;
          case 6: gain = BP_2_00V;
            break;
          case 7: gain = BP_1_25V;
            break;
          case 8: gain = BP_1_00V;
            break;
          default:
            break;
	}
	for (i = 0; i < 8; i++) {
	  channels[i] = i;
	  gains[i] = gain;
	}
	usbALoadQueue_USB1408FS(hid[0], 1, channels, gains);
	options = AIN_EXECUTION | AIN_GAIN_QUEUE;
        for ( i = 0; i < 1024; i++ ) {  // load data with known value
	  in_data[i] = 0xbeef;
	}
        usbAInScan_USB1408FS(hid, 0, 0, count, &freq, options, in_data);
	for ( i = 0; i < count; i++ ) {
	  printf("data[%d] = %#hx  %.2fV\n", i, in_data[i], volts_1408FS(gain, in_data[i]));
	}
	usbAInStop_USB1408FS(hid[0]);
	break;
      case 'h':
        printf("Testing Analog Input Single Ended Mode\n");
        printf("Select channel [0-7]: ");
        scanf("%d", &temp);
	channel = (__u8) (temp);
	gain =  SE_10_00V;
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
        do {
          sleep(1);
	  svalue = usbAIn_USB1408FS(hid[0], channel, gain);
	  printf("Channel: %d: value = %#hx, %.2fV\n",
		 channel, svalue, volts_1408FS_SE(svalue));
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
	break;
      case 'i':
        printf("Connect pin 1 - pin 21  and pin 2 - pin 3\n");
        printf("Select channel [0-3]: ");
        scanf("%d", &temp);
        if ( temp < 0 || temp > 3 ) break;
        channel = (__u8) temp;
        printf("\t\t1. +/- 20.V\n");
        printf("\t\t2. +/- 10.V\n");
        printf("\t\t3. +/- 5.V\n");
        printf("\t\t4. +/- 4.V\n");
        printf("\t\t5. +/- 2.5V\n");
        printf("\t\t6. +/- 2.0V\n");
        printf("\t\t7. +/- 1.25V\n");
        printf("\t\t8. +/- 1.0V\n");
        printf("Select gain: [1-8]\n");
        scanf("%d", &temp);
        switch(temp) {
          case 1: gain = BP_20_00V;
            break;
          case 2: gain = BP_10_00V;
            break;
          case 3: gain = BP_5_00V;
            break;
          case 4: gain = BP_4_00V;
            break;
          case 5: gain = BP_2_50V;
            break;
          case 6: gain = BP_2_00V;
            break;
          case 7: gain = BP_1_25V;
            break;
          case 8: gain = BP_1_00V;
            break;
          default:
            break;
	}
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
        do {
          usbDOut_USB1408FS(hid[0], DIO_PORTA, 0);
	  sleep(1);
	  svalue = usbAIn_USB1408FS(hid[0], channel, gain);
	  printf("Channel: %d: value = %#hx, %.2fV\n",
		 channel, svalue, volts_1408FS(gain, svalue));
          usbDOut_USB1408FS(hid[0], DIO_PORTA, 0x1);
	  sleep(1);
	  svalue = usbAIn_USB1408FS(hid[0], channel, gain);
	  printf("Channel: %d: value = %#hx, %.2fV\n",
		 channel, svalue, volts_1408FS(gain, svalue));
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
        printf("Doing a timing test.  Please wait ...\n");
        time(&startTime);
        for (count = 0; count < 500; count++) {
	  svalue = usbAIn_USB1408FS(hid[0], channel, gain);
	}
        time(&endTime);
        printf("Sampling speed is %ld Hz.\n", 500/(endTime - startTime));
	break;
      case 'j':
        printf("Test of scan mode (single ended).\n");
        printf("Enter desired frequency [Hz]: ");
        scanf("%f", &freq);
        printf("Enter number of samples [1-1024]: ");
        scanf("%d", &count);
	options = AIN_EXECUTION | AIN_GAIN_QUEUE;
        for ( i = 0; i < 1024; i++ ) {  // load data with known value
	  in_data[i] = 0xbeef;
	}
        usbAInScan_USB1408FS_SE(hid, 0, 0, count, &freq, options, in_data);
	for ( i = 0; i < count; i++ ) {
	  printf("data[%d] = %#hx  %.2fV\n", i, in_data[i], volts_1408FS_SE(in_data[i]));
	}
	break;
      case 'f':
        usbGetAll_USB1408FS(hid[0], &allValue);
	for (i = 0; i < 4; i++) {
	  printf("Differential Reference Low channel[%d] = %hd \n", i, allValue.ref_Low[i]);
	}
	for (i = 0; i < 4; i++) {
	  printf("Differential Reference High channel[%d] = %hd \n", i, allValue.ref_High[i]);
	}
	for (i = 0; i < 8; i++) {
	  printf("Single Ended Input channel[%d] = %hd \n", i, allValue.se[i]);
	}
	printf("DIO Port A = %#x\n", allValue.dio_portA);
	printf("DIO Port B = %#x\n", allValue.dio_portB);
	printf("\n\n\n");
	break;
      case 's':
        printf("Status = %#x\n", usbGetStatus_USB1408FS(hid[0]));
	break;
      case 'r':
        usbReset_USB1408FS(hid[0]);
        return 0;
	break;
      case 'e':
        for ( i = 0; i <= 3; i++ ) {
	  ret = hid_close(hid[i]);
	  if (ret != HID_RET_SUCCESS) {
	    fprintf(stderr, "hid_close failed with return code %d\n", ret);
	    return 1;
	  }
	  hid_delete_HIDInterface(&hid[i]);
	}

	ret = hid_cleanup();
	if (ret != HID_RET_SUCCESS) {
	  fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
	  return 1;
	}
        return 0;
	break;
      default:
        break;
    }
  }
}
int main (int argc, char **argv)
{
  libusb_device_handle *udev = NULL;
  struct tm calDate;
  
  float table_DE_AIN[NGAINS_USB1208FS_PLUS][NCHAN_DE][2];
  float table_SE_AIN[NCHAN_SE][2];

  int ch;
  int i, j, k, m;
  int flag;
  int device;
  uint8_t input;
  int temp;
  uint8_t options;
  char serial[9];
  uint8_t channel, channels;
  uint8_t range;
  uint8_t ranges[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  uint16_t value;
  uint32_t count;
  double frequency, voltage;
  int ret;
  uint16_t dataAIn[8*512];  // holds 16 bit unsigned analog input data
  uint16_t dataAOut[128];   // holds 12 bit unsigned analog input data
  uint16_t data;
  int nchan, repeats;
  int transferred;

  udev = NULL;

  ret = libusb_init(NULL);
  if (ret < 0) {
    perror("usb_device_find_USB_MCC: Failed to initialize libusb");
    exit(1);
  }

  if ((udev = usb_device_find_USB_MCC(USB1208FS_PLUS_PID, NULL))) {
    printf("Success, found a USB 1208FS-Plus!\n");
    device = USB1208FS_PLUS_PID;
  } else if ((udev = usb_device_find_USB_MCC(USB1408FS_PLUS_PID, NULL))) {
    printf("Success, found a USB 1408FS-Plus!\n");
    device = USB1408FS_PLUS_PID;
  } else {
    printf("Failure, did not find a USB 1208FS-Plus/ USB 1408FS-Plus!\n");
    return 0;
  }

  // some initialization
  //print out the wMaxPacketSize.  Should be 64
  printf("wMaxPacketSize = %d\n", usb_get_max_packet_size(udev,0));

  usbBuildGainTable_DE_USB1208FS_Plus(udev, table_DE_AIN);
  usbBuildGainTable_SE_USB1208FS_Plus(udev, table_SE_AIN);
  for (i = 0; i < NGAINS_USB1208FS_PLUS; i++ ) {
    for (j = 0; j < NCHAN_DE; j++) {
      printf("Calibration Table: Range = %d Channel = %d Slope = %f   Offset = %f\n", 
	     i, j, table_DE_AIN[i][j][0], table_DE_AIN[i][j][1]);
    }
  }
  for (i = 0; i < NCHAN_SE; i++ ) {
    printf("Calibration Single Ended Table: Channel = %d Slope = %f   Offset = %f\n", 
	   i, table_SE_AIN[i][0], table_SE_AIN[i][1]);
  }

  usbCalDate_USB1208FS_Plus(udev, &calDate);
  printf("\n");
  printf("MFG Calibration date = %s\n", asctime(&calDate));
  
  while(1) {
    printf("\nUSB 1208FS-Plus/USB 1408FS-Plus Testing\n");
    printf("----------------\n");
    printf("Hit 'b' to blink\n");
    printf("Hit 'c' to test counter\n");
    printf("Hit 'd' to test digitial IO\n");
    printf("Hit 'i' to test Analog Input\n");
    printf("Hit 'I' to test Analog Input Scan\n");
    printf("Hit 'o' to test Analog Output\n");
    printf("Hit 'O' to test Analog Scan Output\n");
    printf("Hit 'x' to test Analog Input Scan (Multi-channel)\n");
    printf("Hit 'r' to reset the device\n");
    printf("Hit 's' to get serial number\n");
    printf("Hit 'S' to get Status\n");
    printf("Hit 'e' to exit\n");

    while((ch = getchar()) == '\0' || ch == '\n');
    switch(ch) {
      case 'b': /* test to see if LED blinks */
        printf("Enter number or times to blink: ");
        scanf("%hhd", &options);
        usbBlink_USB1208FS_Plus(udev, options);
	break;
      case 'c':
        usbCounterInit_USB1208FS_Plus(udev);
        printf("Connect DIO Port A0 to CTR0\n");
	usbDTristateW_USB1208FS_Plus(udev, PORTA, 0x0);
	usbDLatchW_USB1208FS_Plus(udev, PORTA, 0x0);  // put pin 0 into known state
        toContinue();
        for (i = 0; i < 100; i++) {                   // toggle
	  usbDLatchW_USB1208FS_Plus(udev, PORTA, 0x1);
	  usbDLatchW_USB1208FS_Plus(udev, PORTA, 0x0);
        }
        printf("Count = %d.  Should read 100.\n", usbCounter_USB1208FS_Plus(udev));
        break;      
      case 'd':
        printf("\nTesting Digital I/O...\n");
	printf("connect PORTA <--> PORTB\n");
	usbDTristateW_USB1208FS_Plus(udev, PORTA, 0x0);
	printf("Digital Port Tristate Register = %#x\n", usbDTristateR_USB1208FS_Plus(udev, PORTA));
	do {
          printf("Enter a  number [0-0xff] : " );
          scanf("%x", &temp);
          usbDLatchW_USB1208FS_Plus(udev, PORTA, (uint8_t)temp);
	  temp = usbDLatchR_USB1208FS_Plus(udev, PORTA);
          input = usbDPort_USB1208FS_Plus(udev, PORTB);
          printf("The number you entered = %#x   Latched value = %#x\n\n",input, temp);
        } while (toContinue());
        break;
      case 'i':
	printf("Input channel [0-7]: ");
	scanf("%hhd", &channel);
        printf("Input range [0-7]: ");
	scanf("%hhd", &range);
	for (i = 0; i < 20; i++) {
	  value = usbAIn_USB1208FS_Plus(udev, channel, DIFFERENTIAL, range);
	  value = rint(value*table_DE_AIN[range][channel][0] + table_DE_AIN[range][channel][1]);
          if (device == USB1208FS_PLUS_PID) {
  	    printf("Range %d  Channel %d   Sample[%d] = %#x Volts = %lf\n",
		   range, channel,  i, value, volts_USB1208FS_Plus(value, range));
	  } else {
  	    printf("Range %d  Channel %d   Sample[%d] = %#x Volts = %lf\n",
		   range, channel,  i, value, volts_USB1408FS_Plus(value, range));

	  }
	  usleep(50000);	  
	}
        break;
      case 'I':
	printf("Testing USB-1208FS_Plus Analog Input Scan.\n");
	usbAInScanStop_USB1208FS_Plus(udev);
        printf("Enter number of scans (less than 512): ");
        scanf("%d", &count);
	printf("Input channel 0-7: ");
        scanf("%hhd", &channel);
        printf("Enter sampling frequency [Hz]: ");
	scanf("%lf", &frequency);
        printf("Enter Range [0-7]: ");
        scanf("%hhd", &range);
        ranges[channel] = range;
	if (frequency > 100.) {
	  options = DIFFERENTIAL_MODE;
	} else {
	  options = DIFFERENTIAL_MODE | IMMEDIATE_TRANSFER_MODE;
	}
	usbAInScanStop_USB1208FS_Plus(udev);
	usbAInScanClearFIFO_USB1208FS_Plus(udev);
        usbAInScanConfig_USB1208FS_Plus(udev, ranges);
	memset(dataAIn, 0x0, sizeof(dataAIn));
	sleep(1);
        usbAInScanConfigR_USB1208FS_Plus(udev, ranges);
        for (i = 0; i < 4; i++) {
          printf("Channel %d     range %d\n", i, ranges[i]);
	}
	usbAInScanStart_USB1208FS_Plus(udev, count, 0x0, frequency, (0x1<<channel), options);
	ret = usbAInScanRead_USB1208FS_Plus(udev, count, 1, dataAIn, options);
	printf("Number samples read = %d\n", ret/2);
	for (i = 0; i < count; i++) {
	  dataAIn[i] = rint(dataAIn[i]*table_DE_AIN[range][channel][0] + table_DE_AIN[range][channel][1]);
          if (device == USB1208FS_PLUS_PID) {
	    printf("Range %d Channel %d  Sample[%d] = %#x Volts = %lf\n", range, channel,
		   i, dataAIn[i], volts_USB1208FS_Plus(dataAIn[i], range));
	  } else {
	    printf("Range %d Channel %d  Sample[%d] = %#x Volts = %lf\n", range, channel,
		   i, dataAIn[i], volts_USB1408FS_Plus(dataAIn[i], range));
	  }
	}
        break;
      case 'o':
        printf("Test Analog Output\n");
        printf("Enter Channel [0-1] ");
        scanf("%hhd", &channel);
        printf("Enter voltage: ");
	scanf("%lf", &voltage);
        value = voltage * 4096 / 5;
	usbAOut_USB1208FS_Plus(udev, channel, value);
	value = usbAOutR_USB1208FS_Plus(udev, channel);
	printf("Analog Output Voltage = %f V\n", volts_USB1208FS_Plus(value, UP_5V));
        break;
      case 'O':
        printf("Test of Analog Output Scan. \n");
        printf("Hook scope up to VDAC 0\n");
        printf("Enter desired frequency of sine wave [Hz]: ");
        scanf("%lf", &frequency);
	frequency *= 2.;

        for (i = 0; i < 32; i++) {
          dataAOut[4*i] =   0x0;
	  dataAOut[4*i+1] = 0x800;
	  dataAOut[4*i+2] = 0xfff;
	  dataAOut[4*i+3] = 0xcff;
	}
	usbAOutScanStop_USB1208FS_Plus(udev);
        options = 0x3;   // output channel 0 and 1 output scan
	usbAOutScanStart_USB1208FS_Plus(udev, 0, frequency, options);
	printf("Hit \'s <CR>\' to stop ");
	flag = fcntl(fileno(stdin), F_GETFL);
	fcntl(0, F_SETFL, flag | O_NONBLOCK);
	do {
	  if (libusb_bulk_transfer(udev, LIBUSB_ENDPOINT_OUT|2, (unsigned char *) dataAOut, sizeof(dataAOut), &transferred, 1000) < 0) {
	    perror("usb_bulk_transfer error in AOutScan.");
	  }
	  //	  usb_bulk_write(udev, USB_ENDPOINT_OUT|2, (char *) dataAOut, 0x0, 400);
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
	usbAOutScanStop_USB1208FS_Plus(udev);
	break;
      case 'x':
        printf("Testing USB-1208FS_Plus Mult-Channel Analog Input Scan.\n");
        usbAInScanStop_USB1208FS_Plus(udev);
        printf("Enter number of channels (1-8) :");
        scanf("%d", &nchan);
        printf("Enter number of scans (less than 512): ");
        scanf("%d", &count);
        printf("Enter number of repeats: ");
        scanf("%d", &repeats);
        // Build bitmap for the first nchan in channels.
        channels = 0;
        for (i = 0; i < nchan; i++)
	  channels |= (1 << i);
        printf ("channels: %02X   count:%d\n", channels, count);
        frequency = 10000.;
        // Always use BP_20V to make it easy (BP_20V is 0...)
        range = 0;
        memset(ranges, 0, sizeof(ranges));
        usbAInScanConfig_USB1208FS_Plus(udev, ranges);
        // Run a loop for the specified number of repeats and
        // show the results...
        for (m = 0; m < repeats; m++) {
	  printf("\n\n---------------------------------------");
	  printf("\nrepeat: %d\n", m);
	  usbAInScanStart_USB1208FS_Plus(udev, count, 0x0, frequency, channels, 0);
	  ret = usbAInScanRead_USB1208FS_Plus(udev, count, nchan, dataAIn, 0);
	  printf("Number samples read = %d\n", ret/2);
	  if (ret != count * nchan * 2) {
	    printf("***ERROR***  ret = %d   count = %d  nchan = %d\n", ret, count, nchan);
	    continue;
	  } /* if (ret != count * nchan * 2) */
	  for (i = 0; i < count; i++) {
	    printf("%6d", i);
	    for (j = 0; j < nchan; j++)	{
	      k = i*nchan + j;
	      data = rint(dataAIn[k]*table_DE_AIN[range][j][0] + table_DE_AIN[range][j][1]);
	      printf(", %8.4f", volts_USB1208FS_Plus(data, range));
	    } /* for (j - 0; j < 8, j++) */
	    printf("\n");
	  } /* for (i = 0; i < count; i++) */
	} /* for (m = 0; m < repeats; m++) */
	printf("\n\n---------------------------------------");
	break;
      case 'r':
        usbReset_USB1208FS_Plus(udev);
        break;
      case 's':
        usbGetSerialNumber_USB1208FS_Plus(udev, serial);
        printf("Serial number = %s\n", serial);
        break;
      case 'S':
        printf("Status = %#x\n", usbStatus_USB1208FS_Plus(udev));
	break;
      case 'e':
        cleanup_USB1208FS_Plus(udev);
        return 0;
      default:
        break;
    }
  }
}
Example #8
0
int main (int argc, char **argv) {

  int fd[1];
  int ch;
  int numInterfaces = 0;
  __u8 bIReg, bOReg;
  float temperature;
  float temperature_array[8];
  int nchan;
  int flag;
  char type[80];
  int i;
  char serial[9];

  numInterfaces = PMD_Find(MCC_VID, USBTC_PID, fd);

  if ( numInterfaces <= 0 ) {
    fprintf(stderr, "USB TC not found.\n");
    exit(1);
  } else
    printf("USB TC Device is found! Number of interfaces = %d\n",
	   numInterfaces);

  /* config mask 0x01 means all inputs */
  usbDConfigPort_USBTC(fd[0], DIO_DIR_OUT);
  usbDOut_USBTC(fd[0], 0);

  while(1) {
    printf("\nUSB TC Testing\n");
    printf("----------------\n");
    printf("Hit 'b' to blink\n");
    printf("Hit 'c' to calibrate\n");
    printf("Hit 'd' to test DIO\n");
    printf("Hit 'e' to exit\n");
    printf("Hit 'f' for burnout status\n");
    printf("Hit 'g' to get serial number\n");
    printf("Hit 'r' to reset\n");
    printf("Hit 'p' read the CJC\n");
    printf("Hit 's' to get status\n");
    printf("Hit 't' to measure temperature\n");
    printf("Hit 'x' to measure temperature (Thermocouple) multiple channels\n");    

    while((ch = getchar()) == '\0' ||
      ch == '\n');

    switch(ch) {
      case 'b': /* test to see if led blinks */
        usbBlink_USBTC(fd[0]);
        break;
      case 'c': /* calibration */
        usbCalibrate_USBTC(fd[0]);
        break;
      case 'f':  /* Get status of thermocouple burnout detection */
	 printf("Burnout status = %#x\n", usbGetBurnoutStatus_USBTC(fd[0], 0xf));
	 break;
      case 'g':
        strncpy(serial, PMD_GetSerialNumber(fd[0]), 9);
        printf("Serial Number = %s\n", serial);
	break;
      case 'p':  /* read the CJC */
        usbTin_USBTC(fd[0], CJC0, 0, &temperature);
	printf("CJC 0 = %.2f degress Celsius or %.2f degrees Fahrenheit.\n", temperature,
	        celsius2fahr(temperature));
        usbTin_USBTC(fd[0], CJC1, 0, &temperature);
	printf("CJC 1 = %.2f degress Celsius or %.2f degrees Fahrenheit.\n", temperature,
	        celsius2fahr(temperature));
        break;
      case 'd': /* test to see if led blinks */
        printf("conect DIO0 - DIO4\n");
	printf("conect DIO1 - DIO5\n");
	printf("conect DIO2 - DIO6\n");
	printf("conect DIO3 - DIO7\n");
	usbDConfigBit_USBTC(fd[0], 0,  DIO_DIR_OUT);
	usbDConfigBit_USBTC(fd[0], 1,  DIO_DIR_OUT);
	usbDConfigBit_USBTC(fd[0], 2,  DIO_DIR_OUT);
	usbDConfigBit_USBTC(fd[0], 3,  DIO_DIR_OUT);
	usbDConfigBit_USBTC(fd[0], 4,  DIO_DIR_IN);
	usbDConfigBit_USBTC(fd[0], 5,  DIO_DIR_IN);
	usbDConfigBit_USBTC(fd[0], 6,  DIO_DIR_IN);
	usbDConfigBit_USBTC(fd[0], 7,  DIO_DIR_IN);
	do {
  	  printf("Enter value [0-f]: ");
	  scanf("%hhx", &bIReg);
	  bIReg &= 0xf;
  	  usbDOut_USBTC(fd[0], bIReg);
	  usbDIn_USBTC(fd[0], &bOReg);
	  printf("value = %#x\n", bOReg);
	} while (toContinue());
	break;
      case 's':
        printf("Status = %#x\n", usbGetStatus_USBTC(fd[0]));
	break;
      case 'r':
        usbReset_USBTC(fd[0]);
        return 0;
	break;
      case 'e':
        close(fd[0]);
        return 0;
	break;
      case 't':
	printf("Select Channel [0-7]: ");
        scanf("%d", &i);
        printf("Connect thermocouple to channel 0\n");
	printf(" Select Thermocouple Type [JKSRBETN]: ");
	scanf("%s", type);
	switch(type[0]) {
	case 'J':
	  bIReg = TYPE_J;
	  printf("Type J Thermocouple Selected: \n");
	  break;
	case 'K':
	  bIReg = TYPE_K;
  	  printf("Type K Thermocouple Selected: \n");
	  break;
	case 'T':
	  bIReg = TYPE_T;
    	  printf("Type T Thermocouple Selected: \n");
	  break;
	case 'E':
	  bIReg = TYPE_E;
    	  printf("Type E Thermocouple Selected: \n");
	  break;
	case 'R':
	  bIReg = TYPE_R;
      	  printf("Type R Thermocouple Selected: \n");
	  break;
	case 'S':
	  bIReg = TYPE_S;
       	  printf("Type S Thermocouple Selected: \n");
	  break;
	case 'B':
	  bIReg = TYPE_B;
       	  printf("Type B Thermocouple Selected: \n");
	  break;
	case 'N':
	  bIReg = TYPE_N;
       	  printf("Type N Thermocouple Selected: \n");
	  break;
        default:
	  printf("Unknown or unsupported thermocopule type.\n");
	  break;
	}
        usbSetItem_USBTC(fd[0], i/2, i%2+CH_0_TC, bIReg);
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
	do {
          usbTin_USBTC(fd[0], i, 0, &temperature);
    	  printf("Channel: %d  %.2f degress Celsius or %.2f degrees Fahrenheit.\n",
		 i, temperature, celsius2fahr(temperature));
  	  sleep(1);
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
	break;
      case 'x':
	printf("Enter number of Channels (1-8): ");
	scanf("%d", &nchan);
	for ( i = 0; i < nchan; i++ ) {
          printf("Connect thermocouple to channel %d\n", i);
	  printf(" Select Thermocouple Type [JKSRBETN]: ");
	  scanf("%s", type);
	  switch(type[0]) {
	  case 'J':
	    bIReg = TYPE_J;
	    printf("Type J Thermocouple Selected: \n");
	    break;
	  case 'K':
	    bIReg = TYPE_K;
  	    printf("Type K Thermocouple Selected: \n");
	    break;
	  case 'T':
	    bIReg = TYPE_T;
    	    printf("Type T Thermocouple Selected: \n");
	    break;
	  case 'E':
	    bIReg = TYPE_E;
    	    printf("Type E Thermocouple Selected: \n");
	    break;
	  case 'R':
	    bIReg = TYPE_R;
      	    printf("Type R Thermocouple Selected: \n");
	    break;
	  case 'S':
	    bIReg = TYPE_S;
       	    printf("Type S Thermocouple Selected: \n");
	    break;
	  case 'B':
	    bIReg = TYPE_B;
       	    printf("Type B Thermocouple Selected: \n");
	    break;
	  case 'N':
	    bIReg = TYPE_N;
       	    printf("Type N Thermocouple Selected: \n");
	    break;
          default:
	    printf("Unknown or unsupported thermocopule type.\n");
	    break;
	  }
          usbSetItem_USBTC(fd[0], i/2, i%2+CH_0_TC, bIReg);
	}
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
	do {
          usbTinScan_USBTC(fd[0], CH0, nchan-1, 0, temperature_array);
	  for ( i = 0; i < nchan; i++ ) {
  	    printf("Channel %d:  %.2f degress Celsius or %.2f degrees Fahrenheit.\n",
		   i, temperature_array[i], celsius2fahr(temperature_array[i]));
	  }
	  printf("\n");
	  sleep(1);
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
	break;
      default:
        break;
    }
  }
}
int main(int argc, char **argv) {

  int ch;
  int i;
  int nchan;
  __u8 bIReg, bOReg;
  __u8 in_options, out_options;
  float temperature;
  float volts;
  float temperature_array[8];
  float value_1, value_2;
  int flag;
  char type[10];

  HIDInterface*  hid = 0x0;  // Composite device with 1 interface.
  hid_return ret;
  int interface;
  char serial[9];
  
  // Debug information.  Delete when not needed    
  //  hid_set_debug(HID_DEBUG_ALL);
  //  hid_set_debug_stream(stderr);
  //  hid_set_usb_debug(2);

  ret = hid_init();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_init failed with return code %d\n", ret);
    return -1;
  }

  if ((interface = PMD_Find_Interface(&hid, 0, USBTC_AI_PID)) < 0) {
      fprintf(stderr, "USB TC-AI not found.\n");
      exit(1);
  } else {
      printf("USB TC-AI Device is found! Interface = %d\n", interface);
  }

  /* config mask 0x01 means all inputs */
  usbDConfigPort_USBTC_AI(hid, DIO_DIR_OUT);
  usbDOut_USBTC_AI(hid, 0x0);

  while(1) {
    printf("\nUSB TC-AI Testing\n");
    printf("----------------\n");
    printf("Hit 'a' to get alarm status\n");
    printf("Hit 'b' to blink LED\n");
    printf("Hit 'c' to calibrate\n");
    printf("Hit 'd' to test DIO\n");
    printf("Hit 'e' to exit\n");
    printf("Hit 'f' for burnout status\n");
    printf("Hit 'g' to get serial number\n");
    printf("Hit 'h' to run the counter\n");
    printf("Hit 'r' to reset\n");
    printf("Hit 'p' read the CJC\n");
    printf("Hit 's' to get status\n");
    printf("Hit 't' to measure temperature\n");
    printf("Hit 'x' to measure temperature (Thermocouple) multiple channels\n");
    printf("Hit 'v' to measure voltage (channels 4-7)\n");

    while((ch = getchar()) == '\0' ||
      ch == '\n');

    switch(tolower(ch)) {
      case 'a':
        for ( i = 0; i < 8; i++ ) {
	  value_1 = 1.0 + i;
	  value_2 = 0.0;
	  in_options = 0x40 | i;
	  out_options = 0x0;  // disable alarm
          usleep(10000);
          usbConfigAlarm_USBTC_AI(hid, i, in_options, out_options, value_1, value_2);
          usleep(10000);
    	  usbGetAlarmConfig_USBTC_AI(hid, i, &in_options, &out_options, &value_1, &value_2);
	  printf("Alarm %d: input options = %#x  output options = %#x  value_1 = %f  value_2 = %f\n",
		 i, in_options, out_options, value_1, value_2);
	}
        printf("\n");
	break;
      case 'b': /* test to see if led blinks */
        usbBlink_USBTC_AI(hid);
        break;
      case 'c': /* calibration */
        usbCalibrate_USBTC_AI(hid, 0);
        usbCalibrate_USBTC_AI(hid, 1);
        break;
      case 'f':  /* Get status of thermocouple burnout detection */
	 printf("Burnout status = %#x\n", usbGetBurnoutStatus_USBTC_AI(hid, 0xf));
	 break;
      case 'g':
        strncpy(serial, PMD_GetSerialNumber(hid), 9);
        printf("Serial Number = %s\n", serial);
        break;
      case 'p':  /* read the CJC */
        usbAin_USBTC_AI(hid, CJC0, 0, &temperature);
	printf("CJC 0 = %.2f degress Celsius or %.2f degrees Fahrenheit.\n", temperature,
	        celsius2fahr(temperature));
        break;
      case 'd': /* test to see if led blinks */
        printf("conect DIO0 - DIO4\n");
	printf("conect DIO1 - DIO5\n");
	printf("conect DIO2 - DIO6\n");
	printf("conect DIO3 - DIO7\n");
	usbDConfigBit_USBTC_AI(hid, 0,  DIO_DIR_OUT);
	usbDConfigBit_USBTC_AI(hid, 1,  DIO_DIR_OUT);
	usbDConfigBit_USBTC_AI(hid, 2,  DIO_DIR_OUT);
	usbDConfigBit_USBTC_AI(hid, 3,  DIO_DIR_OUT);
	usbDConfigBit_USBTC_AI(hid, 4,  DIO_DIR_IN);
	usbDConfigBit_USBTC_AI(hid, 5,  DIO_DIR_IN);
	usbDConfigBit_USBTC_AI(hid, 6,  DIO_DIR_IN);
	usbDConfigBit_USBTC_AI(hid, 7,  DIO_DIR_IN);
	do {
  	  printf("Enter value [0-f]: ");
	  scanf("%hhx", &bIReg);
	  bIReg &= 0xf;
  	  usbDOut_USBTC_AI(hid, bIReg);
	  usbDIn_USBTC_AI(hid, &bOReg);
	  printf("value = %#hhx\n", bOReg);
	} while (toContinue());
	break;
      case 'h':
	printf("connect CTR to DIO0.\n");
	usbDConfigBit_USBTC_AI(hid, 0,  DIO_DIR_OUT);
	usbInitCounter_USBTC_AI(hid);
        sleep(1);
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
        do {
          usbDOutBit_USBTC_AI(hid, 0, 1);
	  usleep(200000);
          usbDOutBit_USBTC_AI(hid, 0, 0);
	  printf("Counter = %d\n",usbReadCounter_USBTC_AI(hid));
        } while (!isalpha(getchar()));
        fcntl(fileno(stdin), F_SETFL, flag);
	break;
      case 's':
        printf("Status = %#x\n", usbGetStatus_USBTC_AI(hid));
	break;
      case 'r':
        usbReset_USBTC_AI(hid);
        return 0;
	break;
      case 'e':
	ret = hid_close(hid);
        if (ret != HID_RET_SUCCESS) {
	  fprintf(stderr, "hid_close failed with return code %d\n", ret);
	  return 1;
        }
        hid_delete_HIDInterface(&hid);
        ret = hid_cleanup();
        if (ret != HID_RET_SUCCESS) {
	  fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
	  return 1;
        }
        return 0;
	break;
      case 't':
        printf("Select Channel [0-3]: ");
	scanf("%d", &i);
        if ( i > 3 ) {
          printf("Channel must be between 0-3.\n");
          i = 0;
	}
        printf("Connect thermocouple to channel %d\n", i);
	printf(" Select Thermocouple Type [JKSRBETN]: ");
	scanf("%s", type);
	switch(type[0]) {
	case 'J':
	  bIReg = TYPE_J;
	  printf("Type J Thermocouple Selected: \n");
	  break;
	case 'K':
	  bIReg = TYPE_K;
  	  printf("Type K Thermocouple Selected: \n");
	  break;
	case 'T':
	  bIReg = TYPE_T;
    	  printf("Type T Thermocouple Selected: \n");
	  break;
	case 'E':
	  bIReg = TYPE_E;
    	  printf("Type E Thermocouple Selected: \n");
	  break;
	case 'R':
	  bIReg = TYPE_R;
      	  printf("Type R Thermocouple Selected: \n");
	  break;
	case 'S':
	  bIReg = TYPE_S;
       	  printf("Type S Thermocouple Selected: \n");
	  break;
	case 'B':
	  bIReg = TYPE_B;
       	  printf("Type B Thermocouple Selected: \n");
	  break;
	case 'N':
	  bIReg = TYPE_N;
       	  printf("Type N Thermocouple Selected: \n");
	  break;
        default:
	  printf("Unknown or unsupported thermocopule type.\n");
	  break;
	}
        usbSetItem_USBTC_AI(hid, i, i%2+CH_0_TC, bIReg);
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
	do {
          usbAin_USBTC_AI(hid, i, 0, &temperature);
    	  printf("Channel: %d  %.2f degress Celsius or %.2f degrees Fahrenheit.\n",
		 i, temperature, celsius2fahr(temperature));
  	  sleep(1);
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
	break;
      case 'x':
	printf("Enter number of Channels (1-4): ");
	scanf("%d", &nchan);
        if (nchan > 4) nchan = 4;
	for ( i = 0; i < nchan; i++ ) {
          printf("Connect thermocouple to channel %d\n", i);
	  printf(" Select Thermocouple Type [JKSRBETN]: ");
	  scanf("%s", type);
	  switch(type[0]) {
	  case 'J':
	    bIReg = TYPE_J;
	    printf("Type J Thermocouple Selected: \n");
	    break;
	  case 'K':
	    bIReg = TYPE_K;
  	    printf("Type K Thermocouple Selected: \n");
	    break;
	  case 'T':
	    bIReg = TYPE_T;
    	    printf("Type T Thermocouple Selected: \n");
	    break;
	  case 'E':
	    bIReg = TYPE_E;
    	    printf("Type E Thermocouple Selected: \n");
	    break;
	  case 'R':
	    bIReg = TYPE_R;
      	    printf("Type R Thermocouple Selected: \n");
	    break;
	  case 'S':
	    bIReg = TYPE_S;
       	    printf("Type S Thermocouple Selected: \n");
	    break;
	  case 'B':
	    bIReg = TYPE_B;
       	    printf("Type B Thermocouple Selected: \n");
	    break;
	  case 'N':
	    bIReg = TYPE_N;
       	    printf("Type N Thermocouple Selected: \n");
	    break;
          default:
	    printf("Unknown or unsupported thermocopule type.\n");
	    break;
	  }
          usbSetItem_USBTC_AI(hid, i/2, i%2+CH_0_TC, bIReg);
	}
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
	do {
          usbAinScan_USBTC_AI(hid, CH0, nchan-1, 0, temperature_array);
	  for ( i = 0; i < nchan; i++ ) {
  	    printf("Channel %d:  %.2f degress Celsius or %.2f degrees Fahrenheit.\n",
		   i, temperature_array[i], celsius2fahr(temperature_array[i]));
	  }
	  printf("\n");
	  sleep(1);
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
	break;
      case 'v':
      	printf("Select Channel [4-7]: ");
        scanf("%d", &i);
        if ( i < 4 || i > 7 ) {
	  printf("Channel must be between 4-7 inclusive.  Setting to 4\n");
	i = 4;
    	}
	//usbSetItem_USBTC_AI(hid, i/2, SENSOR_TYPE, VOLTAGE);
	usbSetItem_USBTC_AI(hid, i/2, i%2+CH_0_VOLT_CONN, DIFFERENTIAL);
	usbSetItem_USBTC_AI(hid, i/2, i%2+CH_0_GAIN, GAIN_4X);  // +/- 10V.
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
	do {
          usbAin_USBTC_AI(hid, i, 0, &volts);
    	  printf("Channel: %d  %.2f Volts.\n", i, volts);
  	  sleep(1);
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
        break;
      default:
        break;
    }
  }
}
Example #10
0
int main(int argc, char **argv)
{
  int ch;
  uint8_t bIReg, bOReg;
  float temperature;
  float temperature_array[8];
  int nchan;
  int flag;
  char type;
  int i;
  float R0, A, B, C;      // RTD: Callendar-Van Dusen coefficients
  float A0, A1, A2;       // Thermistor: Steinhart-Hart coefficients
  float Offset, Scale;    // Semiconductor
  float value;
  
  hid_device *hid = 0x0;  // Composite device with 1 interface.
  int ret;
  wchar_t serial[64];
  wchar_t wstr[MAX_STR];

  ret = hid_init();
  if (ret < 0) {
    fprintf(stderr, "hid_init failed with return code %d\n", ret);
    return -1;
  }

  if ((hid = hid_open(MCC_VID, USB_TEMP_AI_PID, NULL)) > 0) {
      printf("USB TEMP-AI is found.\n");
  } else {
    fprintf(stderr, "USB-TEMP-AI is not found.\n");
  }


  /* config mask 0x01 means all inputs */
  usbDConfigPort_USBTC_AI(hid, DIO_DIR_OUT);
  usbDOut_USBTC_AI(hid, 0x0);

  while(1) {
    printf("\nUSB-TEMP-AI Testing\n");
    printf("----------------\n");
    printf("Hit 'b' to blink LED\n");
    printf("Hit 'c' to calibrate\n");
    printf("Hit 'd' to test DIO\n");
    printf("Hit 'e' to exit\n");
    printf("Hit 'f' for burnout status\n");
    printf("Hit 'g' to get serial number\n");
    printf("Hit 'i' for information\n");
    printf("Hit 'R' to reset\n");
    printf("Hit 'r' to measure temperature (RTD)\n");
    printf("Hit 'p' read the CJC\n");
    printf("Hit 's' to get status\n");
    printf("Hit 'S' to measure temperature (Semiconductor)\n");
    printf("Hit 't' to measure temperature (Thermocouple)\n");
    printf("Hit 'T' to measure temperature (Thermistor)\n");    
    printf("Hit 'x' to measure temperature (Thermocouple) multiple channels\n");

    while((ch = getchar()) == '\0' ||  ch == '\n');

    switch(ch) {
      case 'b': /* test to see if led blinks */
        usbBlink_USBTC_AI(hid);
        break;
      case 'c': /* calibration */
        usbCalibrate_USBTC_AI(hid, 0);
        usbCalibrate_USBTC_AI(hid, 1);
        break;
      case 'f':  /* Get status of thermocouple burnout detection */
	 printf("Burnout status = %#x\n", usbGetBurnoutStatus_USBTC_AI(hid, 0xf));
	 break;
      case 'g':
	hid_get_serial_number_string(hid, serial, 64);
        printf("Serial Number = %ls\n", serial);
        break;
      case 'i':
        // Read the Manufacuter String
        ret = hid_get_manufacturer_string(hid, wstr, MAX_STR);
        printf("Manufacturer String: %ls\n", wstr);
        // Read the Product String
        ret = hid_get_product_string(hid, wstr, MAX_STR);
        printf("Product String: %ls\n", wstr);
        // Read the Serial Number String
        ret = hid_get_serial_number_string(hid, wstr, MAX_STR);
        printf("Serial Number String: %ls\n", wstr);
        break;            
      case 'p':  /* read the CJC */
        usbAin_USBTC_AI(hid, CJC0, 0, &temperature);
	printf("CJC 0 = %.2f degress Celsius or %.2f degrees Fahrenheit.\n", temperature,
	        celsius2fahr(temperature));
        break;
      case 'd': /* test to see if led blinks */
        printf("conect DIO0 - DIO4\n");
	printf("conect DIO1 - DIO5\n");
	printf("conect DIO2 - DIO6\n");
	printf("conect DIO3 - DIO7\n");
	usbDConfigBit_USBTC_AI(hid, 0,  DIO_DIR_OUT);
	usbDConfigBit_USBTC_AI(hid, 1,  DIO_DIR_OUT);
	usbDConfigBit_USBTC_AI(hid, 2,  DIO_DIR_OUT);
	usbDConfigBit_USBTC_AI(hid, 3,  DIO_DIR_OUT);
	usbDConfigBit_USBTC_AI(hid, 4,  DIO_DIR_IN);
	usbDConfigBit_USBTC_AI(hid, 5,  DIO_DIR_IN);
	usbDConfigBit_USBTC_AI(hid, 6,  DIO_DIR_IN);
	usbDConfigBit_USBTC_AI(hid, 7,  DIO_DIR_IN);
	do {
  	  printf("Enter value [0-f]: ");
	  scanf("%hhx", &bIReg);
	  bIReg &= 0xf;
  	  usbDOut_USBTC_AI(hid, bIReg);
	  usbDIn_USBTC_AI(hid, &bOReg);
	  printf("value = %#x\n", bOReg);
	} while (toContinue());
	break;
      case 's':
        printf("Status = %#x\n", usbGetStatus_USBTC_AI(hid));
	break;
      case 'S':
        printf("Sampling Semiconductor TMP36\n");
	printf("Enter channel number [0-7]: ");
	scanf("%d", &ch);
        usbSetItem_USBTEMP_AI(hid, ch/2, SENSOR_TYPE, SEMICONDUCTOR);
	printf("        1.  Single ended.\n");
	printf("        2.  Differential.\n");
	printf("Enter connector type [1-4]: \n");
	scanf("%d", &i);
	switch (i) {
	  case 1: usbSetItem_USBTEMP_AI(hid, ch/2, CONNECTION_TYPE, SINGLE_ENDED);break;
	  case 2: usbSetItem_USBTEMP_AI(hid, ch/2, CONNECTION_TYPE, DIFFERENTIAL); break;
	  default: printf("Unknown type\n"); break;
	}
	printf("Enter Offset: ");
	scanf("%f", &Offset);
	printf("Enter Scale: ");
	scanf("%f", &Scale);
	usbSetItem_USBTEMP_AI(hid, ch/2, EXCITATION, EXCITATION_OFF);
	usbSetItem_USBTEMP_AI(hid, ch/2, CH_0_GAIN + ch%2, 0x1);       // Set for Semiconductor
	usbSetItem_USBTEMP_AI(hid, ch/2, CH_0_COEF_0 + ch%2, Offset);  // Offset
	usbGetItem_USBTC_AI(hid, ch/2, CH_0_COEF_0 + ch%2, &value);
	printf("Offset = %f     ", value);
	usbSetItem_USBTEMP_AI(hid, ch/2, CH_0_COEF_1 + ch%2, Scale);   // Scale
	usbGetItem_USBTC_AI(hid, ch/2, CH_0_COEF_1 + ch%2, &value);
	printf("Scale = %f     ", value);
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(fileno(stdin), F_SETFL, flag | O_NONBLOCK);
	do {
          usbAin_USBTC_AI(hid, ch, 0, &temperature);
  	  printf("Channel: %d  %.2f degress Celsius or %.2f degrees Fahrenheit.\n",
		 ch, temperature, celsius2fahr(temperature));
  	  sleep(1);
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
	break;
      case 'r':
        printf("Sampling RTD\n");
	printf("Enter channel number [0-7]: ");
	scanf("%d", &ch);
        usbSetItem_USBTEMP_AI(hid, ch/2, SENSOR_TYPE, RTD);
	printf("        1.  2-wire with 1 sensor.\n");
	printf("        2.  2-wire with 2 sensors.\n");
	printf("        3.  3-wire. \n");
	printf("        4.  4-wire.\n");
	printf("Enter connector type [1-4]: \n");
	scanf("%d", &i);
	switch (i) {
	  case 1: usbSetItem_USBTEMP_AI(hid, ch/2, CONNECTION_TYPE, TWO_WIRE_ONE_SENSOR);break;
	  case 2: usbSetItem_USBTEMP_AI(hid, ch/2, CONNECTION_TYPE, TWO_WIRE_TWO_SENSOR); break;
	  case 3: usbSetItem_USBTEMP_AI(hid, ch/2, CONNECTION_TYPE, THREE_WIRE);
	    printf("Connection-type = 3 wire.\n");
	    break;
	  case 4: usbSetItem_USBTEMP_AI(hid, ch/2, CONNECTION_TYPE, FOUR_WIRE);
	    printf("Connection-type = 4 wire.\n");
	    break;
	}
	R0 = 100.;
	A = .003908;
	B = -5.8019E-7;
	C = -4.2735E-12;
	usbSetItem_USBTEMP_AI(hid, ch/2, EXCITATION, MU_A_210);
	usbSetItem_USBTEMP_AI(hid, ch/2, CH_0_GAIN + ch%2, 0x2);          // Set 0 - 0.5V for RTD
	usbSetItem_USBTEMP_AI(hid, ch/2, CH_0_COEF_0 + ch%2, R0);         // R0 value
	usbGetItem_USBTC_AI(hid, ch/2, CH_0_COEF_0 + ch%2, &value);
	printf("R0 = %f     ", value);
	usbSetItem_USBTEMP_AI(hid, ch/2, CH_0_COEF_1 + ch%2, A);          // Callendar-Van Dusen Coefficient A
	usbGetItem_USBTC_AI(hid, ch/2, CH_0_COEF_1 + ch%2, &value);
	printf("A = %e     ", value);
	usbSetItem_USBTEMP_AI(hid, ch/2, CH_0_COEF_2 + ch%2, B);          // Callendar-Van Dusen Coefficient B
	usbGetItem_USBTC_AI(hid, ch/2, CH_0_COEF_2 + ch%2, &value);
	printf("B = %e     ", value);
	usbSetItem_USBTEMP_AI(hid, ch/2, CH_0_COEF_3 + ch%2, C);          // Callendar-Van Dusen Coefficient C
	usbGetItem_USBTC_AI(hid, ch/2, CH_0_COEF_3 + ch%2, &value);
	printf("C = %e\n", value);
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(fileno(stdin), F_SETFL, flag | O_NONBLOCK);
	do {
          usbAin_USBTC_AI(hid, ch, 0, &temperature);
  	  printf("Channel: %d  %.2f degress Celsius or %.2f degrees Fahrenheit.\n",
		 ch, temperature, celsius2fahr(temperature));
  	  sleep(1);
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
	break;
      case 'R':
        usbReset_USBTC_AI(hid);
        return 0;
	break;
      case 'e':
	hid_close(hid);
        hid_exit();
        return 0;
	break;
      case 't':
        printf("Select Channel [0-7]: ");
        scanf("%d", &i);
	usbSetItem_USBTEMP_AI(hid, i/2, SENSOR_TYPE, THERMOCOUPLE);
        usbSetItem_USBTEMP_AI(hid, i/2, EXCITATION, EXCITATION_OFF);
        printf("Connect thermocouple to channel %d\n", i);
	printf(" Select Thermocouple Type [JKSRBETN]: ");
	scanf("%s", &type);
	switch(type) {
	case 'J':
	  bIReg = TYPE_J;
	  printf("Type J Thermocouple Selected: \n");
	  break;
	case 'K':
	  bIReg = TYPE_K;
  	  printf("Type K Thermocouple Selected: \n");
	  break;
	case 'T':
	  bIReg = TYPE_T;
    	  printf("Type T Thermocouple Selected: \n");
	  break;
	case 'E':
	  bIReg = TYPE_E;
    	  printf("Type E Thermocouple Selected: \n");
	  break;
	case 'R':
	  bIReg = TYPE_R;
      	  printf("Type R Thermocouple Selected: \n");
	  break;
	case 'S':
	  bIReg = TYPE_S;
       	  printf("Type S Thermocouple Selected: \n");
	  break;
	case 'B':
	  bIReg = TYPE_B;
       	  printf("Type B Thermocouple Selected: \n");
	  break;
	case 'N':
	  bIReg = TYPE_N;
       	  printf("Type N Thermocouple Selected: \n");
	  break;
        default:
	  printf("Unknown or unsupported thermocopule type.\n");
	  break;
	}
        usbSetItem_USBTEMP_AI(hid, i/2, i%2+CH_0_TC, bIReg);
        usbCalibrate_USBTC_AI(hid, 0);
        usbCalibrate_USBTC_AI(hid, 1);
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
	do {
          usbAin_USBTC_AI(hid, i, 0, &temperature);
	  printf("Channel: %d  %.2f degress Celsius or %.2f degrees Fahrenheit.\n",
		 i, temperature, celsius2fahr(temperature));
  	  sleep(1);
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
	break;
      case 'T':
        printf("Sampling Thermistor\n");
	printf("Enter channel number [0-7]: ");
	scanf("%d", &ch);
        usbSetItem_USBTEMP_AI(hid, ch/2, SENSOR_TYPE, THERMISTOR);
	printf("        1.  2-wire with 1 sensor.\n");
	printf("        2.  2-wire with 2 sensors.\n");
	printf("        3.  3-wire.\n");
	printf("        4.  4-wire.\n");
	printf("Enter connector type [1-4]: \n");
	scanf("%d", &i);
	switch (i) {
	  case 1: usbSetItem_USBTEMP_AI(hid, ch/2, CONNECTION_TYPE, TWO_WIRE_ONE_SENSOR);break;
	  case 2: usbSetItem_USBTEMP_AI(hid, ch/2, CONNECTION_TYPE, TWO_WIRE_TWO_SENSOR); break;
	  case 3: usbSetItem_USBTEMP_AI(hid, ch/2, CONNECTION_TYPE, THREE_WIRE);
	    printf("Connection-type = 3 wire.\n");
	    break;
	  case 4: usbSetItem_USBTEMP_AI(hid, ch/2, CONNECTION_TYPE, FOUR_WIRE);
	    printf("Connection-type = 4 wire.\n");
	    break;
	}
	printf("Enter Steinhart-Hart coefficient A0: ");
	scanf("%f", &A0);
	printf("Enter Steinhart-Hart coefficient A1: ");
	scanf("%f", &A1);
	printf("Enter Steinhart-Hart coefficient A2: ");
	scanf("%f", &A2);
	usbSetItem_USBTEMP_AI(hid, ch/2, EXCITATION, MU_A_10);
	usbSetItem_USBTEMP_AI(hid, ch/2, CH_0_GAIN + ch%2, 0x0);      // Set for Thermnistor
	usbSetItem_USBTEMP_AI(hid, ch/2, CH_0_COEF_0 + ch%2, A0);     // Steinhart-Hart coefficient A0
	usbGetItem_USBTC_AI(hid, ch/2, CH_0_COEF_0 + ch%2, &value);
	printf("A0 = %f     ", value);
	usbSetItem_USBTEMP_AI(hid, ch/2, CH_0_COEF_1 + ch%2, A1);     // Steinhart-Hart coefficient A1
	usbGetItem_USBTC_AI(hid, ch/2, CH_0_COEF_1 + ch%2, &value);
	printf("A1 = %e     ", value);
	usbSetItem_USBTEMP_AI(hid, ch/2, CH_0_COEF_2 + ch%2, A2);     // Steinhart-Hart coefficient A2
	usbGetItem_USBTC_AI(hid, ch/2, CH_0_COEF_2 + ch%2, &value);
	printf("A2 = %e     ", value);
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(fileno(stdin), F_SETFL, flag | O_NONBLOCK);
	do {
          usbAin_USBTC_AI(hid, ch, 0, &temperature);
  	  printf("Channel: %d  %.2f degress Celsius or %.2f degrees Fahrenheit.\n",
		 ch, temperature, celsius2fahr(temperature));
  	  sleep(1);
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
	break;
      case 'x':
        printf("Enter number of Channels (1-8): ");
	scanf("%d", &nchan);
	for ( i = 0; i < nchan; i++ ) {
	  usbSetItem_USBTEMP_AI(hid, i/2, SENSOR_TYPE, THERMOCOUPLE);
	  usbSetItem_USBTEMP_AI(hid, i/2, EXCITATION, EXCITATION_OFF);
          printf("Connect thermocouple to channel %d\n", i);
	  printf("Select Thermocouple Type [JKSRBETN]: ");
	  scanf("%s", &type);
	  switch(type) {
	  case 'J':
	    bIReg = TYPE_J;
	    printf("Type J Thermocouple Selected: \n");
	    break;
	  case 'K':
	    bIReg = TYPE_K;
  	    printf("Type K Thermocouple Selected: \n");
	    break;
	  case 'T':
	    bIReg = TYPE_T;
    	    printf("Type T Thermocouple Selected: \n");
	    break;
	  case 'E':
	    bIReg = TYPE_E;
    	    printf("Type E Thermocouple Selected: \n");
	    break;
	  case 'R':
	    bIReg = TYPE_R;
      	    printf("Type R Thermocouple Selected: \n");
	    break;
	  case 'S':
	    bIReg = TYPE_S;
       	    printf("Type S Thermocouple Selected: \n");
	    break;
	  case 'B':
	    bIReg = TYPE_B;
       	    printf("Type B Thermocouple Selected: \n");
	    break;
	  case 'N':
	    bIReg = TYPE_N;
       	    printf("Type N Thermocouple Selected: \n");
	    break;
          default:
	    printf("Unknown or unsupported thermocopule type.\n");
	    break;
	  }
          usbSetItem_USBTEMP_AI(hid, i/2, i%2+CH_0_TC, bIReg);
	}
        flag = fcntl(fileno(stdin), F_GETFL);
        fcntl(0, F_SETFL, flag | O_NONBLOCK);
	do {
          usbAinScan_USBTC_AI(hid, CH0, nchan-1, 0, temperature_array);
	  for ( i = 0; i < nchan; i++ ) {
  	    printf("Channel %d:  %.2f degress Celsius or %.2f degrees Fahrenheit.\n",
		   i, temperature_array[i], celsius2fahr(temperature_array[i]));
	  }
	  printf("\n");
	  sleep(1);
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
	break;
      default:
        break;
    }
  }
}
Example #11
0
int main (int argc, char **argv)
{
  __u8 input;
  int temp;
  int ch;
  char serial[9];
  
  HIDInterface*  hid = 0x0;
  hid_return ret;
  int interface;
  __u8 port;
  __u8 pin = 0;
  __u8 bit_value;
  __u16 status;
  int device = 0;  // either USBSSR24 or USBSSR08
  
  // Debug information.  Delete when not needed    
  //  hid_set_debug(HID_DEBUG_ALL);
  //  hid_set_debug_stream(stderr);
  //  hid_set_usb_debug(2);

  ret = hid_init();
  if (ret != HID_RET_SUCCESS) {
    fprintf(stderr, "hid_init failed with return code %d\n", ret);
    return -1;
  }

  if ((interface = PMD_Find_Interface(&hid, 0, USBSSR24_PID)) >= 0) {
    printf("USB SSR24 Device is found! Interface = %d\n", interface);
    device = USBSSR24_PID;
  } else if ((interface = PMD_Find_Interface(&hid, 0, USBSSR08_PID)) >= 0) {
    printf("USB SSR08 Device is found! Interface = %d\n", interface);
    device = USBSSR08_PID;
  } else {
    fprintf(stderr, "USB SSR24 or SSR08 not found.\n");
    exit(-1);
  }

  while(1) {
    printf("\nUSB SSR24 & SSR08 Testing\n");
    printf("----------------\n");
    printf("Hit 'b' to blink LED\n");
    printf("Hit 'd' to test digital I/O \n");
    printf("Hit 'e' to exit\n");
    printf("Hit 'g' to get serial number\n");
    printf("Hit 's' to get status\n");
    printf("Hit 'r' to reset\n");
    printf("Hit 't' to test digital bit I/O\n");
    
    while((ch = getchar()) == '\0' || ch == '\n');
    
    switch(ch) {
    case 'b': /* test to see if led blinks */
      usbBlink_USBSSR(hid);
      break;
    case 'd':
      printf("\nTesting Digital I/O....\n");
      do {
	if (device == USBSSR24_PID) {
	  printf("Enter a port number [0-3]: ");
	  scanf("%hhd", &port);
	  if (port > 3) break;
	} else {
	  printf("Enter a port number [2-3]: ");  // only CL and CH on SSR08
	  scanf("%hhd", &port);
	  printf("port = %d\n", port);
	  if (port != 2 &&  port != 3) continue;
	}
        status = usbGetStatus_USBSSR(hid);
        switch (port) {
          case 0:   /* Port A */
            if (status & 0x1<<0) {
              printf("    Port A direction = input\n");
              input = usbDIn_USBSSR(hid, port);
              printf("    Port A = %#x\n", input);
            } else {
              printf("    Port A direction = output\n");
	      printf("Enter a byte number [0-0xff] : " );
	      scanf("%x", &temp);
              usbDOut_USBSSR(hid, port, (__u8)temp);
            }
            if (status & 0x1<<4) {
              printf("    Port A polarity = normal\n");
            } else {
              printf("    Port A polarity = inverted\n");
            }
            if (status & 0x1<<8) {
              printf("    Port A  = pull up\n");
            } else {
              printf("    Port A  = pull down\n");
            }
            break;
          case 1:   /* Port B */
            if (status & 0x1<<1) {
              printf("    Port B direction = input\n");
              input = usbDIn_USBSSR(hid, port);
              printf("    Port B = %#x\n", input);
            } else {
              printf("    Port B direction = output\n");
	      printf("Enter a byte number [0-0xff] : " );
	      scanf("%x", &temp);
              usbDOut_USBSSR(hid, port, (__u8)temp);
            }
            if (status & 0x1<<5) {
              printf("    Port B polarity = normal\n");
            } else {
              printf("    Port B polarity = inverted\n");
            }
            if (status & 0x1<<9) {
              printf("    Port B  = pull up\n");
            } else {
              printf("    Port B  = pull down\n");
            }
            break;
          case 2:   /* Port C Low */
            if (status & 0x1<<2) {
              printf("    Port C Low direction = input\n");
              input = usbDIn_USBSSR(hid, port);
              printf("    Port C Low = %#x\n", input);
            } else {
              printf("    Port C Low direction = output\n");
	      printf("Enter a byte number [0-0xff] : " );
	      scanf("%x", &temp);
              usbDOut_USBSSR(hid, port, (__u8)temp);
            }
            if (status & 0x1<<6) {
              printf("    Port C Low polarity = normal\n");
            } else {
              printf("    Port C Low polarity = inverted\n");
            }
            if (status & 0x1<<10) {
              printf("    Port C Low  = pull up\n");
            } else {
              printf("    Port C Low  = pull down\n");
            }
            break;
          case 3:   /* Port C High */
            if (status & 0x1<<3) {
              printf("    Port C High direction = input\n");
              input = usbDIn_USBSSR(hid, port);
              printf("    Port C High = %#x\n", input);
            } else {
              printf("    Port C High direction = output\n");
	      printf("Enter a byte number [0-0xff] : " );
	      scanf("%x", &temp);
              usbDOut_USBSSR(hid, port, (__u8)temp);

              usbDOut_USBSSR(hid, port, (__u8)temp);
            }
            if (status & 0x1<<7) {
              printf("    Port C High polarity = normal\n");
            } else {
              printf("    Port C High polarity = inverted\n");
            }
            if (status & 0x1<<11) {
              printf("    Port C High  = pull up\n");
            } else {
              printf("    Port C High  = pull down\n");
            }
            break;
        }
      } while (toContinue());
      break;
    case 'g':
      usbReadCode_USBSSR(hid, 0x200000, 8, (__u8 *) serial);
      serial[8] = '\0';
      printf("Serial Number = %s\n", serial);
      break;
    case 't':
      printf("\nTesting Digital Bit I/O....\n");
      do {
	if (device == USBSSR24_PID) {
	  printf("Enter a port number [0-3]: ");
	  scanf("%hhd", &port);
	  if (port > 3) break;
	} else {
	  printf("Enter a port number [2-3]: ");  // only CL and CH on SSR08
	  scanf("%hhd", &port);
	  printf("port = %d\n", port);
	  if (port != 2 &&  port != 3) continue;
	}
        printf("Select the Pin in port  %d  [0-7] :", port);
        scanf("%hhd", &pin);
        status = usbGetStatus_USBSSR(hid);
        switch (port) {
          case 0:   /* Port A */
            if (status & 0x1<<0) {
              printf("    Port A direction = input\n");
              input = usbDBitIn_USBSSR(hid, port, pin);
              printf("    Port %d  Pin %d = %#x\n", port, pin, input);
            } else {
              printf("    Port A direction = output\n");
              printf("Enter a bit value for output (0 | 1) : ");
	      scanf("%hhd", &bit_value);
              usbDBitOut_USBSSR(hid, port, pin, bit_value);
            }
            if (status & 0x1<<4) {
              printf("    Port A polarity = normal\n");
            } else {
              printf("    Port A polarity = inverted\n");
            }
            if (status & 0x1<<8) {
              printf("    Port A  = pull up\n");
            } else {
              printf("    Port A  = pull down\n");
            }
            break;
          case 1:   /* Port B */
            if (status & 0x1<<1) {
              printf("    Port B direction = input\n");
              input = usbDBitIn_USBSSR(hid, port, pin);
              printf("    Port %d  Pin %d = %#x\n", port, pin, input);
            } else {
              printf("    Port B direction = output\n");
	      printf("Enter a bit value for output (0 | 1) : ");
	      scanf("%hhd", &bit_value);
              usbDBitOut_USBSSR(hid, port, pin, bit_value);
            }
            if (status & 0x1<<5) {
              printf("    Port B polarity = normal\n");
            } else {
              printf("    Port B polarity = inverted\n");
            }
            if (status & 0x1<<9) {
              printf("    Port B  = pull up\n");
            } else {
              printf("    Port B  = pull down\n");
            }
            break;
          case 2:   /* Port C Low */
            if (status & 0x1<<2) {
              printf("    Port C Low direction = input\n");
              input = usbDBitIn_USBSSR(hid, port, pin);
              printf("    Port %d  Pin %d = %#x\n", port, pin, input);
            } else {
              printf("    Port C Low direction = output\n");
              printf("Enter a bit value for output (0 | 1) : ");
	      scanf("%hhd", &bit_value);
              usbDBitOut_USBSSR(hid, port, pin, bit_value);
            }
            if (status & 0x1<<6) {
              printf("    Port C Low polarity = normal\n");
            } else {
              printf("    Port C Low polarity = inverted\n");
            }
            if (status & 0x1<<10) {
              printf("    Port C Low  = pull up\n");
            } else {
              printf("    Port C Low  = pull down\n");
            }
            break;
          case 3:   /* Port C High */
            if (status & 0x1<<3) {
              printf("    Port C High direction = input\n");
              input = usbDBitIn_USBSSR(hid, port, pin);
              printf("    Port %d  Pin %d = %#x\n", port, pin, input);

            } else {
              printf("    Port B direction = output\n");
	      printf("Enter a bit value for output (0 | 1) : ");
	      scanf("%hhd", &bit_value);
              usbDBitOut_USBSSR(hid, port, pin, bit_value);
            }
            if (status & 0x1<<7) {
              printf("    Port C High polarity = normal\n");
            } else {
              printf("    Port C High polarity = inverted\n");
            }
            if (status & 0x1<<11) {
              printf("    Port C High  = pull up\n");
            } else {
              printf("    Port C High  = pull down\n");
            }
            break;
        }
      } while (toContinue());
      break;
    case 's':
      status = usbGetStatus_USBSSR(hid);
      printf("Status = %#x\n", status);
      if (device == USBSSR24_PID) {
	if (status & 0x1<<0) {
	  printf("    Port A direction = input\n");
	} else {
	  printf("    Port A direction = output\n");
	}
	if (status & 0x1<<4) {
	printf("    Port A polarity = normal\n");
	} else {
	  printf("    Port A polarity = inverted\n");
	}
	if (status & 0x1<<8) {
	  printf("    Port A  = pull up\n");
	} else {
	  printf("    Port A  = pull down\n");
	}
	/* Port B */
	if (status & 0x1<<1) {
	  printf("    Port B direction = input\n");
	} else {
	  printf("    Port B direction = output\n");
	}
	if (status & 0x1<<5) {
	  printf("    Port B polarity = normal\n");
	} else {
	  printf("    Port B polarity = inverted\n");
	}
	if (status & 0x1<<9) {
	  printf("    Port B  = pull up\n");
	} else {
	  printf("    Port B  = pull down\n");
	}
      }
      /* Port C Low */
      if (status & 0x1<<2) {
	printf("    Port C Low direction = input\n");
      } else {
	printf("    Port C Low direction = output\n");
	usbDOut_USBSSR(hid, port, (__u8)temp);
      }
      if (status & 0x1<<6) {
	printf("    Port C Low polarity = normal\n");
      } else {
	printf("    Port C Low polarity = inverted\n");
      }
      if (status & 0x1<<10) {
	printf("    Port C Low  = pull up\n");
      } else {
	printf("    Port C Low  = pull down\n");
      }
      /* Port C High */
      if (status & 0x1<<3) {
	printf("    Port C High direction = input\n");
      } else {
	printf("    Port C High direction = output\n");
      }
      if (status & 0x1<<7) {
	printf("    Port C High polarity = normal\n");
      } else {
	printf("    Port C High polarity = inverted\n");
      }
      if (status & 0x1<<11) {
	printf("    Port C High  = pull up\n");
      } else {
	printf("    Port C High  = pull down\n");
      }
      break;
    case 'r':
      usbReset_USBSSR(hid);
      return 0;
      break;
    case 'e':
      ret = hid_close(hid);
      if (ret != HID_RET_SUCCESS) {
	fprintf(stderr, "hid_close failed with return code %d\n", ret);
	return 1;
      }

      hid_delete_HIDInterface(&hid);

      ret = hid_cleanup();
      if (ret != HID_RET_SUCCESS) {
	fprintf(stderr, "hid_cleanup failed with return code %d\n", ret);
	return 1;
      }
      return 0;
    default:
      break;
    }
  }
}
int main (int argc, char **argv)
{
  usb_dev_handle *udev = NULL;

  double frequency;
  double voltage;
  float temperature;
  float table_AIN[NGAINS_2600][2];
  float table_AOUT[NCHAN_AO_26X7][2];
  ScanList list[NCHAN_2600];  // scan list used to configure the A/D channels.
  int usb26X7 = FALSE;
  int ch;
  int i, flag;
  int nSamples = 0;
  __u8 input, timer;
  int temp, ret;
  __u8 options;
  char serial[9];
  __u32 period;
  __u16 version;

  __u16 value;
  __u16 dataAIn[512];  // holds 16 bit unsigned analog input data
  __u16 dataAOut[128]; // holds 16 bit unsigned analog output data

  __u8 mode, gain, channel;

  udev = NULL;
  if ((udev = usb_device_find_USB_MCC(USB2637_PID))) {
    printf("Success, found a USB 2637.\n");
    usb26X7 = TRUE;
  } else if ((udev = usb_device_find_USB_MCC(USB2633_PID))) {
    printf("Success, found a USB 2633.\n");
  } else if ((udev = usb_device_find_USB_MCC(USB2623_PID))) {
    printf("Success, found a USB 2623.\n");
  } else if ((udev = usb_device_find_USB_MCC(USB2627_PID))) {
    printf("Success, found a USB 2627.\n");
    usb26X7 = TRUE;
  } else {
    printf("Failure, did not find a USB 2600!\n");
    return 0;
  }
  // some initialization
  usbInit_2600(udev);
  usbBuildGainTable_USB2600(udev, table_AIN);
  for (i = 0; i < NGAINS_2600; i++) {
    printf("Gain: %d   Slope = %f   Offset = %f\n", i, table_AIN[i][0], table_AIN[i][1]);
  }
  if (usb26X7) {
    usbBuildGainTable_USB26X7(udev, table_AOUT);
    printf("\nAnalog Output Table\n");
    for (i = 0; i < NCHAN_AO_26X7; i++) {
      printf("Gain: %d   Slope = %f   Offset = %f\n", i, table_AOUT[i][0], table_AOUT[i][1]);
    }
  }

  while(1) {
    printf("\nUSB 2600 Testing\n");
    printf("----------------\n");
    printf("Hit 'b' to blink\n");
    printf("Hit 'c' to test counter\n");
    printf("Hit 'd' to test digitial IO\n");
    printf("Hit 'i' to test Analog Input\n");
    printf("Hit 'I' to test Analog Input Scan\n");
    printf("Hit 'o' to test Analog Output\n");
    printf("Hit 'O' to test Analog Output Scan\n");
    printf("Hit 'r' to reset the device\n");
    printf("Hit 's' to get serial number\n");
    printf("Hit 'S' to get Status\n");
    printf("Hit 't' to test the timers\n");
    printf("Hit 'T' to get temperature\n");
    printf("Hit 'v' to get version numbers\n");
    printf("Hit 'e' to exit\n");

    while((ch = getchar()) == '\0' || ch == '\n');
    switch(ch) {
      case 'b': /* test to see if LED blinks */
        printf("Enter number or times to blink: ");
        scanf("%hhd", &options);
        usbBlink_USB2600(udev, options);
	break;
      case 'c':
        usbCounterInit_USB2600(udev, COUNTER0);
        printf("Connect A0 to CTR0\n");
	usbDTristateW_USB2600(udev,0, 0x0);
        toContinue();
        for (i = 0; i < 100; i++) {
	  usbDLatchW_USB2600(udev, 0, 0x0);
	  usbDLatchW_USB2600(udev, 0, 0x1);
        }
        printf("Count = %d.  Should read 100.\n", usbCounter_USB2600(udev,COUNTER0));
        break;      
      case 'd':
        printf("\nTesting Digital I/O...\n");
	printf("connect pins Port A [0-8] <--> Port B[0-8]\n");
	usbDTristateW_USB2600(udev,0,0x00);
	usbDTristateW_USB2600(udev,1,0xff);
	printf("Digital Port 0 Tristate Register = %#x\n", usbDTristateR_USB2600(udev,0));
	printf("Digital Port 1 Tristate Register = %#x\n", usbDTristateR_USB2600(udev,1));
	do {
          printf("Enter a byte number [0-0xff]: " );
          scanf("%x", &temp);
          usbDLatchW_USB2600(udev, 0, (__u16)temp);
	  temp = usbDLatchR_USB2600(udev,0);
          input = usbDPort_USB2600(udev,1);
          printf("The number you entered = %#x   Latched value = %#x\n\n",input, temp);
	  for (i = 0; i < 8; i++) {
	    printf("Bit %d = %d\n", i, (temp>>i)&0x1);
	  }
        } while (toContinue());
        break;
      case 'o':
        if (!usb26X7) {
	  printf("Ananlog output only on USB-26X7 models.\n");
	  break;
	}
        printf("Output value on VDAC 0\n");
	do {
	  printf("Enter output voltage [-10 to 10]: ");
	  scanf("%lf", &voltage);
	  usbAOut_USB26X7(udev, 0, voltage, table_AOUT);
	} while (toContinue());
	break;
      case 'O':
        if (!usb26X7) {
	  printf("Analog output only on the USB-26X7 model.\n");
	  break;
	}
        channel = 0;
	printf("Test of Analog Output Scan.\n");
	printf("Hook scope up to VDAC 0\n");
	printf("Enter desired frequency of sine wave [Hz]: ");
	scanf("%lf", &frequency);
        frequency *= 128.;

	for (i = 0; i < 128; i++) {
	  voltage = 10*sin(2.*M_PI*i/128.);
          voltage = (voltage/10.*32768. + 32768.);
	  dataAOut[i] = voltage*table_AOUT[channel][0] + table_AOUT[channel][1];
	  //  dataAOut[i] = 0x0;
          //  dataAOut[i+1] = 0xffff;
	}
	usbAOutScanStop_USB26X7(udev);
	usbAOutScanStart_USB26X7(udev, 0, 0, frequency, (0x1 << channel));
	printf("Hit \'s <CR>\' to stop ");
	flag = fcntl(fileno(stdin), F_GETFL);
	fcntl(0, F_SETFL, flag | O_NONBLOCK);
	do {
	  if (usb_bulk_write(udev, USB_ENDPOINT_OUT|2, (char *) dataAOut, sizeof(dataAOut), 1000) < 0) {
	    //perror("usb_bulk_write error in AOutScan.");
	  }
	} while (!isalpha(getchar()));
	fcntl(fileno(stdin), F_SETFL, flag);
	usbAOutScanStop_USB26X7(udev);
	break;
      case 'e':
        cleanup_USB2600(udev);
        return 0;
      case 'i':
	printf("Input channel [0-7]: ");
	scanf("%hhd", &channel);
	printf("Gain Range for channel %d: 1 = +/-10V  2 = +/- 5V  3 = +/- 2V  4 = +/- 1V: ",channel);
	while((ch = getchar()) == '\0' || ch == '\n');
	switch(ch) {
	  case '1': gain = BP_10V; break;
  	  case '2': gain = BP_5V; break;
	  case '3': gain = BP_2V; break;
	  case '4': gain = BP_1V; break;
	  default:  gain = BP_10V; break;
	}
	mode = (LAST_CHANNEL | SINGLE_ENDED);
	list[0].range = gain;
        list[0].mode = mode;
	list[0].channel = channel;
	usbAInConfig_USB2600(udev, list);
	for (i = 0; i < 20; i++) {
	  value = usbAIn_USB2600(udev, channel);
	  value = rint(value*table_AIN[gain][0] + table_AIN[gain][1]);
	  printf("Channel %d  Mode = %#x  Gain = %d Sample[%d] = %#x Volts = %lf\n",
		 list[0].channel, list[0].mode, list[0].range, i, value, volts_USB2600(udev,gain,value));
	  usleep(50000);	  
	}
        break;
      case 'I':
	printf("Testing USB-2600 Analog Input Scan.\n");
	usbAInScanStop_USB2600(udev);
	usbAInScanClearFIFO_USB2600(udev);
        printf("Enter number of samples (less than 512): ");
        scanf("%d", &nSamples);
	printf("Input channel [0-7]: ");
        scanf("%hhd", &channel);
	printf("Gain Range for channel %d: 1 = +/-10V  2 = +/- 5V  3 = +/- 2V  4 = +/- 1V: ",channel);
	while((ch = getchar()) == '\0' || ch == '\n');
	switch(ch) {
	  case '1': gain = BP_10V; break;
  	  case '2': gain = BP_5V; break;
	  case '3': gain = BP_2V; break;
	  case '4': gain = BP_1V; break;
	  default:  gain = BP_10V; break;
	}
	mode = (LAST_CHANNEL | SINGLE_ENDED);
        list[0].range = gain;
        list[0].mode = mode;
        list[0].channel = channel;
	usbAInConfig_USB2600(udev, list);
        printf("Enter sampling frequency [Hz]: ");
	scanf("%lf", &frequency);
	usbAInScanStart_USB2600(udev, nSamples, 0, frequency, 0xff, 0);
	sleep(1);
	ret = usbAInScanRead_USB2600(udev, nSamples, 1, dataAIn);
	printf("Number samples read = %d\n", ret/2);
	for (i = 0; i < nSamples; i++) {
	  dataAIn[i] = rint(dataAIn[i]*table_AIN[gain][0] + table_AIN[gain][1]);
	  printf("Channel %d  Mode = %d  Gain = %d Sample[%d] = %#x Volts = %lf\n", channel,
		 mode, gain, i, dataAIn[i], volts_USB2600(udev,gain,dataAIn[i]));
	}
        break;
      case 'r':
	usbReset_USB2600(udev);
	return 0;
	break;
      case 's':
        usbGetSerialNumber_USB2600(udev, serial);
        printf("Serial number = %s\n", serial);
        break;
      case 'S':
        printf("Status = %#x\n", usbStatus_USB2600(udev));
	break;
      case 't':
        printf("Enter timer [0-3]: ");
        scanf("%hhd", &timer);
        printf("Enter frequency of timer: ");
        scanf("%lf", &frequency);
	period = 64.E6/frequency - 1;
	usbTimerPeriodW_USB2600(udev, timer, period);
	usbTimerPulseWidthW_USB2600(udev, timer, period / 2);
	usbTimerCountW_USB2600(udev, timer, 0);
	usbTimerDelayW_USB2600(udev, timer, 0);
	usbTimerControlW_USB2600(udev, timer, 0x1);
	toContinue();
	usbTimerControlW_USB2600(udev, timer, 0x0);
        break;
      case 'T':
        usbTemperature_USB2600(udev, &temperature);
	printf("Temperature = %.2f deg C  or  %.2f deg F \n", temperature, 9.0/5.0*temperature + 32.);
	break;
      case 'v':
	version = 0xbeef;
        usbFPGAVersion_USB2600(udev, &version);
	printf("FPGA version %02x.%02x\n", version >> 0x8, version & 0xff);
	break;
    default:
        break;
    }
  }
}
Example #13
0
int main (int argc, char **argv) {
  
  int fd = -1;
  int ch;
  int nInterfaces;
  int temp;
  int device = 0;  // either USBERB24 or USBERB08
  __u16 status;
  char serial[9];
  __u8 input; 
  __u8 port;
  __u8 pin = 0;
  __u8 bit_value;

 if ((nInterfaces = PMD_Find(MCC_VID, USBERB24_PID, &fd)) > 0) {
   printf("USB ERB-24 Device is found! Number of Interfaces = %d\n", nInterfaces);
   device = USBERB24_PID;
  } else if ((nInterfaces = PMD_Find(MCC_VID, USBERB08_PID, &fd)) > 0) {
    printf("USB ERB-08 Device is found! Number of Interfaces = %d\n", nInterfaces);
    device = USBERB08_PID;
  } else {
    fprintf(stderr, "USB ERB 24 & 08  not found.\n");
    exit(1);	
  }

  while(1) {
    printf("\nUSB ERB24 and ERB08 Testing\n");
    printf("----------------\n");
    printf("Hit 'b' to blink LED\n");
    printf("Hit 'd' to test digital I/O \n");
    printf("Hit 'e' to exit\n");
    printf("Hit 'g' to get serial number\n");
    printf("Hit 's' to get status\n");
    printf("Hit 'r' to reset\n");
    printf("Hit 't' to test digital bit I/O\n");
    printf("Hit 'l' to get temperature\n");
    
    while((ch = getchar()) == '\0' || ch == '\n');

    switch(ch) {
    case 'b': /* test to see if led blinks */
      usbBlink_USBERB(fd);
      break;
    case 'd':
      printf("\nTesting Digital I/O \n");
      do {
	if (device == USBERB24_PID) {
	  printf("Enter a port number [0-3]: ");
	  scanf("%hhd", &port);
	  if (port > 3) break;
	  printf("Enter a byte number [0-0xff] : " );
          scanf("%x", &temp);
          usbDOut_USBERB(fd, port, (__u8)temp);
	} else {
	  printf("Enter a port number [2-3]: ");  // only CL and CH on ERB08
	  scanf("%hhd", &port);
	  printf("port = %d\n", port);
	  if (port != 2 &&  port != 3) continue;
          printf("Enter a nibble number [0-0xf] : " );
          scanf("%x", &temp);
	  usbDOut_USBERB(fd, port, (__u8)temp);
	}
        status = usbGetStatus_USBERB(fd);
	input = usbDIn_USBERB(fd, port);
        switch (port) {
          case 0:   /* Port A */
	    printf("    Port A = %#x\n", input);
            if (status & 0x1<<0) {
              printf("    Port A polarity = normal\n");
            } else {
              printf("    Port A polarity = inverted\n");
            }
            if (status & 0x1<<4) {
              printf("    Port A  = pull up\n");
            } else {
              printf("    Port A  = pull down\n");
            }
            break;
          case 1:   /* Port B */
	    printf("    Port B = %#x\n", input);
            if (status & 0x1<<1) {
              printf("    Port B polarity = normal\n");
            } else {
              printf("    Port B polarity = inverted\n");
            }
            if (status & 0x1<<5) {
              printf("    Port B  = pull up\n");
            } else {
              printf("    Port B  = pull down\n");
            }
            break;
          case 2:   /* Port C Low */
	    printf("    Port C Low = %#x\n", input);
            if (status & 0x1<<2) {
              printf("    Port C Low polarity = normal\n");
            } else {
              printf("    Port C Low polarity = inverted\n");
            }
            if (status & 0x1<<6) {
              printf("    Port C Low  = pull up\n");
            } else {
              printf("    Port C Low  = pull down\n");
            }
            break;
          case 3:   /* Port C High */
	    printf("    Port C High = %#x\n", input);
            if (status & 0x1<<3) {
              printf("    Port C High polarity = normal\n");
            } else {
              printf("    Port C High polarity = inverted\n");
            }
            if (status & 0x1<<7) {
              printf("    Port C High  = pull up\n");
            } else {
              printf("    Port C High  = pull down\n");
            }
            break;
        }
      } while (toContinue());
      break;
    case 'g':
      strncpy(serial, PMD_GetSerialNumber(fd), 9);
      printf("Serial Number = %s\n", serial);
      break;
    case 't':
      printf("\nTesting Digital Bit I/O....\n");
      do {
	if (device == USBERB24_PID) {
	  printf("Enter a port number [0-3]: ");
	  scanf("%hhd", &port);
	  if (port > 3) break;
	} else {
	  printf("Enter a port number [2-3]: ");  // only CL and CH on ERB08
	  scanf("%hhd", &port);
	  printf("port = %d\n", port);
	  if (port != 2 &&  port != 3) continue;
	}
        printf("Select the Pin in port  %d  [0-7] :", port);
        scanf("%hhd", &pin);
	printf("Enter a bit value for output (0 | 1) : ");
	scanf("%hhd", &bit_value);
	usbDBitOut_USBERB(fd, port, pin, bit_value);
        status = usbGetStatus_USBERB(fd);
	input = usbDBitIn_USBERB(fd, port, pin);
	printf("    Port %d  Pin %d = %#x\n", port, pin, input);
        switch (port) {
          case 0:   /* Port A */
            if (status & 0x1<<0) {
              printf("    Port A polarity = normal\n");
            } else {
              printf("    Port A polarity = inverted\n");
            }
            if (status & 0x1<<4) {
              printf("    Port A  = pull up\n");
            } else {
              printf("    Port A  = pull down\n");
            }
            break;
          case 1:   /* Port B */
            if (status & 0x1<<1) {
              printf("    Port B polarity = normal\n");
            } else {
              printf("    Port B polarity = inverted\n");
            }
            if (status & 0x1<<1) {
              printf("    Port B  = pull up\n");
            } else {
              printf("    Port B  = pull down\n");
            }
            break;
          case 2:   /* Port C Low */
            if (status & 0x1<<2) {
              printf("    Port C Low polarity = normal\n");
            } else {
              printf("    Port C Low polarity = inverted\n");
            }
            if (status & 0x1<<6) {
              printf("    Port C Low  = pull up\n");
            } else {
              printf("    Port C Low  = pull down\n");
            }
            break;
          case 3:   /* Port C High */
            if (status & 0x1<<3) {
              printf("    Port C High polarity = normal\n");
            } else {
              printf("    Port C High polarity = inverted\n");
            }
            if (status & 0x1<<7) {
              printf("    Port C High  = pull up\n");
            } else {
              printf("    Port C High  = pull down\n");
            }
            break;
        }
      } while (toContinue());
      break;
    case 's':
      status = usbGetStatus_USBERB(fd);      
      printf("Status = %#x\n", status);
      if (device == USBERB24_PID) {
	if (status & 0x1<<1) {
	  printf("    Port A polarity = normal\n");
	} else {
	  printf("    Port A polarity = inverted\n");
	}
	if (status & 0x1<<4) {
	  printf("    Port A  = pull up\n");
	} else {
	  printf("    Port A  = pull down\n");
	}
	/* Port B */
	if (status & 0x1<<2) {
	  printf("    Port B polarity = normal\n");
	} else {
	  printf("    Port B polarity = inverted\n");
	}
	if (status & 0x1<<4) {
	  printf("    Port B  = pull up\n");
	} else {
	  printf("    Port B  = pull down\n");
	}
      }
      /* Port C Low */
      if (status & 0x1<<3) {
	printf("    Port C Low polarity = normal\n");
      } else {
	printf("    Port C Low polarity = inverted\n");
      }
      if (status & 0x1<<6) {
	printf("    Port C Low  = pull up\n");
      } else {
	printf("    Port C Low  = pull down\n");
      }
      /* Port C High */
      if (status & 0x1<<4) {
	printf("    Port C High polarity = normal\n");
      } else {
	printf("    Port C High polarity = inverted\n");
      }
      if (status & 0x1<<7) {
	printf("    Port C High  = pull up\n");
      } else {
	printf("    Port C High  = pull down\n");
      }
      break;
    case 'l':
      if (device == USBERB24_PID) {
	printf("Temperature = %f C\n", usbGetTemp_USBERB(fd));
      } else {
        printf("N/A on ERB08\n");
      }
      break;
    case 'r':
      usbReset_USBERB(fd);
      return 0;
      break;
    case 'e':
      close(fd);
      return 0;
    default:
      break;
    }
  }
}