|
|
@@ -45,10 +45,12 @@ void CPosPrinter::InitUsb(int usbType)
|
|
|
|
|
|
LOG_INFO("准备打开端口 Port = " << m_usb_devices[i].c_str());
|
|
|
|
|
|
- HANDLE hPort = CreateFile(m_usb_devices[i].c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
|
|
|
+ HANDLE hPort = CreateFile(m_usb_devices[i].c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
|
|
if(hPort == INVALID_HANDLE_VALUE)
|
|
|
{
|
|
|
+ LOG_INFO("打开USB端口失败, Port = " << m_usb_devices[i].c_str());
|
|
|
+
|
|
|
// 打开端口失败
|
|
|
DWORD error = GetLastError();
|
|
|
|
|
|
@@ -65,7 +67,7 @@ void CPosPrinter::InitUsb(int usbType)
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
- LOG_INFO("打开usb端口,准备进行打印机检测! hPort:" << hPort);
|
|
|
+ LOG_INFO("打开usb端口成功,准备进行打印机检测! hPort:" << hPort);
|
|
|
|
|
|
if(PortTest(hPort) == true)
|
|
|
{
|
|
|
@@ -93,10 +95,12 @@ bool CPosPrinter::InitOneUsb(wstring usb_path)
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
- HANDLE hPort = CreateFile(usb_path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
|
|
|
+ HANDLE hPort = CreateFile(usb_path.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
|
|
if(hPort == INVALID_HANDLE_VALUE)
|
|
|
{
|
|
|
+ LOG_INFO("打开USB端口失败, Port = " << usb_path.c_str());
|
|
|
+
|
|
|
// 打开端口失败
|
|
|
DWORD error = GetLastError();
|
|
|
|
|
|
@@ -139,10 +143,12 @@ bool CPosPrinter::InitBingkou()
|
|
|
{
|
|
|
std::wstring LptStr = L"lpt1";
|
|
|
|
|
|
- HANDLE hPort = CreateFile(LptStr.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, NULL);
|
|
|
+ HANDLE hPort = CreateFile(LptStr.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
|
|
|
|
|
|
if(hPort == INVALID_HANDLE_VALUE)
|
|
|
{
|
|
|
+ LOG_INFO("打开并口失败, Port = " << LptStr.c_str());
|
|
|
+
|
|
|
DWORD error = GetLastError();
|
|
|
|
|
|
if(error == 2)
|
|
|
@@ -254,16 +260,13 @@ void CPosPrinter::InitCom()
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
- *找到所有可用的小票打印机,包括USB、并口、串口3个类型,注意这里不包含网口
|
|
|
+ *找到所有可用的小票打印机,这里只初始化USB,不包含并口、串口、网口
|
|
|
*智能识别模式不再自动处理串口,串口的需要人工去选择类型
|
|
|
**/
|
|
|
void CPosPrinter::InitShouyin()
|
|
|
{
|
|
|
//开始添加usb
|
|
|
InitUsb();
|
|
|
-
|
|
|
- //开始添加并口的端口
|
|
|
- InitBingkou();
|
|
|
}
|
|
|
|
|
|
/*
|
|
|
@@ -427,6 +430,8 @@ int CPosPrinter::GetPrinterType(wstring vid, wstring pid)
|
|
|
**/
|
|
|
void CPosPrinter::PrintWaimaiOrderShouyin(CWaimaiOrder& order)
|
|
|
{
|
|
|
+ LOG_INFO("_______________________________开始打印收银小票,订单编号:"<<order.m_order_num.c_str()<<"_________________________");
|
|
|
+
|
|
|
//设置模式,后面输出数据不会错
|
|
|
m_type = 1;
|
|
|
|
|
|
@@ -905,8 +910,12 @@ void CPosPrinter::PrintWaimaiOrderShouyin(CWaimaiOrder& order)
|
|
|
{
|
|
|
m_socket.close();
|
|
|
}
|
|
|
+
|
|
|
+ LOG_INFO("_______________________________结束打印收银小票,订单编号:" << order.m_order_num.c_str() << "_________________________");
|
|
|
+ LOG_INFO(" ");
|
|
|
}
|
|
|
|
|
|
+
|
|
|
void CPosPrinter::PrintWaimaiOrderBiaoqian(CWaimaiOrder& order)
|
|
|
{
|
|
|
m_type = 1;
|
|
|
@@ -1314,52 +1323,26 @@ int CPosPrinter::WriteData(string msg)
|
|
|
return WriteBuf((unsigned char*)msg.c_str(), msg.length());
|
|
|
}
|
|
|
|
|
|
+/*
|
|
|
+ *这个函数只负责把数据拼接到打印缓存里面,不负责实际的发送数据
|
|
|
+ **/
|
|
|
int CPosPrinter::WriteBuf(const unsigned char* buf, int len)
|
|
|
{
|
|
|
if(m_type == 1)
|
|
|
{
|
|
|
//USB、串口、并口,走这里
|
|
|
- DWORD dwWrite;
|
|
|
-
|
|
|
for(std::vector<PrinterHandle>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
|
|
|
{
|
|
|
HANDLE hPort = (*it).hPort;
|
|
|
|
|
|
- if((*it).type == 3)
|
|
|
- {
|
|
|
- //串口,同步写数据
|
|
|
- WriteFile(hPort, buf, len, &dwWrite, NULL);
|
|
|
- }
|
|
|
- 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);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- if(GetLastError() == ERROR_IO_PENDING)
|
|
|
- {
|
|
|
- /*WaitForSingleObject(hPort, 10000);
|
|
|
-
|
|
|
- DWORD numread;
|
|
|
- rc = GetOverlappedResult(
|
|
|
- hPort,
|
|
|
- &overlap,
|
|
|
- &numread,
|
|
|
- FALSE
|
|
|
- );
|
|
|
+ //同步写数据
|
|
|
+ DWORD dwWrite;
|
|
|
|
|
|
- LOG_INFO("Write success " << numread << " bytes");*/
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
+ BOOL ret = WriteFile(hPort, buf, len, &dwWrite, NULL);
|
|
|
+ if (!ret)
|
|
|
+ {
|
|
|
+ LOG_INFO("WriteFile Failed!, hPort:"<<hPort<<", type:"<<(*it).type);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
return 1;
|
|
|
@@ -1376,7 +1359,7 @@ int CPosPrinter::WriteBuf(const unsigned char* buf, int len)
|
|
|
LOG_INFO("网口打印机发送消息失败,错误信息:" << e.what());
|
|
|
}
|
|
|
|
|
|
- return 0;
|
|
|
+ return 1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -1390,34 +1373,19 @@ bool CPosPrinter::PortTest(HANDLE hPort)
|
|
|
|
|
|
DWORD dwWrite;
|
|
|
|
|
|
- OVERLAPPED overlap;
|
|
|
- memset(&overlap, 0, sizeof(overlap));
|
|
|
-
|
|
|
- BOOL rc = WriteFile(hPort, chInitCode, 2, &dwWrite, &overlap);
|
|
|
+ BOOL rc = WriteFile(hPort, chInitCode, 2, &dwWrite, NULL);
|
|
|
|
|
|
if(rc)
|
|
|
{
|
|
|
- LOG_INFO("writefile success immediately, handle:" << hPort);
|
|
|
+ LOG_INFO("porttest success, handle:" << hPort);
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- if(GetLastError() == ERROR_IO_PENDING)
|
|
|
- {
|
|
|
- DWORD ret = WaitForSingleObject(hPort, 1000);
|
|
|
+ LOG_INFO("porttest fail, handle:" << hPort);
|
|
|
|
|
|
- if(ret == 0)
|
|
|
- {
|
|
|
- LOG_INFO("printer is connect, handle:" << hPort);
|
|
|
-
|
|
|
- return true;
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- LOG_INFO("printer is not connect, handle:" << hPort);
|
|
|
- }
|
|
|
- }
|
|
|
+ return true;
|
|
|
}
|
|
|
|
|
|
return false;
|