USB读图算法调整

This commit is contained in:
lovelyyoung 2020-11-12 19:35:20 +08:00
parent a4ad46d42e
commit d04656ae74
1 changed files with 92 additions and 69 deletions

View File

@ -156,7 +156,7 @@ GScanO200::~GScanO200()
m_threadUsb->join(); m_threadUsb->join();
m_threadUsb.reset(); m_threadUsb.reset();
} }
if(m_usb.get()) if (m_usb.get())
m_usb.reset(); m_usb.reset();
} }
@ -299,7 +299,7 @@ std::string GScanO200::GetSerialNum()
if (m_usb.get() && m_usb->is_connected()) if (m_usb.get() && m_usb->is_connected())
{ {
std::lock_guard<std::mutex> lck(m_imgLocker); std::lock_guard<std::mutex> lck(m_imgLocker);
//if (SerialNum.empty()) { if (SerialNum.empty()) {
#ifndef G200 #ifndef G200
SerialNum.resize(14); SerialNum.resize(14);
#else // !G200 #else // !G200
@ -309,7 +309,7 @@ std::string GScanO200::GetSerialNum()
m_usb->write_bulk(&usbcb, sizeof(usbcb)); m_usb->write_bulk(&usbcb, sizeof(usbcb));
m_usb->read_bulk(&SerialNum[0], SerialNum.size()); m_usb->read_bulk(&SerialNum[0], SerialNum.size());
//} }
return SerialNum; return SerialNum;
} }
return ""; return "";
@ -352,69 +352,69 @@ void GScanO200::config_params(GScanCap& params)
void GScanO200::Scanner_StartScan(UINT16 count) void GScanO200::Scanner_StartScan(UINT16 count)
{ {
std::lock_guard<std::mutex> lck(m_imgLocker); std::lock_guard<std::mutex> lck(m_imgLocker);
if (m_threadUsb && m_threadUsb->joinable()) { if (m_threadUsb && m_threadUsb->joinable()) {
devState = DEV_STOP; devState = DEV_STOP;
m_threadUsb->join(); m_threadUsb->join();
} }
USBCB status = { GET_DSP_STATUS ,0,0 }; USBCB status = { GET_DSP_STATUS ,0,0 };
if (m_usb.get() && m_usb->is_connected()) if (m_usb.get() && m_usb->is_connected())
m_usb->write_bulk(&status, sizeof(status)); m_usb->write_bulk(&status, sizeof(status));
if (m_usb.get() && m_usb->is_connected()) if (m_usb.get() && m_usb->is_connected())
m_usb->read_bulk(&status, sizeof(status)); m_usb->read_bulk(&status, sizeof(status));
switch (status.u32_Data) switch (status.u32_Data)
{ {
case COUNT_MODE: case COUNT_MODE:
case NO_FEED: case NO_FEED:
case OPEN_COVER: case OPEN_COVER:
case FEED_IN_ERROR: case FEED_IN_ERROR:
case PAPER_JAM: case PAPER_JAM:
case DETECT_DOUBLE_FEED: case DETECT_DOUBLE_FEED:
case DETECT_STAPLE: case DETECT_STAPLE:
case PAPER_SKEW: case PAPER_SKEW:
case HARDWARE_ERROR: case HARDWARE_ERROR:
case PC_SCAN_BUSY_or_ERROR: case PC_SCAN_BUSY_or_ERROR:
m_pImages->setscanflags(false); m_pImages->setscanflags(false);
devState = DEV_WRONG; devState = DEV_WRONG;
Set_ErrorCode(status.u32_Data); Set_ErrorCode(status.u32_Data);
if (huagods) if (huagods)
dev_callback(status.u32_Data, huagods); dev_callback(status.u32_Data, huagods);
return; return;
default: default:
break; break;
} }
//#ifndef G200 #ifndef G200
USBCB paperstatus = { GET_PAPER_STATUS ,0,0 }; USBCB paperstatus = { GET_PAPER_STATUS ,0,0 };
if (m_usb.get() && m_usb->is_connected()) if (m_usb.get() && m_usb->is_connected())
m_usb->write_bulk(&paperstatus, sizeof(paperstatus)); m_usb->write_bulk(&paperstatus, sizeof(paperstatus));
if (m_usb.get() && m_usb->is_connected()) if (m_usb.get() && m_usb->is_connected())
m_usb->read_bulk(&paperstatus, sizeof(paperstatus)); m_usb->read_bulk(&paperstatus, sizeof(paperstatus));
if (paperstatus.u32_Data == 0) { if (paperstatus.u32_Data == 0) {
m_pImages->setscanflags(false); m_pImages->setscanflags(false);
devState = DEV_WRONG; devState = DEV_WRONG;
Set_ErrorCode(NO_FEED); Set_ErrorCode(NO_FEED);
if (huagods) if (huagods)
dev_callback(NO_FEED, huagods); dev_callback(NO_FEED, huagods);
return; return;
} }
//#endif // !G200 #endif // !G200
if (gcap.is_duplex) if (gcap.is_duplex)
count = count == 65535 ? 65535 : count / 2; count = count == 65535 ? 65535 : count / 2;
USBCB usbcb = { START_COMMAND,(UINT32)count ,0 }; USBCB usbcb = { START_COMMAND,(UINT32)count ,0 };
if (m_usb.get() && m_usb->is_connected()) if (m_usb.get() && m_usb->is_connected())
m_usb->write_bulk(&usbcb, sizeof(usbcb)); m_usb->write_bulk(&usbcb, sizeof(usbcb));
this_thread::sleep_for(std::chrono::milliseconds(200)); this_thread::sleep_for(std::chrono::milliseconds(200));
if (m_usb.get() && m_usb->is_connected()) if (m_usb.get() && m_usb->is_connected())
{ {
m_pImages->setscanflags(true); m_pImages->setscanflags(true);
m_threadUsb.reset(new std::thread(&GScanO200::usbmain, this)); m_threadUsb.reset(new std::thread(&GScanO200::usbmain, this));
m_pImages->run(); m_pImages->run();
} }
} }
void GScanO200::Stop_scan() void GScanO200::Stop_scan()
@ -425,6 +425,7 @@ void GScanO200::Stop_scan()
m_usb->write_bulk(&usbcb, sizeof(usbcb)); m_usb->write_bulk(&usbcb, sizeof(usbcb));
} }
void GScanO200::ResetScanner() void GScanO200::ResetScanner()
{ {
if (m_usb.get() && !m_usb->is_connected()) if (m_usb.get() && !m_usb->is_connected())
@ -432,7 +433,7 @@ void GScanO200::ResetScanner()
std::lock_guard<std::mutex> lck(m_imgLocker); std::lock_guard<std::mutex> lck(m_imgLocker);
USBCB usbcb = { INIT_HARDWARE_SYS ,0,0 }; USBCB usbcb = { INIT_HARDWARE_SYS ,0,0 };
if(m_usb.get() && m_usb->is_connected()) if (m_usb.get() && m_usb->is_connected())
m_usb->write_bulk(&usbcb, sizeof(usbcb)); m_usb->write_bulk(&usbcb, sizeof(usbcb));
} }
@ -556,7 +557,15 @@ void GScanO200::usbmain()
return; return;
} }
if (gcap.resolution_dst >= 300.0f) if (gcap.resolution_dst == 200.0f)
{
if (m_pImages->orginimgcount() > 5)
{
this_thread::sleep_for(chrono::milliseconds(10));
continue;
}
}
else if(gcap.resolution_dst>200.0f)
{ {
if (m_pImages->orginimgcount() > 2) if (m_pImages->orginimgcount() > 2)
{ {
@ -578,6 +587,7 @@ void GScanO200::usbmain()
writelog("imgData->size() error"); writelog("imgData->size() error");
break; break;
} }
m_pImages->pushMat(std::shared_ptr<IDecode>(new G200Decode(imgData))); m_pImages->pushMat(std::shared_ptr<IDecode>(new G200Decode(imgData)));
//static int rawdataindex = 0; //static int rawdataindex = 0;
//writelog("origin rawbuffer index " + std::to_string(++rawdataindex)); //writelog("origin rawbuffer index " + std::to_string(++rawdataindex));
@ -665,16 +675,29 @@ std::shared_ptr<std::vector<char>> GScanO200::Get_Img_Data(int bufferSize)
std::shared_ptr<std::vector<char>> imData(new std::vector<char>(bufferSize)); std::shared_ptr<std::vector<char>> imData(new std::vector<char>(bufferSize));
StopWatch sw; StopWatch sw;
int readed = 0; int readed = 0;
while (readed < bufferSize && sw.elapsed_ms() < 5000) { USBCB usbcb = { GET_IMAGE,0,bufferSize };
USBCB usbcb = { GET_IMAGE,0,(UINT32)bufferSize }; m_usb->write_bulk(&usbcb, sizeof(usbcb));
m_usb->write_bulk(&usbcb, sizeof(usbcb)); int totalength = bufferSize;
readed = m_usb->read_bulk(imData->data(), bufferSize); int startindex = 0;
while (totalength > 0)
{
int dstlength = 1024 * 1024;
if (totalength <= dstlength)
{
dstlength = totalength;
totalength = 0;
}
else
totalength -= dstlength;
int tt = m_usb->read_bulk(imData->data() + startindex, dstlength);
startindex += dstlength;
} }
if (sw.elapsed_ms() > 3000) { if (sw.elapsed_ms() > 5000)
{
writelog("Usb read data timeout\n"); writelog("Usb read data timeout\n");
} }
return imData; return imData;
} }