About readin data from USB

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
//#include <windows.h>
//#include <atlbase.h>
//#include <comdef.h>
#include <string>
//#include <cstring>
#include <lusb0_usb.h>
#include <stdio.h>
#include <tchar.h>
#include "Resource.h"
#include <iostream>
#define ep 0x16

using namespace std;
struct usb_bus *busses;
struct usb_dev_handle *handlTAOS;
int a;
int b;
//char tmp[BUF_SIZE];
char *rd_tmp;
struct libusb_device_descriptor *desc;

std::string GetLastErrorStdStr(void);

int main (void){   
	usb_init();
	usb_set_debug(255);
	usb_find_busses();
	usb_find_devices();
    
	busses = usb_get_busses();

	struct usb_bus *bus;
	int c, i, a;

	/* ... */
	a=0;
	for (bus = busses; bus; bus = bus->next) {
		struct usb_device *dev;
		printf("%i\n",a);
		a++;
    
		for (dev = bus->devices; dev; dev = dev->next) {
    		/* Check if this device is a printer */
    		if (dev->descriptor.idProduct == 64258) {
    			/* Open the device, claim the interface and do your processing */
				printf("Found TAOS by idProduct--\n");
				handlTAOS=usb_open(dev);
				b=0;
				b=usb_set_configuration(handlTAOS, 1);
				if (b==0)
				{
					printf("Configuring success\n");
				}
				else
				{
					printf("error configuring:\n%s\n", usb_strerror());
				}
				
				b=1;
				b=usb_clear_halt(handlTAOS, 0x81);
				if (b==0)
				{
					printf("Clear success %i\n", b);
				}
				b=0;
				//b=usb_claim_interface(handlTAOS, 0);
				b=usb_claim_interface(handlTAOS, dev->config->interface->altsetting->bInterfaceNumber);
				if(b==0) 
					printf("Interface claimed\n");
				else 
				{
					printf("iterface claim FAIL %i\n", b);
					//printf("Last Erroe: %s\n",GetLastErrorStdStr());
					cout << "Error:: " << GetLastErrorStdStr() <<endl;
				}

				b=0;
				b=usb_interrupt_read(handlTAOS, 0x81, rd_tmp, 8, 500);
				if (b>0)
				{
					printf("Read success %i\n", b);
					
				}
				else
				{
					printf("%i error reading: %s\n", b, usb_strerror());
					cout << "Error read: " << GetLastErrorStdStr() <<endl;
				}
				b=0;
				/*b=usb_interrupt_read(handlTAOS, 0x01, rd_tmp, 2, 500);
				if (b>0)
				{
					printf("Read success: %i\n", b);
				}
				else
				{
					printf("%i error reading:\n%s\n", b, usb_strerror());
				}*/
				/*for (int i = 0; i < 130; i++)
				{
					b=usb_interrupt_read(handlTAOS, i, rd_tmp, 32, 500);
					if (b>0)
					{
						printf("Read success: %i\n", b);
					}
				}*/
    		}
    
    		/* Loop through all of the configurations */
    		for (c = 0; c < dev->descriptor.bNumConfigurations; c++) {
    			/* Loop through all of the interfaces */
				if(dev->descriptor.bNumConfigurations == 1)
					printf("Found TAOS by bNumConfigurations\n");
    			for (i = 0; i < dev->config[c].bNumInterfaces; i++) {
					if(dev->config[c].bNumInterfaces == 1)
						printf("Found TAOS by bNumInterfaces\n");
    				/* Loop through all of the alternate settings */
    				for (a = 0; a < dev->config[c].interface[i].num_altsetting; a++) {
    					/* Check if this interface is a printer */
    					if (dev->config[c].interface[i].altsetting[a].bInterfaceClass == 3) {
    					/* Open the device, set the alternate setting, claim the interface and do your processing */
							printf("Found TAOS by bInterfaceClass\n");
							handlTAOS=usb_open(dev);
							b=0;
							b=usb_set_configuration(handlTAOS, 1);
							if (b==0)
							{
								printf("Configuring success\n");
							}
							else
							{
								printf("error configuring:\n%s\n", usb_strerror());
							}
				
							b=1;
							b=usb_clear_halt(handlTAOS, 0x81);
							if (b==0)
							{
								printf("Clear success %i\n", b);
							}
							b=0;
							//b=usb_claim_interface(handlTAOS, 0);
							b=usb_claim_interface(handlTAOS, dev->config->interface->altsetting->bInterfaceNumber);
							if(b==0) 
								printf("Interface claimed\n");
							else 
							{
								printf("iterface claim FAIL %i\n", b);
								//printf("Last Erroe: %s\n",GetLastErrorStdStr());
								cout << "Error:: " << GetLastErrorStdStr() <<endl;
							}

							b=0;
							b=usb_interrupt_read(handlTAOS, 0x81, rd_tmp, 8, 500);
							if (b>0)
							{
								printf("Read success %i\n", b);
					
							}
							else
							{
								printf("%i error reading: %s\n", b, usb_strerror());
								cout << "Error read: " << GetLastErrorStdStr() <<endl;
							}
    					}
    				}
    			}
    		}
		}
	}
	system("pause");
}

