Browse Source

多个usb打印机的问题

zhangyang 6 years ago
parent
commit
01b1d8e517

BIN
bin/Win32/Debug/zhipuzi_pos_windows/zhipuzi_pos_windows.exe


BIN
bin/Win32/Release/setup/zhipuzi_pos_windows_setup_1.0.0.1.exe


+ 32 - 18
zhipuzi_pos_windows/tool/CPosPrinter.cpp

@@ -40,22 +40,25 @@ bool CPosPrinter::InitShouyin()
     LOG_INFO("nDevice:" << nDevice);
     int i = 0;
 
-    while(i < nDevice)
-    {
-        Port = szDevicePath[i++];
-        LOG_INFO("device.Port = " << Port);
-    }
-
-    m_hPort = CreateFile(Port, GENERIC_READ | GENERIC_WRITE,
-                         0, NULL,
-                         OPEN_EXISTING,
-                         FILE_ATTRIBUTE_NORMAL, NULL);
-
-    if(m_hPort == INVALID_HANDLE_VALUE)
-    {
-        // 打开端口失败
-        return false;
-    }
+	while (i < nDevice)
+	{
+		Port = szDevicePath[i++];
+		//LOG_INFO("device.Port = " << Port);
+
+		HANDLE hPort = CreateFile(Port, GENERIC_READ | GENERIC_WRITE,
+			0, NULL,
+			OPEN_EXISTING,
+			FILE_ATTRIBUTE_NORMAL, NULL);
+
+		if (hPort == INVALID_HANDLE_VALUE)
+		{
+			// 打开端口失败
+			continue;
+		}
+
+		//把当前发现的保存下来
+		m_hPorts.push_back(hPort);
+	}
 
     return true;
 }
@@ -819,7 +822,13 @@ int CPosPrinter::WriteData(string msg)
     if(m_type == 1)
     {
         DWORD dwWrite;
-        return WriteFile(m_hPort, msg.c_str(), (DWORD)msg.length(), &dwWrite, NULL);
+        
+		for (std::vector<HANDLE>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
+		{
+			WriteFile(*it, msg.c_str(), (DWORD)msg.length(), &dwWrite, NULL);
+		}
+
+		return 1;
     }
 
     else
@@ -843,7 +852,12 @@ int CPosPrinter::WriteBuf(char* buf, int len)
     if(m_type == 1)
     {
         DWORD dwWrite;
-        return WriteFile(m_hPort, buf, len, &dwWrite, NULL);
+        
+		for (std::vector<HANDLE>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
+		{
+			WriteFile(*it, buf, len, &dwWrite, NULL);
+		}
+		return 1;
     }
 
     else

+ 1 - 1
zhipuzi_pos_windows/tool/CPosPrinter.h

@@ -55,7 +55,7 @@ private:
 	std::string HandleFoodTotalPrice(std::string oldprice, int guige = 1);
 
 private:
-	HANDLE m_hPort = NULL;
+	std::vector<HANDLE> m_hPorts;
 
 	boost::asio::io_service m_io;
 	boost::asio::ip::tcp::socket m_socket;