|
|
@@ -46,25 +46,44 @@ bool CPosPrinter::InitShouyin()
|
|
|
while(i < nDevice)
|
|
|
{
|
|
|
Port = szDevicePath[i++];
|
|
|
- LOG_INFO("device.Port = " << Port);
|
|
|
+ LOG_INFO("准备打开端口 Port = " << Port);
|
|
|
|
|
|
- HANDLE hPort = CreateFile(Port, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
+ HANDLE hPort = CreateFile(Port, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
|
|
|
|
|
|
if(hPort == INVALID_HANDLE_VALUE)
|
|
|
{
|
|
|
// 打开端口失败
|
|
|
- LOG_INFO("open usb port fail, port:" << Port);
|
|
|
+ DWORD error = GetLastError();
|
|
|
+
|
|
|
+ if(error == 2)
|
|
|
+ {
|
|
|
+ //没有指定的文件
|
|
|
+ LOG_INFO("没有找对对应的usb端口");
|
|
|
+ }
|
|
|
+ else if(error == 5)
|
|
|
+ {
|
|
|
+ LOG_INFO("usb端口被占用!");
|
|
|
+ }
|
|
|
+
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- //LOG_INFO("open port success usb, handle" << hPort);
|
|
|
+ LOG_INFO("打开usb端口,准备进行打印机检测! hPort:" << hPort);
|
|
|
|
|
|
- //把当前发现的保存下来
|
|
|
- PrinterHandle newHandle;
|
|
|
- newHandle.hPort = hPort;
|
|
|
- newHandle.type = 1;
|
|
|
+ if(PortTest(hPort) == true)
|
|
|
+ {
|
|
|
+ //端口测试连通,保存起来
|
|
|
+ PrinterHandle newHandle;
|
|
|
+ newHandle.hPort = hPort;
|
|
|
+ newHandle.type = 1;
|
|
|
|
|
|
- m_hPorts.push_back(newHandle);
|
|
|
+ m_hPorts.push_back(newHandle);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //端口打印机没有连接,那么就直接关闭掉并口,避免占用
|
|
|
+ CloseHandle(hPort);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//开始添加并口的端口
|
|
|
@@ -75,37 +94,36 @@ bool CPosPrinter::InitShouyin()
|
|
|
if(hPort == INVALID_HANDLE_VALUE)
|
|
|
{
|
|
|
DWORD error = GetLastError();
|
|
|
- //LOG_INFO("lpt error:" << error);
|
|
|
|
|
|
if(error == 2)
|
|
|
{
|
|
|
//没有指定的文件
|
|
|
LOG_INFO("本机器没有并口!");
|
|
|
}
|
|
|
- else if (error == 5)
|
|
|
- {
|
|
|
- LOG_INFO("并口被占用!");
|
|
|
- }
|
|
|
+ else if(error == 5)
|
|
|
+ {
|
|
|
+ LOG_INFO("并口被占用!");
|
|
|
+ }
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
//这个表示并口可以使用
|
|
|
- LOG_INFO("找到并口,准备进行打印机检测!");
|
|
|
-
|
|
|
- if (lptTest(hPort) == true)
|
|
|
- {
|
|
|
- //并口测试连通,保存起来
|
|
|
- PrinterHandle newHandle;
|
|
|
- newHandle.hPort = hPort;
|
|
|
- newHandle.type = 2;
|
|
|
-
|
|
|
- m_hPorts.push_back(newHandle);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- //并口打印机没有连接,那么就直接关闭掉并口,避免占用
|
|
|
- CloseHandle(hPort);
|
|
|
- }
|
|
|
+ LOG_INFO("找到并口,准备进行打印机检测! hPort:" << hPort);
|
|
|
+
|
|
|
+ if(PortTest(hPort) == true)
|
|
|
+ {
|
|
|
+ //并口测试连通,保存起来
|
|
|
+ PrinterHandle newHandle;
|
|
|
+ newHandle.hPort = hPort;
|
|
|
+ newHandle.type = 2;
|
|
|
+
|
|
|
+ m_hPorts.push_back(newHandle);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //并口打印机没有连接,那么就直接关闭掉并口,避免占用
|
|
|
+ CloseHandle(hPort);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
//开始添加串口
|
|
|
@@ -120,12 +138,15 @@ bool CPosPrinter::InitShouyin()
|
|
|
if(hPort == INVALID_HANDLE_VALUE)
|
|
|
{
|
|
|
DWORD error = GetLastError();
|
|
|
- LOG_INFO("com error:" << error << ", " << com2Str.c_str());
|
|
|
|
|
|
if(error == 2)
|
|
|
{
|
|
|
//没有指定的文件
|
|
|
- LOG_INFO("no com, " << com2Str.c_str());
|
|
|
+ LOG_INFO("没有找到对应的串口 " << com2Str.c_str());
|
|
|
+ }
|
|
|
+ else if(error == 5)
|
|
|
+ {
|
|
|
+ LOG_INFO("串口被占用 " << com2Str.c_str());
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
@@ -143,7 +164,7 @@ bool CPosPrinter::InitShouyin()
|
|
|
if(!SetCommState(hPort, &dcb))
|
|
|
{
|
|
|
LOG_INFO("set baudRate failed!");
|
|
|
- continue;
|
|
|
+ continue;
|
|
|
}
|
|
|
|
|
|
//设置端口缓冲
|
|
|
@@ -161,7 +182,7 @@ bool CPosPrinter::InitShouyin()
|
|
|
//清除通讯端口缓存
|
|
|
PurgeComm(hPort, PURGE_TXCLEAR | PURGE_RXCLEAR | PURGE_TXABORT | PURGE_RXABORT);
|
|
|
|
|
|
- //保存端口
|
|
|
+ //保存端口
|
|
|
PrinterHandle newHandle;
|
|
|
newHandle.hPort = hPort;
|
|
|
newHandle.type = 3;
|
|
|
@@ -173,14 +194,9 @@ bool CPosPrinter::InitShouyin()
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
-////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
-//获取CreateFile的USB端口号
|
|
|
-////////////////////////////////////////////////////////////////////////////////////////////////////////
|
|
|
-
|
|
|
-// 根据GUID获得设备路径
|
|
|
-// lpGuid: GUID指针
|
|
|
-// pszDevicePath: 设备路径指针的指针,用于返回找到的路径
|
|
|
-// 返回: 成功得到的设备路径个数,可能不止1个
|
|
|
+/*
|
|
|
+ *获取CreateFile的USB端口号
|
|
|
+ **/
|
|
|
int CPosPrinter::GetDevicePath(LPGUID lpGuid, LPTSTR* pszDevicePath)
|
|
|
{
|
|
|
HDEVINFO hDevInfoSet;
|
|
|
@@ -194,7 +210,7 @@ int CPosPrinter::GetDevicePath(LPGUID lpGuid, LPTSTR* pszDevicePath)
|
|
|
//这2个字符串,用于根据usb的名字对比是否为打印机设备
|
|
|
wstring strUSBPrint = TEXT("USB 打印支持");
|
|
|
|
|
|
- //xp上是英文
|
|
|
+ //xp上是英文
|
|
|
wstring strUSBPrint_EN = L"USB Printing Support";
|
|
|
|
|
|
// 取得一个该GUID相关的设备信息集句柄
|
|
|
@@ -203,7 +219,7 @@ int CPosPrinter::GetDevicePath(LPGUID lpGuid, LPTSTR* pszDevicePath)
|
|
|
NULL, // 不指定父窗口句柄
|
|
|
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE); // 目前存在的设备
|
|
|
|
|
|
- // 失败...
|
|
|
+ // 失败...
|
|
|
if(hDevInfoSet == INVALID_HANDLE_VALUE)
|
|
|
{
|
|
|
LOG_INFO("SetupDiGetClassDevs failed \r\n");
|
|
|
@@ -657,7 +673,7 @@ void CPosPrinter::PrintWaimaiOrderShouyin(CWaimaiOrder& order)
|
|
|
POS_FeedLine();
|
|
|
|
|
|
POS_FeedLine();
|
|
|
- POS_FeedLine();
|
|
|
+ POS_FeedLine();
|
|
|
|
|
|
POS_CutPaper();
|
|
|
}
|
|
|
@@ -911,7 +927,7 @@ void CPosPrinter::PrintWaimaiOrderChufang(CWaimaiOrder& order)
|
|
|
|
|
|
int CPosPrinter::WriteData(string msg)
|
|
|
{
|
|
|
- return WriteBuf(msg.c_str(), msg.length());
|
|
|
+ return WriteBuf(msg.c_str(), msg.length());
|
|
|
}
|
|
|
|
|
|
int CPosPrinter::WriteBuf(const char* buf, int len)
|
|
|
@@ -924,29 +940,31 @@ int CPosPrinter::WriteBuf(const char* buf, int len)
|
|
|
{
|
|
|
HANDLE hPort = (*it).hPort;
|
|
|
|
|
|
- if((*it).type == 1 || (*it).type == 3)
|
|
|
+ if((*it).type == 3)
|
|
|
{
|
|
|
- //usb或者串口,同步写数据
|
|
|
+ //串口,同步写数据
|
|
|
+ LOG_INFO("before writefile handle:" << hPort);
|
|
|
WriteFile(hPort, buf, len, &dwWrite, NULL);
|
|
|
+ LOG_INFO("after writefile handle:" << hPort);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- //并口,异步写数据
|
|
|
+ //usb和并口,异步写数据
|
|
|
OVERLAPPED overlap;
|
|
|
memset(&overlap, 0, sizeof(overlap));
|
|
|
BOOL rc = WriteFile(hPort, buf, len, &dwWrite, &overlap);
|
|
|
|
|
|
if(rc)
|
|
|
{
|
|
|
- //LOG_INFO("writefile success immediately, handle:" << hPort);
|
|
|
+ LOG_INFO("writefile success immediately, handle:" << hPort);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
if(GetLastError() == ERROR_IO_PENDING)
|
|
|
{
|
|
|
- //LOG_INFO("Request queued, waiting... handle:" << hPort);
|
|
|
+ LOG_INFO("Request queued, waiting... handle:" << hPort);
|
|
|
WaitForSingleObject(hPort, 5000);
|
|
|
- //LOG_INFO("Request completed, handle:" << hPort);
|
|
|
+ LOG_INFO("Request completed, handle:" << hPort);
|
|
|
DWORD numread;
|
|
|
rc = GetOverlappedResult(
|
|
|
hPort,
|
|
|
@@ -979,46 +997,42 @@ int CPosPrinter::WriteBuf(const char* buf, int len)
|
|
|
/*
|
|
|
*测试这个并口是否连通的
|
|
|
**/
|
|
|
-bool CPosPrinter::lptTest(HANDLE hPort)
|
|
|
+bool CPosPrinter::PortTest(HANDLE hPort)
|
|
|
{
|
|
|
- char chInitCode[] = "\x1B\x40";
|
|
|
-
|
|
|
- DWORD dwWrite;
|
|
|
-
|
|
|
- OVERLAPPED overlap;
|
|
|
- memset(&overlap, 0, sizeof(overlap));
|
|
|
+ char chInitCode[] = "\x1B\x40";
|
|
|
|
|
|
- BOOL rc = WriteFile(hPort, chInitCode, (DWORD)2L, &dwWrite, &overlap);
|
|
|
+ DWORD dwWrite;
|
|
|
|
|
|
- if (rc)
|
|
|
- {
|
|
|
- LOG_INFO("writefile success immediately, handle:" << hPort);
|
|
|
+ OVERLAPPED overlap;
|
|
|
+ memset(&overlap, 0, sizeof(overlap));
|
|
|
|
|
|
- return true;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- if (GetLastError() == ERROR_IO_PENDING)
|
|
|
- {
|
|
|
- //LOG_INFO("before lpt test, handle:" << hPort);
|
|
|
+ BOOL rc = WriteFile(hPort, chInitCode, (DWORD)2L, &dwWrite, &overlap);
|
|
|
|
|
|
- DWORD ret = WaitForSingleObject(hPort, 500);
|
|
|
+ if(rc)
|
|
|
+ {
|
|
|
+ LOG_INFO("writefile success immediately, handle:" << hPort);
|
|
|
|
|
|
- //LOG_INFO("after lpt test, handle" << hPort);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if(GetLastError() == ERROR_IO_PENDING)
|
|
|
+ {
|
|
|
+ DWORD ret = WaitForSingleObject(hPort, 500);
|
|
|
|
|
|
- if (ret == 0)
|
|
|
- {
|
|
|
- LOG_INFO("并口打印机已经连接!");
|
|
|
- return true;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- LOG_INFO("并口打印机未连接!");
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ if(ret == 0)
|
|
|
+ {
|
|
|
+ LOG_INFO("printer is connect, handle:" << hPort);
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ LOG_INFO("printer is not connect, handle:" << hPort);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- return false;
|
|
|
+ return false;
|
|
|
}
|
|
|
|
|
|
int CPosPrinter::POS_Reset(void)
|