std::string GetLastErrorStdStr(void)//gives last error as string
{
  DWORD error = GetLastError();
  if (error)
  {
    LPVOID lpMsgBuf;
    DWORD bufLen = FormatMessage(
        FORMAT_MESSAGE_ALLOCATE_BUFFER | 
        FORMAT_MESSAGE_FROM_SYSTEM |
        FORMAT_MESSAGE_IGNORE_INSERTS,
        NULL,
        error,
        MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
        (LPTSTR) &lpMsgBuf,
        0, NULL );
    if (bufLen)
    {
		printf("Error from function: %s",lpMsgBuf);
		LPCSTR lpMsgStr = (LPCSTR)lpMsgBuf;
		std::string result(lpMsgStr, lpMsgStr+bufLen);
      
		LocalFree(lpMsgBuf);
		cout << "test" << endl;
		return result;
    }
  }
  return std::string();
}


This code is meant to read data from 'TCS3414 LIGHT-TO-DIGITAL COLOR LIGHT
SENSOR EVALUATION SYSTEM'. It also has a program that displays the light levels from the device (connected through USB) but I need to read them in somehow.
I tried the above program but I get this output:
0
Found TAOS by bNumConfigurations
Found TAOS by bNumInterfaces
Found TAOS by bInterfaceClass
Configuring success
Interface claimed
-5 error reading: libusb0-dll:err [submit_async] submitting request failed, win
error: Invalid access to memory location.

Error from function: Invalid access to memory location.
test
Error read: Invalid access to memory location.

Any ideas why I can't read info from the device?
Because you don't provide a buffer to read to. Instead you just provide an uninitialized pointer.
I changed:
char rd_tmp[100];
and
b=usb_interrupt_read(handlTAOS, 0x81, rd_tmp, 64, 500);
packet size is 64

And got this error:
-22 error reading: libusb0-dll:err [submit_async] submitting request failed, win
 error: The parameter is incorrect.
I have another question.
I have this light sensor and its software that displays the values. I used USBlyser to see what data is sent. The program sends some instructions and the device replies with some. But for some reason, the data is sent 3 times and followed with 64 bytes buffer.
USBlyzer Report															
															
Capture List															
															
