tahoma2d/toonz/sources/tnzbase/tscanner/TScannerIO/TUSBScannerIO_W.cpp

136 lines
3.8 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#include "TUSBScannerIO.h"
#include "tsystem.h"
#include <windows.h>
#include <strstream>
2016-06-15 18:43:10 +12:00
class TUSBScannerIOPD {
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
TUSBScannerIOPD() : m_handle(INVALID_HANDLE_VALUE), m_trace(false) {}
HANDLE m_handle;
bool m_trace;
2016-03-19 06:57:51 +13:00
};
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
namespace {
void buf2printable(const unsigned char *buffer, const int size,
std::ostrstream &os) {
int i = 0;
if ((size == 2) && (buffer[0] == 0x1b)) {
os << "ESC ";
char c = buffer[1];
if (isprint(c)) os << c << " ";
return;
}
os << std::hex;
for (; i < std::min(size, 0x40); ++i) {
char c = buffer[i];
os << "0x" << (unsigned int)c << " ";
}
if (i < size) os << "...";
2016-03-19 06:57:51 +13:00
}
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TUSBScannerIO::TUSBScannerIO() : m_data(new TUSBScannerIOPD()) {}
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool TUSBScannerIO::open() {
m_data->m_handle = CreateFile("\\\\.\\usbscan0", GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE | FILE_SHARE_READ, NULL,
OPEN_EXISTING, 0, NULL);
if (m_data->m_handle == INVALID_HANDLE_VALUE) return false;
return true;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TUSBScannerIO::close() {
if (m_data->m_handle && (m_data->m_handle != INVALID_HANDLE_VALUE))
CloseHandle(m_data->m_handle);
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
int TUSBScannerIO::receive(unsigned char *buffer, int size) {
int bytesLeft = size;
unsigned char *ptr = buffer;
DWORD count;
static int m_maxReadSize = 0x10000;
do {
int bytesToRead = bytesLeft;
notMoreThan<int>(m_maxReadSize, bytesToRead);
OVERLAPPED overlapped;
memset(&overlapped, 0, sizeof(OVERLAPPED));
overlapped.hEvent = CreateEvent(NULL, // pointertosecurityattributes,
// WIN95ignoresthisparameter
FALSE, // automaticreset
FALSE, // initializetonotsignaled
NULL); // pointertotheevent-objectname
ReadFile(m_data->m_handle, ptr, bytesToRead, &count, &overlapped);
DWORD waitRC = WaitForSingleObject(overlapped.hEvent, INFINITE);
if (m_data->m_trace) {
std::ostrstream os;
os.freeze(false);
os << "receive: size=" << size << " got = " << count << " buf=";
buf2printable(ptr, count, os);
os << '\n' << '\0';
TSystem::outputDebug(os.str());
}
if (count != bytesToRead) return 0;
ptr += count;
bytesLeft = bytesLeft - count;
} while (bytesLeft);
return size;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
int TUSBScannerIO::send(unsigned char *buffer, int size) {
int bytesLeft = size;
DWORD count;
static int m_maxWriteSize = 64;
// bytesLeft = 64;
do {
int bytesToWrite = bytesLeft;
notMoreThan<int>(m_maxWriteSize, bytesToWrite);
WriteFile(m_data->m_handle, buffer, bytesToWrite, &count, 0);
if (m_data->m_trace) {
std::ostrstream os;
os.freeze(false);
os << "send: size=" << size << " wrote = " << count << " buf=";
buf2printable(buffer, size, os);
os << '\n' << '\0';
TSystem::outputDebug(os.str());
}
if (count != bytesToWrite) return 0;
// ptr += count;
bytesLeft = bytesLeft - count;
} while (bytesLeft);
return size;
2016-03-19 06:57:51 +13:00
}
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void TUSBScannerIO::trace(bool on) { m_data->m_trace = on; }
2016-03-19 06:57:51 +13:00
//-----------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
TUSBScannerIO::~TUSBScannerIO() {}