Ejemplo n.º 1
0
JNIEXPORT jstring JNICALL Java_com_codeminders_hidapi_HIDDevice_getProductString
  (JNIEnv *env, jobject self)
{
    hid_device *peer = getPeer(env, self);
    if(!peer)
    {
        throwIOException(env, peer);
        return NULL; /* not an error, freed previously */ 
    }

    wchar_t data[MAX_BUFFER_SIZE];
    int res = hid_get_product_string(peer, data, MAX_BUFFER_SIZE);
    if(res < 0)
    {
        /* We decided not to treat this as an error, but return an empty string in this case
        throwIOException(env, peer);
        return NULL;
        */
        data[0] = 0;
    }
       
    char *u8 = convertToUTF8(env, data);
    jstring string = env->NewStringUTF(u8);
    free(u8);
    
    return string;
}
void UT612ByteSource::printHidDeviceInfo(hid_device* handle)
{
	// Read the Manufacturer String
#define MAX_STR 255
	wchar_t wstr[MAX_STR];

	wstr[0] = 0x0000;
	int res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
	if (res < 0)
		std::cerr << "Unable to read manufacturer string" << std::endl;
	printf("Manufacturer String: %ls\n", wstr);

	// Read the Product String
	wstr[0] = 0x0000;
	res = hid_get_product_string(handle, wstr, MAX_STR);
	if (res < 0)
		std::cerr << "Unable to read product string" << std::endl;
	printf("Product String: %ls\n", wstr);

	// Read the Serial Number String
	wstr[0] = 0x0000;
	res = hid_get_serial_number_string(handle, wstr, MAX_STR);
	if (res < 0)
		std::cerr << "Unable to read serial number string" << std::endl;
	printf("Serial Number String: (%d) %ls", wstr[0], wstr);
	std::cout << std::endl;
}
Ejemplo n.º 3
0
void USBInterface::HIDDescription()
{
    if(HIDisOpen)
    {
        res = hid_get_manufacturer_string(DeviceHandle, wstr, MAX_STR);
        printf("Manufacturer String: %ls\n", wstr);
        res = hid_get_product_string(DeviceHandle, wstr, MAX_STR);
        printf("Product String: %ls\n", wstr);
        res =  hid_get_serial_number_string(DeviceHandle, wstr, MAX_STR);
        printf("Serial String: %ls\n", wstr);
    }
}
Ejemplo n.º 4
0
/*test et initialise une connexion hid vers vId:0x046d pId:0xc050
 * une souris usb hid logitech rx250 ;) */