Type	Seq	Time	Elapsed	Duration	Request	Request Details	Raw Data	I/O	C:I:E	Device Object	Device Name	Driver Name	IRP	Status	
START	1	14:23:13.570													
URB	1436	14:23:18.334	4.764151 s		Bulk or Interrupt Transfer	Output Report len:64	01 39 11 10 39 02 01 B6...	out	01:00:01	FFFFFA8005132CF0h		ausbmon	FFFFFA80095C7B40h		01 39 11 10 39 02 01 B6 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
URB	1437	14:23:18.334	4.764156 s		Bulk or Interrupt Transfer	Output Report len:64	01 39 11 10 39 02 01 B6...	out	01:00:01	FFFFFA80046CA160h		ACPI	FFFFFA80095C7B40h		01 39 11 10 39 02 01 B6 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
URB	1438	14:23:18.334	4.764161 s		Bulk or Interrupt Transfer	Output Report len:64	01 39 11 10 39 02 01 B6...	out	01:00:01	FFFFFA800448A060h	USBPDO-4	usbhub	FFFFFA80095C7B40h		01 39 11 10 39 02 01 B6 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
URB	1439-1438	14:23:18.335	4.765540 s	1.379 ms	Bulk or Interrupt Transfer	64 bytes buffer		out	01:00:01	FFFFFA800448A060h	USBPDO-4	usbhub	FFFFFA80095C7B40h	Success (Success)	
URB	1440-1437	14:23:18.335	4.765543 s	1.386 ms	Bulk or Interrupt Transfer	64 bytes buffer		out	01:00:01	FFFFFA80046CA160h		ACPI	FFFFFA80095C7B40h	Success (Success)	
URB	1441-1436	14:23:18.335	4.765545 s	1.393 ms	Bulk or Interrupt Transfer	64 bytes buffer		out	01:00:01	FFFFFA8005132CF0h		ausbmon	FFFFFA80095C7B40h	Success (Success)	
URB	1442-1423	14:23:18.336	4.766537 s	13.958 ms	Bulk or Interrupt Transfer	Input Report len:64	01 39 11 10 39 02 80 00...	in	01:00:81	FFFFFA800448A060h	USBPDO-4	usbhub	FFFFFA80051CB010h	Success (Success)	01 39 11 10 39 02 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
URB	1443-1422	14:23:18.336	4.766541 s	13.963 ms	Bulk or Interrupt Transfer	Input Report len:64	01 39 11 10 39 02 80 00...	in	01:00:81	FFFFFA80046CA160h		ACPI	FFFFFA80051CB010h	Success (Success)	01 39 11 10 39 02 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
URB	1444-1421	14:23:18.336	4.766544 s	13.968 ms	Bulk or Interrupt Transfer	Input Report len:64	01 39 11 10 39 02 80 00...	in	01:00:81	FFFFFA8005132CF0h		ausbmon	FFFFFA80051CB010h	Success (Success)	01 39 11 10 39 02 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
URB	1445	14:23:18.336	4.766558 s		Bulk or Interrupt Transfer	64 bytes buffer		in	01:00:81	FFFFFA8005132CF0h		ausbmon	FFFFFA80051CB010h		
URB	1446	14:23:18.336	4.766561 s		Bulk or Interrupt Transfer	64 bytes buffer		in	01:00:81	FFFFFA80046CA160h		ACPI	FFFFFA80051CB010h		
URB	1447	14:23:18.336	4.766564 s		Bulk or Interrupt Transfer	64 bytes buffer		in	01:00:81	FFFFFA800448A060h	USBPDO-4	usbhub	FFFFFA80051CB010h		
URB	1448	14:23:18.336	4.766583 s		Bulk or Interrupt Transfer	Output Report len:64	01 3A 11 10 39 02 01 B2...	out	01:00:01	FFFFFA8005132CF0h		ausbmon	FFFFFA80095C7B40h		01 3A 11 10 39 02 01 B2 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
URB	1449	14:23:18.336	4.766586 s		Bulk or Interrupt Transfer	Output Report len:64	01 3A 11 10 39 02 01 B2...	out	01:00:01	FFFFFA80046CA160h		ACPI	FFFFFA80095C7B40h		01 3A 11 10 39 02 01 B2 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
URB	1450	14:23:18.336	4.766589 s		Bulk or Interrupt Transfer	Output Report len:64	01 3A 11 10 39 02 01 B2...	out	01:00:01	FFFFFA800448A060h	USBPDO-4	usbhub	FFFFFA80095C7B40h		01 3A 11 10 39 02 01 B2 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
URB	1451-1450	14:23:18.337	4.767535 s	946 us	Bulk or Interrupt Transfer	64 bytes buffer		out	01:00:01	FFFFFA800448A060h	USBPDO-4	usbhub	FFFFFA80095C7B40h	Success (Success)	
URB	1452-1449	14:23:18.337	4.767538 s	952 us	Bulk or Interrupt Transfer	64 bytes buffer		out	01:00:01	FFFFFA80046CA160h		ACPI	FFFFFA80095C7B40h	Success (Success)	
URB	1453-1448	14:23:18.337	4.767540 s	956 us	Bulk or Interrupt Transfer	64 bytes buffer		out	01:00:01	FFFFFA8005132CF0h		ausbmon	FFFFFA80095C7B40h	Success (Success)	
URB	1454-1435	14:23:18.338	4.768572 s	14.010 ms	Bulk or Interrupt Transfer	Input Report len:64	01 3A 11 10 39 02 2F 00...	in	01:00:81	FFFFFA800448A060h	USBPDO-4	usbhub	FFFFFA8004BCC390h	Success (Success)	01 3A 11 10 39 02 2F 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
URB	1455-1434	14:23:18.338	4.768575 s	14.016 ms	Bulk or Interrupt Transfer	Input Report len:64	01 3A 11 10 39 02 2F 00...	in	01:00:81	FFFFFA80046CA160h		ACPI	FFFFFA8004BCC390h	Success (Success)	01 3A 11 10 39 02 2F 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
URB	1456-1433	14:23:18.338	4.768577 s	14.021 ms	Bulk or Interrupt Transfer	Input Report len:64	01 3A 11 10 39 02 2F 00...	in	01:00:81	FFFFFA8005132CF0h		ausbmon	FFFFFA8004BCC390h	Success (Success)	01 3A 11 10 39 02 2F 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
URB	1457	14:23:18.338	4.768587 s		Bulk or Interrupt Transfer	64 bytes buffer		in	01:00:81	FFFFFA8005132CF0h		ausbmon	FFFFFA8004BCC390h		
URB	1458	14:23:18.338	4.768590 s		Bulk or Interrupt Transfer	64 bytes buffer		in	01:00:81	FFFFFA80046CA160h		ACPI	FFFFFA8004BCC390h		
URB	1459	14:23:18.338	4.768592 s		Bulk or Interrupt Transfer	64 bytes buffer		in	01:00:81	FFFFFA800448A060h	USBPDO-4	usbhub	FFFFFA8004BCC390h		
URB	1460	14:23:18.338	4.768613 s		Bulk or Interrupt Transfer	Output Report len:64	01 3B 11 10 39 02 01 B0...	out	01:00:01	FFFFFA8005132CF0h		ausbmon	FFFFFA80095C7B40h		01 3B 11 10 39 02 01 B0 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
URB	1461	14:23:18.338	4.768616 s		Bulk or Interrupt Transfer	Output Report len:64	01 3B 11 10 39 02 01 B0...	out	01:00:01	FFFFFA80046CA160h		ACPI	FFFFFA80095C7B40h		01 3B 11 10 39 02 01 B0 03 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

What is the reason for sending the data 3 times?
And why is it followed by the 64 bytes buffer?

Its so complicated. Is there a way to leave the program running( so that it sends data requests and I don't have to) but intercept the data coming from the device with my program (is there a sample code for this in c or c++)?? I just need to get the RGB values that the device sends.
Last edited on
well, it seems that it sends the same message to 3 different devices:
ausbmon
ACPI
USBPDO-4 usbhub


So I took a look at the libusb header. You find
1
2
3
4
5
6
7
8
#define USB_ENDPOINT_ADDRESS_MASK	0x0f    /* in bEndpointAddress */
#define USB_ENDPOINT_DIR_MASK		  0x80

#define USB_ENDPOINT_TYPE_MASK		0x03    /* in bmAttributes */
#define USB_ENDPOINT_TYPE_CONTROL	    0
#define USB_ENDPOINT_TYPE_ISOCHRONOUS	1
#define USB_ENDPOINT_TYPE_BULK		    2
#define USB_ENDPOINT_TYPE_INTERRUPT	  3 
for the endpoints. From that I'd say the the endpoint (the second parameter for usb_interrupt_read()) should be 0x83
Topic archived. No new replies allowed.