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();
}
|