示例#1
0
int usbAInScanRead_USB1608FS_Plus(libusb_device_handle *udev, int nScan, int nChan, uint16_t *data, uint8_t options)
{
  int i;
  int ret = -1;
  int nbytes = nChan*nScan*2;    // number of bytes to read in 64 bit chunks
  int transferred;
  uint16_t status = 0;
  char value[MAX_PACKET_SIZE];

  if (options & IMMEDIATE_TRANSFER_MODE) {
    for (i = 0; i < nbytes/2; i++) {
      ret = libusb_bulk_transfer(udev, LIBUSB_ENDPOINT_IN|1, (unsigned char *) &data[i], 2, &transferred, 2000);
      if (ret < 0) {
	perror("usbAInScanRead_USB1608FS_Plus: error in usb_bulk_transfer.");
      }
      if (transferred != 2) {
	fprintf(stderr, "usbAInScanRead_USB1608_Plus: number of bytes transferred = %d, nbytes = %d\n", transferred, nbytes);
      }
    }
  } else { 
    ret = libusb_bulk_transfer(udev, LIBUSB_ENDPOINT_IN|1, (unsigned char *) data, nbytes, &transferred, HS_DELAY);
    if (ret < 0) {
      perror("usbAInScanRead_USB1608FS_Plus: error in usb_bulk_transfer.");
    }
    if (transferred != nbytes) {
      fprintf(stderr, "usbAInScanRead_USB1608_Plus: number of bytes transferred = %d, nbytes = %d\n", transferred, nbytes);
    }
  }

  status = usbStatus_USB1608FS_Plus(udev);
  // if nbytes is a multiple of wMaxPacketSize the device will send a zero byte packet.
  if ((nbytes%wMaxPacketSize) == 0 && !(status & AIN_SCAN_RUNNING)) {
    libusb_bulk_transfer(udev, LIBUSB_ENDPOINT_IN|1, (unsigned char *) value, 2, &ret, 100);
  }

  if ((status & AIN_SCAN_OVERRUN)) {
    printf("Analog AIn scan overrun.\n");
    usbAInScanStop_USB1608FS_Plus(udev);
    usbAInScanClearFIFO_USB1608FS_Plus(udev);
  }
  return nbytes;
}
int usbAInScanRead_USB1608FS_Plus(usb_dev_handle *udev, int nScan, int nChan, __u16 *data)
{
  int ret = -1;
  int nbytes = nChan*nScan*2;    // number of bytes to read in 64 bit chunks
  __u16 status;

  ret = usb_bulk_read(udev, USB_ENDPOINT_IN|1, (char *) data, nbytes, HS_DELAY);
  if (ret != nbytes) {
    printf("usbAInScanRead_USB1608FS_Plus: error in usb_bulk_read.  ret = %d\n", ret);
  }

  status = usbStatus_USB1608FS_Plus(udev);
  if (status & AIN_SCAN_RUNNING) {
    printf("Analog In scan not done.\n");
    usbAInScanStop_USB1608FS_Plus(udev);
  }

  if ((status & AIN_SCAN_OVERRUN)) {
    printf("Analog AIn scan overrun.\n");
  }
  return ret;
}