#include "usbthread.h" #include #include #include #include usbthread::usbthread(QObject *parent): QThread{parent} { qRegisterMetaType("cv::Mat"); // qRegisterMetaType("Mat"); newProcess = new datathread(); connect(this,SIGNAL(signalAppendData(char*,int)),newProcess,SLOT(appendData(char*,int))); connect(newProcess,SIGNAL(signalGetImage(cv::Mat )),this,SLOT(slotGetImage(cv::Mat ))); connect(newProcess,SIGNAL(signalSetID(QString)),this,SLOT(slotSetID(QString))); rows = -1; cols = -1; usbData =(unsigned char*) malloc(0x10000); memset(usbData,0,0x10000); datalen = 0x10000; isRun = false; tg = new QTimer(); lensum = 0; connect(tg,&QTimer::timeout,[=](){ // qDebug()<<"lensum = "<start(1000); usbstatus = 0; } usbthread::~usbthread() { } void usbthread::SetImageInfo(int _type, int _rows, int _cols) { type = _type; rows = _rows; cols = _cols; if(newProcess!=NULL) newProcess->SetImageInfo(type,rows,cols); b_test = false; qDebug()<<__FILE__<<__LINE__; } void usbthread::stopProcessData() { // if(handle){ // libusb_close(handle); // } // if(ctx){ // libusb_exit(ctx); // } newProcess->setStatu(false); newProcess->exit(); // newProcess->terminate(); newProcess->wait(); } void usbthread::setStatu(bool _run) { isRun = _run; } int usbthread::getUsbStatus() { return usbstatus; } void usbthread::settest() { rows = 1024; cols = 1280; // rows = 512; // cols = 640; b_test = true; if(newProcess!=NULL) newProcess->SetImageInfo(type,rows,cols); qDebug()<<__FILE__<<__LINE__; } QString usbthread::getid() { return id; } //void usbClass::setcallback(void (*func)(cv::Mat)) //{ // imageCallback = func; //} #define queue 128 #define UPCHAR unsigned char* void usbthread::run() { newProcess->setStatu(true); newProcess->start(); device = new CCyUSBDevice(NULL); int vid,pid; int d = 0; int devices = device->DeviceCount(); do{ device->Open(d); vid = device->VendorID; pid = device->ProductID; d++; }while((dGetDeviceDescriptor(desc); // if(desc->bDeviceSubClass == 0x03){ // } CCyBulkEndPoint* bulkinPoint =(CCyBulkEndPoint*)device->EndPointOf(0x81); if(bulkinPoint == NULL){ qDebug()<<"no endpoint "; usbstatus = 2; return; } if( device->bSuperSpeed !=true){ signalMessage(QString::fromLocal8Bit("当前usb接口并非usb3.0, 请确认连接")); newProcess->setStatu(false); usbstatus = 3; return; } usbstatus = 1; unsigned char** buffer = new unsigned char*[128]; unsigned char** contexts = (unsigned char**)malloc(sizeof(unsigned char*)*128); OVERLAPPED inOvLap[128]; long len = bulkinPoint->MaxPktSize; for(int i = 0;i<128;i++){ buffer[i] = (unsigned char*)malloc(len); inOvLap[i].hEvent = CreateEventW(NULL,false,false,NULL); contexts[i] = bulkinPoint->BeginDataXfer(buffer[i],len,&inOvLap[i]); } int i =0; QFile f("bin.bin"); f.open(QIODevice::ReadWrite); int ccccc = 0; while(isRun){ long rlen = len; if(!bulkinPoint->WaitForXfer(&inOvLap[i],1500)){ } if(bulkinPoint->FinishDataXfer(buffer[i],rlen,&inOvLap[i],contexts[i])){ if(ccccc<100){ f.write((char*)buffer[i],rlen); ccccc+=1; } if(ccccc == 100){ f.close(); ccccc+=1; } emit(signalAppendData((char*)buffer[i],rlen)); } contexts[i] = bulkinPoint->BeginDataXfer(buffer[i],len,&inOvLap[i]); i++; if(i==queue){ i = 0; } } usbstatus = 0; } void usbthread::logout() { } void usbthread::slotGetImage(Mat image) { if(b_test){ // qDebug()<<__FILE__<<__LINE__<<"origin size"<