libusb-1.0.22 示例
环境配置:
win7-x86
Qt5.9.7 + mingw-win32
源码:
g_header.h
1 #ifndef __USB_GLOBAL_H__ 2 #define __USB_GLOBAL_H__ 3 4 typedef unsigned char us_byte; 5 typedef unsigned short us_short; 6 7 #define USB_G_CONF 0x01 8 #define USB_G_INTF 0x00 9 10 #define READ_BUS_TIMEOUT (5 * 1000)//(3 * 60 * 1000) 11 #define READ_BUS_CONFIRM_TIMEOUT (500) 12 13 #define TRANSFER_DELAY_MS 500 14 #define TRANSFER_SPACE_MS 100 15 16 #define BUFFER_MAX_LENGTH 512 17 #define BUFFER_MAX_DATA_LENGTH (BUFFER_MAX_LENGTH - 18) 18 #define USP_FIX_PRINTDPI_PARA (100 / 2.54) 19 20 #define LOG_LOCAL_PATH "./" 21 22 /////////////////////////////////// 23 #define IMAGE_FIXED_WIDTH 1008 24 #define IMAGE_FIXED_HEIGHT 2000 25 #define IMAGE_CODE_FIXED_HEIGHT 1000 26 27 #define ERR_USB_FIND_BUS 0xE000 28 #define ERR_USB_FIND_DEV 0xE001 29 #define ERR_USB_STATIC_READ 0xE002 30 #define ERR_USB_STATIC_WRITE 0xE003 31 #define ERR_USB_DONT_OPEN 0xE004 32 #define ERR_USB_SET_CONFIG 0xE005 33 #define ERR_USB_SET_CLAIM 0xE006 34 #define ERR_TRANS_ACK 0xE101 // 35 #define ERR_TRANS_DATA 0xE102 // crc check code 36 #define ERR_TRANS_NULLSTR 0xE103 // null data 37 #define ERR_TRANS_MISSONE 0xE104 // miss package 38 #define ERR_TRANS_TOOLONG 0xE105 // package is too long 39 #define ERR_TRANS_PARA 0xE106 // para is wrong 40 #define ERR_TRANS_NOTSEND 0xE107 // send unsuccessfully 41 #define ERR_TRANS_MALLOC 0xE108 // malloc unsuccessfully 42 #define ERR_TRANS_UNKNOW 0xE1FF // unknown error 43 #define ERR_IMAGE_SAVE 0xE200 // image do not save 44 #define ERR_BILLS_UNKNOW 0xE201 // bills processing error 45 #define ERR_JSON_FORMAT 0xE202 // json str format is wrong 46 #define ERR_JSON_PARA 0xE203 // json str para is wrong 47 #define ERR_JSON_FILE 0xE204 // json file not exist 48 #define ERR_DEVICE_BUSY 0xE205 49 #define ERR_UPDATE_FAILED 0xE206 // update failed 50 51 typedef enum { 52 IMGNumRGB = 0x01, // 1 票号彩色 01 02 03 53 IMGNum_IGRAY, // 2 票号红外灰度 04 54 IMGFrontRGB, // 3 整票正面彩色 05 06 07 55 IMGFloorRGB, // 4 整票背面彩色 08 09 10 56 IMGFront_URGB, // 5 整票正面紫外反射彩色 11 12 13 57 IMGFloor_URGB, // 6 整票反面紫外反射彩色 14 15 16 58 IMG_UTRGB, // 7 整票紫外透射彩色 17 18 19 59 IMGFront_IGRAY, // 8 整票红外反射正面灰度 20 60 IMGFloor_IGRAY, // 9 整票红外反射反面灰度 21 61 IMG_ITGRAY // 10 整票红外透射灰度 22 62 }IMAGETYPE; 63 64 typedef enum { 65 IMG_F_BMP = 0x00, 66 IMG_F_JPEG, 67 IMG_F_TIFF 68 }IMAGE_FORMAT; 69 70 #endif // USBGLOBAL_H
usbbasic.h
1 #ifndef __USBBASIC_H__ 2 #define __USBBASIC_H__ 3 4 #include "libusb.h" 5 #include "g_header.h" 6 7 extern libusb_device_handle *u_devHandle; 8 9 #ifdef __cplusplus 10 extern "C" 11 { 12 #endif 13 14 int device_init(void); 15 libusb_device_handle* device_openWithID(const us_short u_vid, const us_short u_pid); 16 int device_config(libusb_device_handle *u_dev, const int cfg, const int inf); 17 int device_config_info_printf(); 18 19 void device_close(void); 20 21 int usbBusWrite(us_byte *p_data, int *p_length, int s_timeout = 0); 22 int usbBusRead(us_byte *p_data, int *p_length, int s_timeout = 0); 23 24 /**/ 25 us_short g_getCRC16(const us_byte *p_data, int p_length); 26 27 int execCmdWrite(us_short t_fi, const us_byte *iData, int is_length, int it_length, 28 int iFlag, int iNum, int iClean = 0); 29 int execCmdCoder(const us_byte *iData, int iLength, int iClean = 0); 30 int execBusClear(); 31 int execCmdRead(us_byte *iData, int &it_length, int s_timeout = READ_BUS_TIMEOUT); 32 33 int execCmdCfm(int tNum, us_short tFlag); 34 int execCfmChk(us_byte *iData, int iNum, us_short iFlag); 35 36 // int execCmdCheck(int tNum, us_short tFlag); 37 38 int execSendData(us_short t_fi, const char *iData, unsigned int iLength, unsigned int iMaxLength, 39 us_short iFlag); 40 int execSaveRGBData(char *iData, int &iLength, int iType, us_short iFlag); 41 42 43 44 #ifdef __cplusplus 45 } 46 #endif 47 #endif // USBBASIC_H
usbbasic.cpp
1 #include "usbbasic.h" 2 3 #include <qDebug> 4 #include <QByteArray> 5 6 //LIBUSB_ENDPOINT_IN = 0x80 /** In: device-to-host */ 7 #define USB_EP_IN 0x81 8 9 //LIBUSB_ENDPOINT_OUT = 0x00 /** Out: host-to-device */ 10 #define USB_EP_OUT 0x01 11 12 libusb_device **g_devs; 13 libusb_device_handle *u_devHandle = NULL; 14 15 int device_init(void) 16 { 17 if (u_devHandle) device_close(); 18 19 int relt = libusb_init(NULL); 20 if (relt < 0) return relt; 21 22 relt = libusb_get_device_list(NULL, &g_devs); 23 if (relt < 0) return relt; 24 25 const struct libusb_version * t_version = libusb_get_version(); 26 qDebug("-- Using libusb v%d.%d.%d.%d", 27 t_version->major, t_version->minor, t_version->micro, t_version->nano); 28 29 return relt; 30 } 31 32 libusb_device_handle* device_openWithID(const us_short u_vid, const us_short u_pid) 33 { 34 libusb_device *dev = NULL; 35 struct libusb_device_descriptor t_desc; 36 libusb_device_handle *t_handle = NULL; 37 38 int ii = 0, relt = 0; 39 while ((dev = g_devs[ii ++]) != NULL) { 40 relt = libusb_get_device_descriptor(dev, &t_desc); 41 if (relt < 0) continue; 42 43 if ((t_desc.idVendor == u_vid) && (t_desc.idProduct == u_pid)) { 44 relt = libusb_open(dev, &t_handle); 45 break; 46 } 47 } 48 49 return t_handle; 50 } 51 52 int device_config(libusb_device_handle *u_dev, const int cfg, const int inf) 53 { 54 int relt = 0; 55 56 relt = libusb_kernel_driver_active(u_dev, 0); 57 qDebug() <<"-- set driver active .." <<relt; 58 59 if (1 == relt) { 60 relt = libusb_set_auto_detach_kernel_driver(u_dev, 0); 61 qDebug() <<"-- set auto driver detach .." <<relt; 62 } 63 64 relt = libusb_set_configuration(u_dev, cfg); 65 qDebug() <<"-- set configure .." <<relt; 66 if (relt < 0) { 67 } 68 69 relt = libusb_claim_interface(u_dev, inf); 70 qDebug() <<"-- set interface .." <<relt; 71 if (relt < 0) { 72 } 73 74 return relt; 75 } 76 77 void device_close(void) 78 { 79 // libusb_release_interface(u_dev, USB_G_INTF); 80 if (u_devHandle == NULL) return; 81 82 libusb_close(u_devHandle); 83 u_devHandle = NULL; 84 85 libusb_free_device_list(g_devs, 1); 86 libusb_exit(NULL); 87 88 qDebug() <<"** close device and free .."; 89 } 90 91 int device_config_info_printf() 92 { 93 struct libusb_device_descriptor desc; 94 struct libusb_config_descriptor *config; 95 int relt = 0; 96 struct libusb_device *dev = libusb_get_device(u_devHandle); 97 98 relt = libusb_get_device_descriptor(dev, &desc); 99 if (relt < 0) return -1; 100 qDebug() <<"-- get device descriptor .." <<relt; 101 qDebug() <<"-- Bus:" <<libusb_get_bus_number(dev) <<"device:" <<libusb_get_device_address(dev); 102 qDebug("-- device vID pID : %04X - %04X", desc.idVendor, desc.idProduct); 103 104 for (int a = 0; a < desc.bNumConfigurations; a++) { 105 relt = libusb_get_config_descriptor(dev, a, &config); 106 if (relt < 0) break; 107 qDebug() <<"-- get device config descriptor .." <<relt; 108 qDebug() <<"\t-- Configure .." <<a; 109 qDebug() <<"\t< wTotalLength: \t" <<config->wTotalLength; 110 qDebug() <<"\t< bNumInterfaces: \t" <<config->bNumInterfaces; 111 qDebug() <<"\t< bConfigurationValue: \t" <<config->bConfigurationValue; 112 qDebug() <<"\t< iConfiguration: \t" <<config->iConfiguration; 113 qDebug() <<"\t< bmAttributes: \t" <<config->bmAttributes; 114 qDebug() <<"\t< MaxPower: \t" <<config->MaxPower; 115 116 for (int b = 0; b < config->bNumInterfaces; b++) { 117 const struct libusb_interface *infa = &config->interface[b]; 118 for (int c = 0; c < infa->num_altsetting; c ++) { 119 const struct libusb_interface_descriptor *altsetting = &infa->altsetting[c]; 120 qDebug() <<"\t\t-- Interface .." <<c; 121 qDebug() <<"\t\t<< bInterfaceNumber: \t" <<altsetting->bInterfaceNumber; 122 qDebug() <<"\t\t<< bAlternateSetting: \t" <<altsetting->bAlternateSetting; 123 qDebug() <<"\t\t<< bNumEndpoints: \t" <<altsetting->bNumEndpoints; 124 qDebug() <<"\t\t<< bInterfaceClass: \t" <<altsetting->bInterfaceClass; 125 qDebug() <<"\t\t<< bInterfaceSubClass: \t" <<altsetting->bInterfaceSubClass; 126 qDebug() <<"\t\t<< bInterfaceProtocol: \t" <<altsetting->bInterfaceProtocol; 127 qDebug() <<"\t\t<< iInterface: \t" <<altsetting->iInterface; 128 } 129 } 130 } 131 return 0; 132 } 133 134 int usbBusWrite(us_byte *p_data, int *p_length, int s_timeout) 135 { 136 int iResult = ERR_USB_STATIC_WRITE; 137 if (!u_devHandle) return iResult; 138 139 // iResult = libusb_bulk_transfer(u_dev, USB_EP_OUT, p_data, p_length, NULL, s_timeout); 140 iResult = libusb_interrupt_transfer(u_devHandle, USB_EP_OUT, p_data, *p_length, NULL, s_timeout); 141 qDebug("+ [%03d] %s", (*p_length), QByteArray::fromRawData((char*)p_data, *p_length).toHex(' ').data()); 142 return (iResult == 0) ? (*p_length) : iResult; 143 } 144 145 int usbBusRead(us_byte *p_data, int *p_length, int s_timeout) 146 { 147 int iResult = ERR_USB_STATIC_READ; 148 if (!u_devHandle) return iResult; 149 150 // iResult = libusb_bulk_transfer(u_dev, USB_EP_IN, p_data, BUFFER_MAX_LENGTH, &p_length, s_timeout); 151 iResult = libusb_interrupt_transfer(u_devHandle, USB_EP_IN, p_data, BUFFER_MAX_LENGTH, p_length, s_timeout); 152 153 qDebug("- [%03d] %s", (*p_length), QByteArray::fromRawData((char*)p_data, *p_length).toHex(' ').data()); 154 return (iResult == 0) ? (*p_length) : iResult; 155 } 156 157 us_short g_getCRC16(const us_byte *p_data, int p_length) 158 { 159 us_short cst_H[16] = { 0x00, 0x10, 0x20, 0x30, 0x40, 0x50, 0x60, 0x70, 160 0x81, 0x91, 0xa1, 0xb1, 0xc1, 0xd1, 0xe1, 0xf1 }; 161 us_short cst_L[16] = { 0x00, 0x21, 0x42, 0x63, 0x84, 0xa5, 0xc6, 0xe7, 162 0x08, 0x29, 0x4a, 0x6b, 0x8c, 0xad, 0xce, 0xef }; 163 164 if (0 >= p_length) return 0x00; 165 166 us_byte kh = 0x00, kl = 0x00; 167 kh = *p_data; 168 if (p_length > 1) kl = *(p_data + 1); 169 else kl = 0; 170 171 us_byte j = 0; 172 for (int i = 0; i < p_length; i++) { 173 j = (us_byte)(kh / 16); 174 kh = (us_byte)(kh * 16); 175 kh = (us_byte)(kh + (kl / 16)); 176 177 kl = (us_byte)(kl * 16); 178 if ((i + 2) < p_length) kl = (us_byte)(kl + (*(p_data + i + 2) / 16)); 179 180 kh = (us_byte)(kh ^ cst_H[j]); 181 kl = (us_byte)(kl ^ cst_L[j]); 182 183 j = (us_byte)(kh / 16); 184 185 kh = (us_byte)(kh * 16); 186 kh = (us_byte)(kh + (kl / 16)); 187 188 kl = (us_byte)(kl * 16); 189 if ((i + 2) < p_length) kl = (us_byte)(kl + (*(p_data + i + 2) & 0x0F)); 190 191 kh = (us_byte)(kh ^ cst_H[j]); 192 kl = (us_byte)(kl ^ cst_L[j]); 193 } 194 195 return (us_short)(kh * 256 + kl); 196 } 197 198 int execCmdCoder(const us_byte *iData, int iLength, int iClean) 199 { 200 int iResult = 0, tCount = iLength; 201 if (iLength < 0 || iLength > BUFFER_MAX_LENGTH) return ERR_TRANS_PARA; 202 203 // free Bus Data 204 if (1 == iClean) libusb_clear_halt(u_devHandle, USB_EP_IN); 205 206 // 207 iResult = usbBusWrite(const_cast<us_byte*>(iData), &tCount); 208 if (iResult == ERR_USB_STATIC_WRITE) iResult = ERR_USB_DONT_OPEN; 209 else if (iResult <= 0) iResult = ERR_TRANS_NOTSEND; 210 211 return (iResult = 0); 212 } 213 214 int execCmdWrite(us_short t_fi, const us_byte *iData, int is_length, int it_length, 215 int iFlag, int iNum, int iClean) 216 { 217 int iResult = 0; 218 if (is_length < 0 || is_length > BUFFER_MAX_DATA_LENGTH) return ERR_TRANS_PARA; 219 if (iNum < 0) return ERR_TRANS_PARA; 220 221 // free Bus Data 222 if (1 == iClean) libusb_clear_halt(u_devHandle, USB_EP_IN); 223 224 // 225 us_byte *iCommands = (us_byte *)malloc(BUFFER_MAX_LENGTH * sizeof(us_byte)); 226 if (iCommands == NULL) return ERR_TRANS_MALLOC; 227 228 us_byte *pData = iCommands; 229 int tCount = 0; 230 memset(iCommands, 0x00, BUFFER_MAX_LENGTH * sizeof(char)); 231 232 // 10 07 233 memset(pData + tCount++, 0x10, sizeof(us_byte)); 234 memset(pData + tCount++, 0x07, sizeof(us_byte)); 235 // flag 236 memset(pData + tCount++, iFlag, sizeof(us_byte)); 237 // data length 238 memset(pData + tCount++, (us_short)(is_length), sizeof(us_byte)); 239 memset(pData + tCount++, (us_short)(is_length >> 8), sizeof(us_byte)); 240 // totle length 241 memset(pData + tCount++, (us_short)(it_length), sizeof(us_byte)); 242 memset(pData + tCount++, (us_short)(it_length >> 8), sizeof(us_byte)); 243 memset(pData + tCount++, (us_short)(it_length >> 16), sizeof(us_byte)); 244 memset(pData + tCount++, (us_short)(it_length >> 24), sizeof(us_byte)); 245 // fi 246 memset(pData + tCount++, t_fi, sizeof(us_byte)); 247 // data 248 if (is_length > 0) { 249 memcpy(pData + tCount, iData, is_length * sizeof(us_byte)); 250 tCount += is_length; 251 } 252 // 253 memset(pData + tCount++, (us_short)(iNum), sizeof(us_byte)); 254 memset(pData + tCount++, (us_short)(iNum >> 8), sizeof(us_byte)); 255 // 256 memset(pData + tCount++, 0x10, sizeof(us_byte)); 257 memset(pData + tCount++, 0x08, sizeof(us_byte)); 258 259 // SRC 260 // int t_crc = g_getCRC16(pData, tCount); 261 // if (0 == t_crc) iResult = ERR_TRANS_DATA; 262 // memset(pData + tCount++, (us_short)(t_crc), sizeof(us_byte)); 263 // memset(pData + tCount++, (us_short)(t_crc >> 8), sizeof(us_byte)); 264 memset(pData + tCount++, 0x00, sizeof(us_byte)); 265 memset(pData + tCount++, 0x00, sizeof(us_byte)); 266 267 // send data 268 iResult = usbBusWrite(iCommands, &tCount); 269 if (iResult == ERR_USB_STATIC_WRITE) iResult = ERR_USB_DONT_OPEN; 270 if (iResult <= 0) iResult = ERR_TRANS_NOTSEND; 271 272 // clean 273 free(iCommands); 274 pData = iCommands = NULL; 275 276 return iResult; 277 } 278 279 int execBusClear() 280 { 281 int iResult = 0, i_len = 0; 282 283 us_byte *iData = (us_byte*)malloc(BUFFER_MAX_LENGTH * sizeof(char)); 284 if (!iData) return ERR_TRANS_MALLOC; 285 286 do { 287 memset(iData, 0x00, BUFFER_MAX_LENGTH * sizeof(char)); 288 iResult = usbBusRead(iData, &i_len); 289 290 Sleep(10); 291 }while((0 != iResult) && (ERR_USB_STATIC_READ != iResult)); 292 293 free(iData); 294 return iResult; 295 } 296 297 int execCmdRead(us_byte *iData, int &it_length, int s_timeout) 298 { 299 int iResult = 0; 300 long iEndTime = 0, iStartTime = GetTickCount(); 301 do { 302 iResult = usbBusRead(iData, &it_length); 303 304 if (0 == iResult) Sleep(TRANSFER_SPACE_MS); 305 iEndTime = GetTickCount(); 306 307 } while (iResult <= 0 && (iEndTime - iStartTime) < s_timeout); 308 309 return iResult; 310 } 311 312 int execCmdCfm(int tNum, us_short tFlag) 313 { 314 int iResult = 0, iCount = 0; 315 if (tNum < 0) return ERR_TRANS_PARA; 316 317 us_byte *iCommands = (us_byte *)malloc(BUFFER_MAX_LENGTH * sizeof(char)); 318 if (iCommands == NULL) return ERR_TRANS_MALLOC; 319 320 memset(iCommands, 0x00, BUFFER_MAX_LENGTH * sizeof(char)); 321 us_byte *t_pdata = iCommands; 322 // 10 05 00 00 00 323 memset(t_pdata + iCount ++, 0x10, sizeof(us_byte)); 324 memset(t_pdata + iCount ++, 0x05, sizeof(us_byte)); 325 memset(t_pdata + iCount ++, tFlag, sizeof(us_byte)); 326 memset(t_pdata + iCount ++, (us_short)(tNum >> 0), sizeof(us_byte)); 327 memset(t_pdata + iCount ++, (us_short)(tNum >> 8), sizeof(us_byte)); 328 329 iResult = usbBusWrite(iCommands, &iCount); 330 if (iResult == ERR_USB_STATIC_WRITE) iResult = ERR_USB_DONT_OPEN; 331 if (iResult <= 0) iResult = ERR_TRANS_NOTSEND; 332 333 // clean 334 free(iCommands); 335 t_pdata = iCommands = NULL; 336 337 return iResult; 338 } 339 340 int execCfmChk(us_byte *iData, int iNum, us_short iFlag) 341 { 342 if ((int)iData[0x00] != 0x10) return -1; 343 if ((int)iData[0x01] != 0x06) return -1; 344 if ((int)iData[0x02] != iFlag) return -1; 345 346 int i_num = ((int)iData[0x04] << 8) + ((int)iData[0x03]); 347 if (i_num != iNum) return -1; 348 349 return 0; 350 } 351 352 //int execCmdCheck(int tNum, us_short tFlag) 353 //{ 354 // int iResult = 0, iLen = BUFFER_MAX_LENGTH; 355 356 // us_byte *tData = (us_byte*)malloc(BUFFER_MAX_LENGTH * sizeof(char)); 357 // if (!tData) return ERR_TRANS_MALLOC; 358 359 // memset(tData, 0x00, BUFFER_MAX_LENGTH * sizeof(char)); 360 // long iEndTime = 0, iStartTime = GetTickCount(); 361 362 // do { 363 // iResult = usbBusRead(tData, &iLen); 364 365 // if (((int)tData[0x00] == 0x10) && ((int)tData[0x01] == 0x06) && ((int)tData[0x02] == tFlag) 366 // && (((int)tData[0x04] << 8) + ((int)tData[0x03])) == tNum) break; 367 // else { 368 // iResult = 0; 369 // memset(tData, 0x00, BUFFER_MAX_LENGTH * sizeof(char)); 370 // } 371 372 // if (0 >= iResult) Sleep(TRANSFER_SPACE_MS); 373 // iEndTime = GetTickCount(); 374 375 // } while (iResult <= 0 && (iEndTime - iStartTime) < READ_BUS_CONFIRM_TIMEOUT); 376 377 // free(tData); 378 // return (0 == iResult) ? ERR_DEVICE_BUSY : iResult; 379 //} 380 int execSendData(us_short t_fi, const char *iData, unsigned int iLength, unsigned int iMaxLength, 381 us_short iFlag) 382 { 383 if (iMaxLength > BUFFER_MAX_DATA_LENGTH) return ERR_TRANS_PARA; 384 if (NULL == iData) return ERR_TRANS_PARA; 385 386 int iResult = 0, iReadRst = 0, i_Num = 0, tmp_len = 0; 387 us_byte *tp_data = (us_byte*)iData; 388 unsigned int t_data_length = iLength, ts_l = 0; 389 390 us_byte *pRevData = (us_byte*)malloc(BUFFER_MAX_LENGTH * sizeof(us_byte)); 391 memset(pRevData, 0x00, BUFFER_MAX_LENGTH * sizeof(us_byte)); 392 393 do { 394 ts_l = (iLength < iMaxLength) ? t_data_length : iMaxLength; 395 iResult = execCmdWrite(t_fi, (us_byte*)tp_data, ts_l, iLength, iFlag, i_Num); 396 if ((0xDFFF < iResult) && (iResult < 0xEFFF)) break; 397 398 // 10 05 399 iReadRst = execCmdRead(pRevData, tmp_len, READ_BUS_CONFIRM_TIMEOUT); 400 iResult = (0 == iReadRst) ? ERR_DEVICE_BUSY : 0; 401 iResult = execCfmChk(pRevData, (i_Num ++), iFlag); 402 403 if (0 != iResult) break; 404 405 // == == 406 t_data_length -= ts_l; 407 tp_data += ts_l; 408 409 iResult = execCmdCfm((i_Num ++), iFlag); 410 memset(pRevData, 0x00, BUFFER_MAX_LENGTH * sizeof(char)); 411 iReadRst = execCmdRead(pRevData, tmp_len); 412 413 if (iReadRst >= 18) 414 { 415 int ii_flag = (int)pRevData[10] + ((int)pRevData[11] << 8); 416 iResult = (0 == ii_flag) ? ii_flag : ((int)pRevData[12]); 417 } 418 else iResult = ERR_TRANS_DATA; 419 420 // == == == == 421 if (0 != iResult) break; 422 }while(t_data_length > 0); 423 424 free(pRevData); 425 return iResult; 426 } 427 428 int execSaveRGBData(char *iData, int &iLength, int iType, us_short iFlag) 429 { 430 us_byte * t_fi_data = (us_byte*)malloc(sizeof(us_byte)); 431 memset(t_fi_data, 0x00, sizeof(us_byte)); 432 memset(t_fi_data, (us_short)iType, sizeof(us_byte)); 433 434 int iResult = 0, iReadRst = 0, i_len = 0; 435 iResult = execCmdWrite(0xF0, t_fi_data, 0x01, 0x01, iFlag, 0x00); 436 free(t_fi_data); 437 if ((0xDFFF < iResult) && (iResult < 0xEFFF)) return iResult; 438 439 us_byte *pRevData = (us_byte*)malloc(BUFFER_MAX_LENGTH * sizeof(char)); 440 memset(pRevData, 0x00, BUFFER_MAX_LENGTH * sizeof(char)); 441 iReadRst = execCmdRead(pRevData, i_len, READ_BUS_CONFIRM_TIMEOUT); 442 443 iResult = (0 == iReadRst) ? ERR_DEVICE_BUSY : 0; 444 iResult = execCfmChk(pRevData, 0x00, iFlag); 445 if (0 != iResult) return iResult; 446 447 iResult = execCmdCfm(0x01, iFlag); 448 449 // -- -- -- -- 450 int t_length = 0, t_total = 0; 451 do { 452 memset(pRevData, 0x00, BUFFER_MAX_LENGTH * sizeof(char)); 453 454 iReadRst = usbBusRead(pRevData, &i_len); 455 456 if (0 == iReadRst) continue; 457 458 // count data length 459 int t_single = ((int)pRevData[4] << 8) + (int)pRevData[3]; 460 if (0 == t_total) { 461 t_total += (int)pRevData[5]; 462 t_total += ((int)pRevData[6] << 8); 463 t_total += ((int)pRevData[7] << 16); 464 t_total += ((int)pRevData[8] << 24); 465 } 466 467 memcpy(iData + t_length, pRevData + 12, t_single * sizeof(char)); 468 t_length += t_single; 469 } while (t_length < t_total); 470 iLength = t_total; 471 472 free(pRevData); 473 return 0x00; 474 }

浙公网安备 33010602011771号