bool  minicut::try_device(void)
{

    wchar_t wstr[__MNC_MAX_STR];
    QString pr;

    int res;
    unsigned char buf[65];

    if(minicut_device)
    {
        //set_connection_state("open");
        driver_real=true;//is real driver
        // Read the Manufacturer String
        res = hid_get_manufacturer_string(minicut_device, wstr, __MNC_MAX_STR);
        // printf("Manufacturer String: %ls\n", wstr);
        pr= QString::fromWCharArray(wstr);
        std::cout << "Succefully opened:"<<pr.toStdString()<<std::endl;
        // Read the Product String
        res = hid_get_product_string(minicut_device, wstr, __MNC_MAX_STR);
        //printf("Product String: %ls\n", wstr);
        pr= QString::fromWCharArray(wstr);
        //driver_path=realmnc->path;
        std::cout << "Succefully opened:"<<pr.toStdString()<<std::endl;

        //  std::cout << wstr;
        // Read the Serial Number String
        res = hid_get_serial_number_string(minicut_device, wstr, __MNC_MAX_STR);
        //std::cout << wstr;
        //printf("Serial Number String: %ls", wstr);
        //printf("\n");
        /**/
        pr= QString::fromWCharArray(wstr);
        //driver_path="/dev/minicut_virtmnc";


        //std::cout << pr.ascii();

        driver_real=true;
        // this->driver_path="/dev/null";
        //driver_path=minicut_test_device
    }

    if (teststatus())
    {return true;}
    else
    {return false;}
    return false;
}
Ejemplo n.º 5
0
int initializeDevice() {

  struct hid_device_info *devs, *cur_dev;
  const wchar_t targetManufacturer[] = L"Wincor Nixdorf";

  hid_init();
  /* Auto-detection du périphérique */
  //Wincor Nixdorf

	devs = hid_enumerate(0x0, 0x0);
	cur_dev = devs;

	while (cur_dev) {
    if (DEBUG) fprintf(stdout, "<Debug> Périphérique trouvé: %04hx %04hx - %ls - %s\n", cur_dev->vendor_id, cur_dev->product_id, cur_dev->manufacturer_string ,cur_dev->path);

    #if defined(__APPLE__)
      if (!wcscmp(targetManufacturer, cur_dev->manufacturer_string) ) {
        fprintf(stdout, "<Pilote> Afficheur USB %04hx %04hx en cours d\'ouverture sur %s..\n", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path);
        display = hid_open(cur_dev->vendor_id, cur_dev->product_id, NULL);
        break;
      }
    #elif defined(linux)
      if (!wcscmp(targetManufacturer, cur_dev->manufacturer_string) &&  !strcmp(cur_dev->path+(strlen(cur_dev->path)-2), "01")) {
        fprintf(stdout, "<Pilote> Afficheur USB %04hx %04hx en cours d\'ouverture sur %s..\n", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path);
        display = hid_open_path(cur_dev->path);
        break;
      }
    #else
      fprintf(stderr, "<Fatal> Ce système n'est pas (encore) supporté pour l'instant!\n");
      exit(-1);
    #endif

		cur_dev = cur_dev->next;
	}

	hid_free_enumeration(devs);
  if (!display) return 0;

  hid_get_product_string(display, moduleName, MAX_STR);
  if (DEBUG) fprintf(stdout, "<Info> Connected to \"%ls\" display.\n", moduleName);
  return display!=NULL;
}
void read_device_info()
{
	int res;
	wchar_t wstr[MAX_STR];

	// Read the Manufacturer String
	printf("Trying to read manufacturer string...\n");
	res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
	wprintf(L"The manufacturer string is: %ls\n", wstr);

	// Read the Product String
	res = hid_get_product_string(handle, wstr, MAX_STR);
	wprintf(L"Product String: %ls\n", wstr);

	// Read the Serial Number String
	res = hid_get_serial_number_string(handle, wstr, MAX_STR);
	wprintf(L"Serial Number String: (%d) %ls\n", wstr[0], wstr);

	// Read Indexed String 1
	res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
	wprintf(L"Indexed String 1: %ls\n", wstr);
}
Ejemplo n.º 7
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;
    }
  }
}
Ejemplo n.º 8
0
int run(){
	int res;
	unsigned char buf[128];
	#define MAX_STR 255
	wchar_t wstr[MAX_STR];
	hid_device *handle;
	int fd;
	
	memset(buf,0x00,sizeof(buf));
	
	handle = hid_open(VENDOR, PRODUCT, NULL);
	if (!handle) {
		printf("unable to open device\n");
 		return 1;
	}
	
	// Read the Manufacturer String
	wstr[0] = 0x0000;
	res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
	if (res < 0)
		printf("Unable to read manufacturer string\n");
	printf("Vendor: %ls\n", wstr);
	
	// Read the Product String
	wstr[0] = 0x0000;
	res = hid_get_product_string(handle, wstr, MAX_STR);
	if (res < 0)
		printf("Unable to read product string\n");
	printf("Product: %ls\n", wstr);
	
	fd = uinput_connect(wstr);

	// Read the Serial Number String
	wstr[0] = 0x0000;
	res = hid_get_serial_number_string(handle, wstr, MAX_STR);
	if (res < 0)
		printf("Unable to read serial number string\n");
	printf("Serial: %ls", wstr);
	printf("\n");

	// Read Indexed String 1
	wstr[0] = 0x0000;
	res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
	if (res < 0)
		printf("Unable to read indexed string 1\n");
	printf("Indexed: %ls\n", wstr);

	// Set the hid_read() function to be non-blocking.
	hid_set_nonblocking(handle, 1);
	
	res = 0;
	while (1) {
		res = hid_read(handle, buf, sizeof(buf));
		if (res>0){
		    touch(fd, buf, res);
		}else if (res<0){
		    break;
		}
		
		if (exitNow) break;
		
		usleep(10000);
	}
	
	hid_close(handle);

	/* Free static HIDAPI objects. */
	hid_exit();
	
	return 0;
}
Ejemplo n.º 9
0
int main(int argc, char* argv[])
{
	int res;
	unsigned char buf[65];
	#define MAX_STR 255
	wchar_t wstr[MAX_STR];
	hid_device *handle;
	int i;

	// Enumerate and print the HID devices on the system
	struct hid_device_info *devs, *cur_dev;
	
	devs = hid_enumerate(0x0, 0x0);
	cur_dev = devs;	
	while (cur_dev) {
		printf("Device Found\n  type: %04hx %04hx\n  path: %s\n  serial_number: %ls",
			cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
		printf("\n");
		printf("  Manufacturer: %ls\n", cur_dev->manufacturer_string);
		printf("  Product:      %ls\n", cur_dev->product_string);
		printf("\n");
		cur_dev = cur_dev->next;
	}
	hid_free_enumeration(devs);


	// Open the device using the VID, PID,
	// and optionally the Serial number.
	handle = hid_open(0x8021, 0x1941, NULL);

	// Read the Manufacturer String
	res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
	printf("Manufacturer String: %ls\n", wstr);

	// Read the Product String
	res = hid_get_product_string(handle, wstr, MAX_STR);
	printf("Product String: %ls\n", wstr);

	// Read the Serial Number String
	res = hid_get_serial_number_string(handle, wstr, MAX_STR);
	printf("Serial Number String: %ls", wstr);
	printf("\n");

	// Send a Feature Report to the device
	buf[0] = 0x2; // First byte is report number
	buf[1] = 0xa0;
	buf[2] = 0x0a;
	res = hid_send_feature_report(handle, buf, 17);

	// Read a Feature Report from the device
	buf[0] = 0x2;
	res = hid_get_feature_report(handle, buf, sizeof(buf));

	// Print out the returned buffer.
	printf("Feature Report\n   ");
	for (i = 0; i < res; i++)
		printf("%02hhx ", buf[i]);
	printf("\n");

	// Set the hid_read() function to be non-blocking.
	hid_set_nonblocking(handle, 1);

	// Send an Output report to toggle the LED (cmd 0x80)
	buf[0] = 1; // First byte is report number
	buf[1] = 0x80;
	res = hid_write(handle, buf, 65);

	// Send an Output report to request the state (cmd 0x81)
	buf[1] = 0x81;
	hid_write(handle, buf, 65);

	// Read requested state
	res = hid_read(handle, buf, 65);
	if (res < 0)
		printf("Unable to read()\n");

	// Print out the returned buffer.
	for (i = 0; i < res; i++)
		printf("buf[%d]: %d\n", i, buf[i]);

	return 0;
}
int main(int argc, char* argv[])
{
	int res;
	unsigned char buf[65];
	wchar_t wstr[MAX_STR];
	hid_device *handle;
	
	int i;
	
	// Initialize the hidapi library
	res = hid_init();

	// Open the device using the VID, PID,
	// and optionally the Serial number.
	handle = hid_open(0x4d8, 0x3f, NULL);

	// Read the Manufacturer String
	res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
	wprintf(L"Manufacturer String: %s\n", wstr);

	// Read the Product String
	res = hid_get_product_string(handle, wstr, MAX_STR);
	wprintf(L"Product String: %s\n", wstr);

	// Read the Serial Number String
	res = hid_get_serial_number_string(handle, wstr, MAX_STR);
	wprintf(L"Serial Number String: (%d) %s\n", wstr[0], wstr);

	// Read Indexed String 1
	res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
	wprintf(L"Indexed String 1: %s\n", wstr);

	// Toggle LED (cmd 0x80). The first byte is the report number (0x0).
	buf[0] = 0x0;
	buf[1] = 0x80;
	res = hid_write(handle, buf, 65);



	unsigned char message[27];
	//Get user input with scanf ; where message is a character array
	buf[1] = 0x82;
	printf("%s", "Please enter the row number : ");
	scanf("%s", &buf[2]);
	printf("%s", "Please enter a sentence : ");
	scanf("%s", message);
	
	for (i = 0; i < 27; i++)
	{
		buf[i+3] = message[i];
	}
	
	res = hid_write(handle, buf, 65);
	
	
	// Request state (cmd 0x81). The first byte is the report number (0x0).

	buf[0] = 0x0;
	buf[1] = 0x81;
	res = hid_write(handle, buf, 65);

	// Read requested state
	res = hid_read(handle, buf, 65);
	
	
	
	
/*        
    short accels[3];
    
	accels[0] = (buf[2]<<8)|(buf[1]&0xff);
    accels[1] = (buf[4]<<8)|(buf[3]&0xff);
    accels[2] = (buf[6]<<8)|(buf[5]&0xff);
    
    for(i = 0; i<3;i++)
    	wprintf(L"accel[%d]: %d\n",i,accels[i]);

	short x,y,z;
	x = (buf[4]<<8)|(buf[3]);
	y = (buf[6]<<8)|(buf[5]);
	z = (buf[8]<<8)|(buf[7]);
	
	double xx,yy,zz;
	xx=((float)x)/16000;
	yy=((float)y)/16000;
	zz=((float)z)/16000;
	
	// Print out the returned buffer.
	printf("%f \n %f \n %f \n", xx, yy, zz);
	*/
	
	short x[DATAPNTS];
	short y[DATAPNTS];
	short z[DATAPNTS];
			
	FILE *ofp;
	
	ofp = fopen("accels.txt", "w");
	
	for (i = 0; i < DATAPNTS ; i ++ ) {
		
		
		
		res = hid_write(handle, buf, 65);
		res = hid_read(handle, buf, 65);
		
		x[i] = (buf[2]<<8)|(buf[1]&0xff);
    	y[i] = (buf[4]<<8)|(buf[3]&0xff);
    	z[i] = (buf[6]<<8)|(buf[5]&0xff);
		
		fprintf(ofp, "your data \r\n %d\r\n %d\r\n %d\r\n", x[i], y[i], z[i]);
	}
	
	fclose(ofp);
	

	// Finalize the hidapi library
	res = hid_exit();

	return 0;
}
Ejemplo n.º 11
0
int main(int argc, char* argv[])
{
    int res;
    unsigned char buf[65];
    wchar_t wstr[MAX_STR];
    hid_device *handle;
    int i;
    
    int j = 0;
    short accX[DATA_PTS];
    short accY[DATA_PTS];
    short accZ[DATA_PTS];
    
    // Initialize the hidapi library
    res = hid_init();
    
    // Open the device using the VID, PID,
    // and optionally the Serial number.
    handle = hid_open(0x4d8, 0x3f, NULL);
    
    // Read the Manufacturer String
    res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
    wprintf(L"Manufacturer String: %s\n", wstr);
    
    // Read the Product String
    res = hid_get_product_string(handle, wstr, MAX_STR);
    wprintf(L"Product String: %s\n", wstr);
    
    // Read the Serial Number String
    res = hid_get_serial_number_string(handle, wstr, MAX_STR);
    wprintf(L"Serial Number String: (%d) %s\n", wstr[0], wstr);
    
    // Read Indexed String 1
    res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
    wprintf(L"Indexed String 1: %s\n", wstr);
    
    char message[MAX_MSG];
    wprintf(L"Input message:\n");
    wscanf(L"%[^\n]s", message);
    
    int row=0;
    wprintf(L"Input row:\n", wstr);
    wscanf(L"%d", &row);
    
    // Toggle LED (cmd 0x80). The first byte is the report number (0x0).
    buf[0] = 0x0;
    buf[1] = 0x80;
    buf[3] = row;
    for (i = 0; i < MAX_MSG; i++)
        buf[i+5] = message[i];
    res = hid_write(handle, buf, 65);
    
    // Request state (cmd 0x81). The first byte is the report number (0x0).
    buf[0] = 0x0;
    buf[1] = 0x81;
    res = hid_write(handle, buf, 65);
    
    // Read requested state
    //res = hid_read(handle, buf, 65);
    
    // Print out the returned buffer.
    //for (i = 0; i < 7; i++)
       // printf("buf[%d]: %d\n", i, buf[i]);
    
    // Read requested state
    while (j < DATA_PTS)
    {
        buf[0] = 0x0;
        buf[1] = 0x81;
        res = hid_write(handle, buf, 65);
        res = hid_read(handle, buf, 65);
        if (buf[0] == 0x81)
        {
            accX[j] = ((buf[1] << 8) | (buf[2]));
            accY[j] = ((buf[3] << 8) | (buf[4]));
            accZ[j] = ((buf[5] << 8) | (buf[6]));
            j++;
        }
    }
    
    //Save to a file
    wprintf(L"Saving to file\n");
    FILE *ofp;
    ofp = fopen("accels.txt", "w");
    for (i=0; i<DATA_PTS; i++) {
        wprintf(L"X: %d  Y: %d  Z: %d\r\n",accX[i],accY[i],accZ[i]);
        fwprintf(ofp,L"%d, %d, %d\r\n",accX[i],accY[i],accZ[i]);
    }
    fclose(ofp);
    
    // Finalize the hidapi library
    res = hid_exit();
    
    for (i = 0; i<65; i++)
        buf[i] = 0;
    
    return 0;
}
Ejemplo n.º 12
0
int main(int argc, char* argv[])
{
	FILE *ofp;
	int res;
	unsigned char buf[65];
	wchar_t wstr[MAX_STR];
	hid_device *handle;
	int i,k=0,buf_len;
	char message[50];
	short accels[3],x[50],y[50],z[50];
	// Initialize the hidapi library
	res = hid_init();

	// Open the device using the VID, PID,
	// and optionally the Serial number.
	handle = hid_open(0x4d8, 0x3f, NULL);

	// Read the Manufacturer String
	res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
	wprintf(L"Manufacturer String: %s\n", wstr);

	// Read the Product String
	res = hid_get_product_string(handle, wstr, MAX_STR);
	wprintf(L"Product String: %s\n", wstr);

	// Read the Serial Number String
	res = hid_get_serial_number_string(handle, wstr, MAX_STR);
	wprintf(L"Serial Number String: (%d) %s\n", wstr[0], wstr);

	// Read Indexed String 1
	res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
	wprintf(L"Indexed String 1: %s\n", wstr);

	// Toggle LED (cmd 0x80). The first byte is the report number (0x0).
	buf[0] = 0x0;
	buf[1] = 0x80;
	
	//char message[20];
	printf("Enter row: \n");
	scanf("%s",message);

	while(message[k]){
		buf[2+k]=message[k];
		k++;
	}

	buf_len=strlen(message)+2;
	printf("Enter String \n");
	scanf("%s",message);
	buf[buf_len]=strlen(message);
	buf_len=buf_len+1;

	k=0;
	while(message[k]){
		buf[buf_len+k]=message[k];
		k++;
	}

		/*for (j=0;j<strlen(message);j++){
		buf[j+2]=message[j];
		printf("buf[%d]: %d,%d \n", j+2, buf[j+2],strlen(message));*/
	
	

	res = hid_write(handle, buf, 65);

	int j=0;
	while(j<50){
	// Request state (cmd 0x81). The first byte is the report number (0x0).
	buf[0] = 0x0;
	buf[1] = 0x81;

//for (i = 0; i < 65; i++)
//		printf("buf[%d]: %d\n", i, buf[i]);


	res = hid_write(handle, buf, 65);
	// Read requested state
	res = hid_read(handle, buf, 65);

	if (buf[1]){
		for (i=0;i<3;i++){
			accels[i]=makeshort(buf, 2*i+2);
		}
		x[j]=accels[0];
		y[j]=accels[1];
		z[j]=accels[2];
		j++;
	}

	// Print out the returned buffer.
	//for (i = 0; i < 65; i++)
	//	printf("buf[%d]: %d\n", i, buf[i]);

	// Finalize the hidapi library
	res = hid_exit();
}

ofp=fopen("accels.txt","w");
	for(i=0; i<50; i++){
		fprintf(ofp,"%d  %d  %d \r\n",x[i],y[i],z[i]);
	}

	return 0;
}
Ejemplo n.º 13
0
/////////////////////////////////////////////////////////////////////////////////////////////
// Scan /dev looking for hidraw devices and then check to see if each is a Rift
// nthDevice is 0-based
/////////////////////////////////////////////////////////////////////////////////////////////
Device * openRiftHID( int nthDevice, Device *myDev )
{
    BOOLEAN didAlloc = FALSE;
	struct hid_device_info *devs, *cur_dev;
	wchar_t wstr[MAX_STR];
    Device *dev = myDev;
	
    // Iterate over the devices matching our IDs
	devs = hid_enumerate(0x2833, 0x0001);
	cur_dev = devs;	
	while (cur_dev) {
        // Skip to the one we want
        if( nthDevice-- )
        {
            cur_dev = cur_dev->next;
            continue;
        }

        // Allocate space if we need to
        if (! dev )
        {
            dev = (Device *)malloc(sizeof(Device));
            didAlloc = TRUE;
        }

        // Open the device
        dev->hidapi_dev = (hid_device *)hid_open(cur_dev->vendor_id, 
                cur_dev->product_id, cur_dev->serial_number);

        // Did we fail to open the device?
        if( !dev->hidapi_dev )
        {
            // Yep. Clean up
            if( didAlloc )
            {
                free(dev);
            }
            return 0;
        }

        // Save the vendor and product IDs (not that we need them)
        dev->vendorId = cur_dev->vendor_id;
        dev->productId = cur_dev->product_id;

        //printf("\n\nDevice Found:\n");

        // Read the Manufacturer String
        hid_get_manufacturer_string(dev->hidapi_dev, wstr, MAX_STR);
        //printf("Manufacturer:  %ls\n", wstr);
        dev->name = malloc(sizeof(wchar_t) * wcslen(wstr) + 1);
        wcstombs(dev->name, wstr, wcslen(wstr));

        // Read the Product String
        hid_get_product_string(dev->hidapi_dev, wstr, MAX_STR);
        //printf("Product:       %ls\n", wstr);
        dev->product = malloc(sizeof(wchar_t) * wcslen(wstr) + 1);
        wcstombs(dev->product, wstr, wcslen(wstr));

        // Read the Serial Number String
        hid_get_serial_number_string(dev->hidapi_dev, wstr, MAX_STR);
        //printf("Serial Number: %ls", wstr);
        dev->serial = malloc(sizeof(wchar_t) * wcslen(wstr) + 1);
        wcstombs(dev->serial, wstr, wcslen(wstr));

        //printf("\n\n");
        break;
	}
	hid_free_enumeration(devs);

    if ( dev && dev->hidapi_dev > 0 )
    {
        // Make our device nonblocking
        hid_set_nonblocking(dev->hidapi_dev, 1);

        // Init the Rift sensor info
        if( ! getSensorInfo( dev ) )
        {
            // Clean up
            if( didAlloc )
            {
                free(dev);
            }
            dev = 0;
        }
    }
    else
    {
        // Clean up
        if ( didAlloc )
        {
            free(dev);
        }
        dev = 0;
    }

    return dev;
}
Ejemplo n.º 14
0
int main(int argc, char* argv[])
{
		int res;
		unsigned char buf[65];
		wchar_t wstr[MAX_STR];
		hid_device *handle;
		int i;

		// Initialize the hidapi library
		res = hid_init();

		// Open the device using the VID, PID,
		// and optionally the Serial number.
		handle = hid_open(0x4d8, 0x3f, NULL);

		// Read the Manufacturer String
		res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
		wprintf(L"Manufacturer String: %s\n", wstr);

		// Read the Product String
		res = hid_get_product_string(handle, wstr, MAX_STR);
		wprintf(L"Product String: %s\n", wstr);

		// Read the Serial Number String
		res = hid_get_serial_number_string(handle, wstr, MAX_STR);
		wprintf(L"Serial Number String: (%d) %s\n", wstr[0], wstr);

		// Read Indexed String 1
		res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
		wprintf(L"Indexed String 1: %s\n", wstr);


		int request,row;
		char message[62];
		scanf("%d %s", &row, &message);
		for (request=0;request<200;request++) {



		// Toggle LED (cmd 0x80). The first byte is the report number (0x0).
		int ii=0;
		buf[0] = 0x0;
		buf[1] = 0x80;
		//scanf("%d %s", &row, &message);
		buf[2] = row;
		//scanf("%c", &message);
		for (ii=0;ii<62;ii++) {
			buf[ii+3] = message[ii];
		}
		res = hid_write(handle, buf, 65);

		// Request state (cmd 0x81). The first byte is the report number (0x0).
		buf[0] = 0x0;
		buf[1] = 0x81;
		res = hid_write(handle, buf, 65);

		// Read requested state
		res = hid_read(handle, buf, 65);

		// buffer 0-->read, buffer 1-->x high, buffer 2-->x low, buffer 3-->y high, buffer 4-->y low, buffer 5-->z high, buffer 6-->z low
		short accels[3];
		accels[0] = (buf[1]<<8)|buf[2];
		accels[1] = (buf[3]<<8)|buf[4];
		accels[2] = (buf[5]<<8)|buf[6];

		// Print out the returned buffer.
		//for (i = 0; i <= 6; i++) {
		//	printf("buf[%d]: %d\n", i, buf[i]);
		//}
		//printf("***READING #%d***\nx accel: %d\ny accel: %d\nz accel: %d\n\n",(request+1),accels[0],accels[1],accels[2]);

		// Finalize the hidapi library
		res = hid_exit();

		// ADD CODE THAT SAVES X Y AND Z DATA TO ARRAYS

		accX[request] = accels[0];
		accY[request] = accels[1];
		accZ[request] = accels[2];

	}
		printf("***%d SUCCESSFUL READS***\n",(request));

		int jj;
		FILE *dataFile;
		dataFile = fopen("accels.txt","w");
		for (jj=0;jj<200;jj++) {
			fprintf(dataFile,"%d    %d    %d\r\n",accX[jj],accY[jj],accZ[jj]);
		}
		fclose(dataFile);

		printf("data (hopefully) sent to some file\n\n");

		return 0;
}
Ejemplo n.º 15
0
int main(int argc, char* argv[])
{
    int res;
    unsigned char buf[63];
    wchar_t wstr[MAX_STR];
    hid_device *handle;
    int i;
    struct hid_device_info *devs, *cur_dev;

    //Init the hidapi lib
    if(!hid_init()==0){
        printf("Error hidinit\n");
        return -1;
    }
/*
    devs=hid_enumerate(0x0,0x0);
    if (devs==NULL){
        printf("No custom HID devices\n");
        return -1;
    }
    cur_dev=devs;
    while (cur_dev){
        printf("Device found\n type: %04hx %04hx path: %s\n serial number: %ls",
               cur_dev->vendor_id,
               cur_dev->product_id,
               cur_dev->path,
               cur_dev->serial_number);
        printf("\n");
        printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
        printf(" Product: %ls\n", cur_dev->product_string);
        printf(" Release: %d\n", cur_dev->release_number);
        printf(" Interface: %d\n", cur_dev->interface_number);
        printf("\n");
        cur_dev=cur_dev->next;
    }
    hid_free_enumeration(devs);
*/
    //open the device using VID PID and optionaly Serial number
    ////handle = hid_open(0x4d8, 0x3f, L"12345");
    //online guid gen https://www.guidgenerator.com/online-guid-generator.aspx
    handle=hid_open(VID,PID,L"9088a05a-bc89-4c60-9c2a-4d80e86a385d");
    //handle=hid_open(VID,PID,NULL);
    if (!handle){
        printf("Unable to open device");
        return 1;
    }
        //read the manufacturer string
        res=hid_get_manufacturer_string(handle,wstr,MAX_STR);
        if (res<0){
            printf("Unable to read manufacturer string\n");
        }
        wprintf(L"Manufacturer string: %s\n",wstr);

        //read the product string
        res=hid_get_product_string(handle,wstr,MAX_STR);
        if (res<0){
            printf("Unable to read product string\n");
        }
        wprintf(L"Product string: %ls \n",wstr);

        //read the serial number string
        res=hid_get_serial_number_string(handle,wstr,MAX_STR);
        if(res<0){
            printf("Unable to read serial number string\n");
        }
        wprintf(L"Serial number string: %ls \n",wstr);

        //set the hid_read function to non-blocking
        hid_set_nonblocking(handle, true);

        //try to read from device
        /*while(1){
            res=hid_read(handle,buf,sizeof(buf));

                //print the returned buffer
                if (res>0){
                    printf("Data read:\n");
                    for (int j=0;j<res;j++){
                        printf("%02hx ",buf[j]);
                    }
                    printf("\n");
                }
                else{
                    if (res==0){
                        printf("Error data read: null data given\n");
                    }else{
                    printf("Error data read: %ls \n", hid_error(handle));
                    }
                }
        }*/

        //try to write to device
        unsigned char tmp_buf[64];
        memset(tmp_buf,0x0,sizeof(tmp_buf));
        while(1){
                tmp_buf[0]=0x1;
                tmp_buf[63]=0x10;
                res=hid_write(handle,tmp_buf,sizeof(tmp_buf));

                if (res<0){
                    //printf("Unable to write()\n");
                    printf("Error write: %ls", hid_error(handle));
                    printf("\n");
                }
                else{
                    printf("write() OK\n");
                }



                res=hid_read(handle,buf,sizeof(buf));

                //print the returned buffer
                if (res>0){
                    printf("Data read:\n");
                    for (int j=0;j<res;j++){
                        printf("%02hx ",buf[j]);
                    }
                    printf("\n");
                }
                else{
                    if (res==0){
                        printf("Error data read: null data given\n");
                    }else{
                    printf("Error data read: %ls \n", hid_error(handle));
                    }
                }

                Sleep(500);
        }


        int delay=500;
        memset (buf,0x0,sizeof(buf)); //заполняем buf 0
        unsigned char buf1[63];
        memset (buf1,0x0,sizeof(buf1));
        int res1=0;

        while(1){
                //toggle LED (cmd 0x80) The first byte is the report number (0x1)
                for (int i=0;i<4;i++){

                buf[0] = 0x1;
                buf[1] = i;
                res=hid_write(handle,buf,sizeof(buf));
                if (res<0){
                    printf("Unable to write()\n");
                    printf("Error: %ls \n", hid_error(handle));
                }

                res1=hid_read(handle,buf1,sizeof(buf1));

                //print the returned buffer
                if (res1>0){
                    printf("Data read:\n");
                    for (int j=0;j<res1;j++){
                        printf("%02hx ",buf1[j]);
                    }
                    printf("\n");
                }
                else{
                    if (res1==0){
                        printf("Error data read: null data given\n");
                    }else{
                    printf("Error data read: %ls \n", hid_error(handle));
                    }
                }

                Sleep(delay);
                }
        }

        //request state (cmd 0x81) the first byte is the report number (0x0)
        buf[0]=0x0;
        buf[1]=0x81;
        res=hid_write(handle,buf,16);
        if (res<0){
            printf("Unable to write 2\n");
        }

        //read requested state for non-blocking
        res=0;
        while(res==0){
            res=hid_read(handle,buf,sizeof(buf));
            if (res==0){
                printf("Waiting...\n");
            }
            if (res<0){
                printf("Unable to read\n");
            }
                Sleep(500);

        }

        //print the returned buffer
        printf("Data read:\n");
        for (i=0;i<res;i++){
            printf("%02hx ",buf[i]);
        }
        printf("\n");

        hid_close(handle);
        //finalize the hidapi lib
        res=hid_exit();

        #ifdef _WIN32
            system("pause");
        #endif // _WIN32
    return 0;
}
Ejemplo n.º 16
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;
    }
  }
}
Ejemplo n.º 17
0
int main(int argc, char* argv[])
{
	int res;
	unsigned char buf[256];
	#define MAX_STR 255
	wchar_t wstr[MAX_STR];
	hid_device *handle;
	int i;

#ifdef WIN32
	UNREFERENCED_PARAMETER(argc);
	UNREFERENCED_PARAMETER(argv);
#endif

	struct hid_device_info *devs, *cur_dev;
	
	if (hid_init())
		return -1;

	devs = hid_enumerate(0x0, 0x0);
	cur_dev = devs;	
	while (cur_dev) {
		printf("Device Found\n  type: %04hx %04hx\n  path: %s\n  serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
		printf("\n");
		printf("  Manufacturer: %ls\n", cur_dev->manufacturer_string);
		printf("  Product:      %ls\n", cur_dev->product_string);
		printf("  Release:      %hx\n", cur_dev->release_number);
		printf("  Interface:    %d\n",  cur_dev->interface_number);
		printf("\n");
		cur_dev = cur_dev->next;
	}
	hid_free_enumeration(devs);

	// Set up the command buffer.
	memset(buf,0x00,sizeof(buf));
	buf[0] = 0x01;
	buf[1] = 0x81;
	

	// Open the device using the VID, PID,
	// and optionally the Serial number.
	////handle = hid_open(0x4d8, 0x3f, L"12345");
	handle = hid_open(0x4d8, 0x3f, NULL);
	if (!handle) {
		printf("unable to open device\n");
 		return 1;
	}

	// Read the Manufacturer String
	wstr[0] = 0x0000;
	res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
	if (res < 0)
		printf("Unable to read manufacturer string\n");
	printf("Manufacturer String: %ls\n", wstr);

	// Read the Product String
	wstr[0] = 0x0000;
	res = hid_get_product_string(handle, wstr, MAX_STR);
	if (res < 0)
		printf("Unable to read product string\n");
	printf("Product String: %ls\n", wstr);

	// Read the Serial Number String
	wstr[0] = 0x0000;
	res = hid_get_serial_number_string(handle, wstr, MAX_STR);
	if (res < 0)
		printf("Unable to read serial number string\n");
	printf("Serial Number String: (%d) %ls", wstr[0], wstr);
	printf("\n");

	// Read Indexed String 1
	wstr[0] = 0x0000;
	res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
	if (res < 0)
		printf("Unable to read indexed string 1\n");
	printf("Indexed String 1: %ls\n", wstr);

	// Set the hid_read() function to be non-blocking.
	hid_set_nonblocking(handle, 1);
	
	// Try to read from the device. There shoud be no
	// data here, but execution should not block.
	res = hid_read(handle, buf, 17);

	// Send a Feature Report to the device
	buf[0] = 0x2;
	buf[1] = 0xa0;
	buf[2] = 0x0a;
	buf[3] = 0x00;
	buf[4] = 0x00;
	res = hid_send_feature_report(handle, buf, 17);
	if (res < 0) {
		printf("Unable to send a feature report.\n");
	}

	memset(buf,0,sizeof(buf));

	// Read a Feature Report from the device
	buf[0] = 0x2;
	res = hid_get_feature_report(handle, buf, sizeof(buf));
	if (res < 0) {
		printf("Unable to get a feature report.\n");
		printf("%ls", hid_error(handle));
	}
	else {
		// Print out the returned buffer.
		printf("Feature Report\n   ");
		for (i = 0; i < res; i++)
			printf("%02hhx ", buf[i]);
		printf("\n");
	}

	memset(buf,0,sizeof(buf));

	// Toggle LED (cmd 0x80). The first byte is the report number (0x1).
	buf[0] = 0x1;
	buf[1] = 0x80;
	res = hid_write(handle, buf, 17);
	if (res < 0) {
		printf("Unable to write()\n");
		printf("Error: %ls\n", hid_error(handle));
	}
	

	// Request state (cmd 0x81). The first byte is the report number (0x1).
	buf[0] = 0x1;
	buf[1] = 0x81;
	hid_write(handle, buf, 17);
	if (res < 0)
		printf("Unable to write() (2)\n");

	// Read requested state. hid_read() has been set to be
	// non-blocking by the call to hid_set_nonblocking() above.
	// This loop demonstrates the non-blocking nature of hid_read().
	res = 0;
	while (res == 0) {
		res = hid_read(handle, buf, sizeof(buf));
		if (res == 0)
			printf("waiting...\n");
		if (res < 0)
			printf("Unable to read()\n");
		#ifdef WIN32
		Sleep(500);
		#else
		usleep(500*1000);
		#endif
	}

	printf("Data read:\n   ");
	// Print out the returned buffer.
	for (i = 0; i < res; i++)
		printf("%02hhx ", buf[i]);
	printf("\n");

	hid_close(handle);

	/* Free static HIDAPI objects. */
	hid_exit();

#ifdef WIN32
	system("pause");
#endif

	return 0;
}
Ejemplo n.º 18
0
/**
*** Main()
**/
int main(int argc, char* argv[])
{
  int res, verbose = 0, do_read = 0, do_write=1, do_interactive = 0, vid = FAN_VID, pid = FAN_PID, report_size=FAN_SIZE;
  int serial = 0;
  char *name;
  unsigned char buf[256], program[2 * FAN_STEPS];
  #define MAX_STR 255
  wchar_t wstr[MAX_STR];
  hid_device *handle;
  int i;

  #ifdef WIN32
  UNREFERENCED_PARAMETER(argc);
  UNREFERENCED_PARAMETER(argv);
  #endif


  // parse the command-line
  for(int i=1; i<argc; i++)
  {
    if ( !strncmp(argv[i], "--vid=", 6) ) sscanf(argv[i]+6, "%X",  &vid);
    if ( !strncmp(argv[i], "--pid=", 6) ) sscanf(argv[i]+6, "%X",  &pid);
    if ( !strcmp(argv[i], "--verbose") ) verbose = 1;
    if ( !strcmp(argv[i], "--read") ) do_read = 1; 
    if ( !strcmp(argv[i], "--help") ) 
    {
      printf("fanbot: control fanbot via USB HID communication\n" 
       "kekbot.org - rev 1.0 - " __DATE__ " " __TIME__ "\n"
       "USE: \n"
       "  fanbot [options] [data]\n"
       "  --vid=0xABCD      VID to use (default=0x%4.4X)\n"
       "  --pid=0xABCD      PID to use (default=0x%4.4X)\n"
       "  --verbose         Show more information\n",
       FAN_VID, FAN_PID, FAN_SIZE
      );
      exit(1);
    }
  }
  
  // Wait for connection
  while( 1 )
  {
    if ( verbose ) printf("Wait for connection...\n");
    handle = hid_open(vid, pid, NULL);
    if ( handle ) break; // we have a connection
    Sleep(500);    
    // usleep(1000*100);
  }
  hid_flush(handle);
  

  // Read serial# and name
  buf[1] = 0;
  res = hid_read(handle, buf, sizeof(buf));
  memcpy(&serial, &buf[8], sizeof(serial) );
  name = (char*)&buf[12];
  printf("CONNECT %X %s\n", serial, name);
  
  memset(buf, 0, sizeof(buf));
  
  read_program(handle, program);
  print_program(program);
  
  if ( verbose ) 
  {
    // Read the Manufacturer String
    wstr[0] = 0x0000;
    res = hid_get_manufacturer_string(handle, wstr, MAX_STR);
    if (res < 0)
      printf("Unable to read manufacturer string\n");
    else
      printf("Manufacturer String: %ls\n", wstr);
    // Read the Product String
    wstr[0] = 0x0000;
    res = hid_get_product_string(handle, wstr, MAX_STR);
    if (res < 0)
      printf("Unable to read product string\n");
    else
      printf("Product String: %ls\n", wstr);

    // Read the Serial Number String
    wstr[0] = 0x0000;
    res = hid_get_serial_number_string(handle, wstr, MAX_STR);
    if (res < 0)
      printf("Unable to read serial number string\n");
    else
      printf("Serial Number String: (%d) %ls", wstr[0], wstr);
    printf("\n");

    // Read Indexed String 1
    wstr[0] = 0x0000;
    res = hid_get_indexed_string(handle, 1, wstr, MAX_STR);
    if (res < 0)
      printf("Unable to read indexed string 1\n");
    else
      printf("Indexed String 1: %ls\n", wstr);
  }
  
#define BEGIN() pch = strtok (str,",. ")
#define NEXT() pch = strtok (NULL,",. ")
#define COMMAND(s) (!strncmp(pch, s, sizeof(s)-1))

  while ( 1 )
  {
    char str[MAX_STR], *pch;
    
    fgets(str , MAX_STR-1, stdin);
    if ( strlen(str) < 1 ) continue;
    BEGIN();
    
    if ( COMMAND("SET") )
    {
      int leds=0, s1=0, s2=0;
      if ( (NEXT()) != NULL ) sscanf(pch, "%d", &leds);
      if ( (NEXT()) != NULL ) sscanf(pch, "%d", &s1);
      if ( (NEXT()) != NULL ) sscanf(pch, "%d", &s2);    
      buf[1] = CMD_SET;
      buf[2] = leds;
      buf[3] = s1;
      buf[4] = s2;
      res = hid_write(handle, buf, report_size);
    }
    else if ( COMMAND("PLAY") )
    {
      int n = 1;
      buf[1] = CMD_PLAY;
      if ( (NEXT()) != NULL ) sscanf(pch, "%d", &n);
      buf[2] = n;
      res = hid_write(handle, buf, report_size);
    }
    else if ( COMMAND("PROGRAM") )
    {
      int i = 2, val = 0;
      memset(buf,0,sizeof(buf));
      buf[1] = CMD_PROGRAM;
      while ( (NEXT()) != NULL )
      {
        sscanf(pch, "%d", &val);
        buf[i++] = val;     
     }
     res = hid_write(handle, buf, report_size);
    }
    else if ( COMMAND("NAME") )
    {
      buf[1] = CMD_NAME;
      memset(&buf[2], 0, 32);
      for(i=0; i<32 && str[i+5] != 0 && str[i+5] != '\n' && str[i+5] != '\r'; i++)
        buf[i+2] = (unsigned char) str[i+5];
      res = hid_write(handle, buf, report_size);
    }
    else if ( COMMAND("STOP") )
    {
      buf[1] = CMD_STOP;
      res = hid_write(handle, buf, report_size);
    }
    else if ( COMMAND("READ") )  // read memory content
    {
      res = read_program(handle, program);
      if ( res < 0 )
        break;
      print_program(program);
    }
    else if ( COMMAND("QUIT") )
    {
      break; 
    }
    else
      buf[1] = CMD_NONE;
  
    if (res < 0) 
    {
      printf("ERROR: Unable to write(), '%ls'\n", hid_error(handle));
      exit(1);
    }
    
  }

  printf("DISCONNECT\n");  
  
  hid_close(handle); 
  hid_exit();/* Free static HIDAPI objects. */
  
  return res;
}