Browse Source

兼容并口和串口打印机

zhangyang 6 years ago
parent
commit
aeabd1a23f

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


+ 10 - 0
zhipuzi_pos_windows/helper/CLewaimaiString.cpp

@@ -207,6 +207,11 @@ std::wstring CLewaimaiString::UTF8ToUnicode(const std::string& str)
 std::string CLewaimaiString::UnicodeToANSI(const std::wstring& wstr)
 {
     unsigned len = wstr.size() * 4;
+	if (len == 0)
+	{
+		return "";
+	}
+
     setlocale(LC_CTYPE, "");
     char *p = new char[len];
     wcstombs(p, wstr.c_str(), len);
@@ -218,6 +223,11 @@ std::string CLewaimaiString::UnicodeToANSI(const std::wstring& wstr)
 std::wstring CLewaimaiString::ANSIToUnicode(const std::string& str)
 {
     unsigned len = str.size() * 2;// 预留字节数
+	if (len == 0)
+	{
+		return L"";
+	}
+
     setlocale(LC_CTYPE, "");     //必须调用此函数
     wchar_t *p = new wchar_t[len];// 申请一段内存存放转换后的字符串
     mbstowcs(p, str.c_str(), len);// 转换

+ 73 - 6
zhipuzi_pos_windows/tool/CPosPrinter.cpp

@@ -17,9 +17,12 @@ CPosPrinter::~CPosPrinter()
 
 }
 
+/*
+ *找到所有可用的小票打印机,包括USB、并口、串口3个类型
+ **/
 bool CPosPrinter::InitShouyin()
 {
-    //遍历USB设备,找到POS打印机路径
+	//先找USB的
 
     //设备路径
     TCHAR* szDevicePath[MAX_DEVICE];
@@ -37,13 +40,14 @@ bool CPosPrinter::InitShouyin()
 
     //取设备路径
     int nDevice = GetDevicePath((LPGUID)&USB_GUID, szDevicePath);
-    LOG_INFO("nDevice:" << nDevice);
+    //LOG_INFO("nDevice:" << nDevice);
     int i = 0;
 
-	while (i < nDevice)
-	{
-		Port = szDevicePath[i++];
-		//LOG_INFO("device.Port = " << Port);
+	//添加usb端口
+    while(i < nDevice)
+    {
+        Port = szDevicePath[i++];
+        //LOG_INFO("device.Port = " << Port);
 
 		HANDLE hPort = CreateFile(Port, GENERIC_READ | GENERIC_WRITE,
 			0, NULL,
@@ -58,6 +62,63 @@ bool CPosPrinter::InitShouyin()
 
 		//把当前发现的保存下来
 		m_hPorts.push_back(hPort);
+    }
+
+	//开始添加并口的端口
+	std::wstring LptStr = L"lpt1";
+	HANDLE hPort = CreateFile(LptStr.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+	if (hPort == INVALID_HANDLE_VALUE)
+	{
+		DWORD error = GetLastError();
+		//LOG_INFO("error:" << error);
+
+		if (error == 2)
+		{
+			//没有指定的文件
+			//LOG_INFO("no lpt1!");
+		}
+	}
+	else
+	{
+		//这个表示并口可以使用
+		m_hPorts.push_back(hPort);
+	}
+
+	//开始添加串口
+	int comNum = 9;
+	for (int i = 1; i <= 9; i++)
+	{
+		std::wstring com2Str = L"com" + CLewaimaiString::ANSIToUnicode(to_string(i));
+		hPort = CreateFile(com2Str.c_str(), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
+		if (hPort == INVALID_HANDLE_VALUE)
+		{
+			DWORD error = GetLastError();
+			//LOG_INFO("error:" << error);
+
+			if (error == 2)
+			{
+				//没有指定的文件
+				//LOG_INFO("no com2!");
+			}
+		}
+		else
+		{
+			//扎到了可用的串口,设置串口波特率
+			DCB dcb;
+			dcb.DCBlength = sizeof(dcb);
+
+			GetCommState(hPort, &dcb);
+
+			//佳博的串口打印机,默认是这个波特率
+			dcb.BaudRate = 19200;
+
+			if (!SetCommState(hPort, &dcb))
+			{
+				LOG_INFO("set baudRate failed!");
+			}
+
+			m_hPorts.push_back(hPort);
+		}
 	}
 
     return true;
@@ -387,6 +448,12 @@ void CPosPrinter::PrintWaimaiOrderShouyin(CWaimaiOrder& order)
 
         POS_CutPaper();
     }
+	
+	//关闭设备
+	for (std::vector<HANDLE>::iterator it = m_hPorts.begin(); it != m_hPorts.end(); it++)
+	{
+		CloseHandle(*it);
+	}
 }
 
 void CPosPrinter::PrintWaimaiOrderChufang(CWaimaiOrder& order)