コード例 #1
0
ファイル: HIDDevice.cpp プロジェクト: DeqingSun/javahidapi
JNIEXPORT jstring JNICALL Java_com_codeminders_hidapi_HIDDevice_getIndexedString
  (JNIEnv *env, jobject self, jint index)
{
    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_indexed_string(peer, index, 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;
}
コード例 #2
0
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);
}
コード例 #3
0
ファイル: usb-hid.c プロジェクト: antongulenko/TankOS
int HID_API_EXPORT_CALL hid_get_serial_number_string(hid_device *dev, wchar_t *string, size_t maxlen)
{
	return hid_get_indexed_string(dev, dev->serial_index, string, maxlen);
}
コード例 #4
0
ファイル: usb-hid.c プロジェクト: antongulenko/TankOS
int HID_API_EXPORT_CALL hid_get_product_string(hid_device *dev, wchar_t *string, size_t maxlen)
{
	return hid_get_indexed_string(dev, dev->product_index, string, maxlen);
}
コード例 #5
0
ファイル: usb-hid.c プロジェクト: antongulenko/TankOS
int HID_API_EXPORT_CALL hid_get_manufacturer_string(hid_device *dev, wchar_t *string, size_t maxlen)
{
	return hid_get_indexed_string(dev, dev->manufacturer_index, string, maxlen);
}
コード例 #6
0
ファイル: hidtest.cpp プロジェクト: 5ere2hik/hidapi
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;
}
コード例 #7
0
ファイル: elo_input.cpp プロジェクト: blow05/elo
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;
}
コード例 #8
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;
}
コード例 #9
0
ファイル: hidtest.cpp プロジェクト: andrewbeir/ME433_Beir
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;
}
コード例 #10
0
ファイル: hidtest.cpp プロジェクト: slee65/HW8
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;
}
コード例 #11
0
ファイル: hidtest.cpp プロジェクト: epshanker/Homework
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;
}
コード例 #12
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;
}