You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
3.3 KiB
155 lines
3.3 KiB
#include "usbthread.h" |
|
#include <QDebug> |
|
#include <opencv2/opencv.hpp> |
|
#include <QFile> |
|
#include <chrono> |
|
|
|
usbthread::usbthread(QObject *parent): |
|
QThread{parent} |
|
{ |
|
qRegisterMetaType<cv::Mat>("cv::Mat"); |
|
// qRegisterMetaType<Mat>("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 ))); |
|
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 = "<<lensum; |
|
// lensum = 0; |
|
}); |
|
tg->start(1000); |
|
} |
|
|
|
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); |
|
} |
|
|
|
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; |
|
} |
|
|
|
//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((d<devices)&&(vid!=0x04b4)&&(pid!=0x00f1)); |
|
|
|
|
|
// PUSB_DEVICE_DESCRIPTOR desc; |
|
// device->GetDeviceDescriptor(desc); |
|
// if(desc->bDeviceSubClass == 0x03){ |
|
|
|
// } |
|
|
|
CCyBulkEndPoint* bulkinPoint =(CCyBulkEndPoint*)device->EndPointOf(0x81); |
|
if(bulkinPoint == NULL){ |
|
qDebug()<<"no endpoint "; |
|
return; |
|
} |
|
if( device->bSuperSpeed !=true){ |
|
signalMessage(QString::fromLocal8Bit("当前usb接口并非usb3.0, 请确认连接")); |
|
newProcess->setStatu(false); |
|
return; |
|
} |
|
|
|
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; |
|
while(isRun){ |
|
long rlen = len; |
|
if(!bulkinPoint->WaitForXfer(&inOvLap[i],1500)){ |
|
} |
|
|
|
if(bulkinPoint->FinishDataXfer(buffer[i],rlen,&inOvLap[i],contexts[i])){ |
|
|
|
emit(signalAppendData((char*)buffer[i],rlen)); |
|
} |
|
contexts[i] = bulkinPoint->BeginDataXfer(buffer[i],len,&inOvLap[i]); |
|
i++; |
|
if(i==queue){ |
|
i = 0; |
|
} |
|
} |
|
|
|
|
|
|
|
} |
|
|
|
void usbthread::logout() |
|
{ |
|
|
|
} |
|
|
|
void usbthread::slotGetImage(Mat image) |
|
{ |
|
emit signalGetImage(image); |
|
|
|
}
|
|
|