zhangyang 6 år sedan
förälder
incheckning
a9bdafea90

BIN
bin/Win32/Debug/zhipuzi_pay_plugin/db/pos.db


Filskillnaden har hållts tillbaka eftersom den är för stor
+ 602 - 10904
bin/Win32/Debug/zhipuzi_pay_plugin/log/pos.log


BIN
bin/Win32/Debug/zhipuzi_pay_plugin/zhipuzi_pay_plugin.exe


BIN
zhipuzi_pay_plugin/.vs/zhipuzi_pos_windows/v15/.suo


BIN
zhipuzi_pay_plugin/.vs/zhipuzi_pos_windows/v15/Browse.VC.db


+ 0 - 2
zhipuzi_pay_plugin/network/CZhipuziHttpClient.cpp

@@ -175,8 +175,6 @@ bool CZhipuziHttpClient::RequestPingtai(std::string url, std::map<string, string
 	params["timestamp"] = timestamp;
 	params["url"] = m_client.m_pingtai_url + url;
 
-	params["timestamp"] = "1564301876";
-
 	//计算签名
 	std::string postString;
 	for (std::map<string, string>::iterator it = params.begin(); it != params.end();)

+ 50 - 0
zhipuzi_pay_plugin/tool/CComHelper.cpp

@@ -0,0 +1,50 @@
+#include "../pch/pch.h"
+#include "CComHelper.h"
+
+
+CComHelper::CComHelper()
+{
+}
+
+
+CComHelper::~CComHelper()
+{
+}
+
+std::vector<std::wstring> CComHelper::getComPort()
+{
+	std::vector<std::wstring> comVector;
+
+	HKEY hKey;
+	TCHAR portName[256], commName[256];
+
+	// 打开串口注册表对应的键值  
+	if (ERROR_SUCCESS == ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Hardware\\DeviceMap\\SerialComm", NULL, KEY_READ, &hKey))
+	{
+		int i = 0;
+		int mm = 0;
+		DWORD  dwLong, dwSize;
+		while (TRUE)
+		{
+			dwLong = dwSize = sizeof(portName);
+
+			// 枚举串口
+			if (ERROR_NO_MORE_ITEMS == ::RegEnumValue(hKey, i, portName, &dwLong, NULL, NULL, (PUCHAR)commName, &dwSize))
+			{
+				break;
+			}
+
+			comVector.push_back(commName);
+			i++;
+		}
+		// 关闭注册表
+		RegCloseKey(hKey);
+	}
+	else
+	{
+		MessageBox(NULL, L"您的计算机的注册表上没有HKEY_LOCAL_MACHINE:Hardware\\DeviceMap\\SerialComm项", L"警告", MB_OK);
+	}
+
+	// 返回串口号
+	return comVector;
+}

+ 11 - 0
zhipuzi_pay_plugin/tool/CComHelper.h

@@ -0,0 +1,11 @@
+#pragma once
+
+class CComHelper
+{
+public:
+	CComHelper();
+	~CComHelper();
+
+	std::vector<std::wstring> getComPort();
+};
+

+ 160 - 0
zhipuzi_pay_plugin/tool/CSerialPort.cpp

@@ -0,0 +1,160 @@
+#include "../pch/pch.h"
+#include "CSerialPort.h"
+
+CSerialPort::CSerialPort(
+	const std::wstring portNum,
+	DWORD baudRate /* = 9600 */,
+	BYTE byteSize /* = 8 */,
+	BYTE parityBit /* = NOPARITY */,
+	BYTE stopBit /* = ONESTOPBIT */
+) : m_portNum(portNum),
+m_dwBaudRate(baudRate),
+m_byteSize(byteSize),
+m_parityBit(parityBit),
+m_stopBit(stopBit),
+m_bOpen(false)
+{
+
+}
+
+CSerialPort::~CSerialPort()
+{
+
+}
+
+// 打开串口成功,返回 true
+
+bool CSerialPort::openComm()
+{
+	m_hComm = CreateFile(m_portNum.c_str(),
+		GENERIC_READ | GENERIC_WRITE, //允许读和写
+		FILE_SHARE_READ | FILE_SHARE_WRITE,		//独占方式
+		NULL, OPEN_EXISTING,   //打开而不是创建
+		0,   //同步方式
+		NULL
+	);
+
+	if (m_hComm == INVALID_HANDLE_VALUE)
+	{
+		int error = GetLastError();
+		int a = error;
+
+		return false;
+
+	}
+	else
+	{
+		DCB dcb;
+		SetupComm(m_hComm, MAX_BUFFER_SIZE, MAX_BUFFER_SIZE);	// 设置读写缓冲区大小
+		GetCommState(m_hComm, &dcb);
+		dcb.BaudRate = m_dwBaudRate;
+		dcb.ByteSize = m_byteSize;
+		dcb.Parity = m_parityBit;
+		dcb.StopBits = m_stopBit;
+
+		if (!SetCommState(m_hComm, &dcb))
+		{
+			TCHAR szBuf[1024] = { 0 };
+			wsprintf(szBuf, L"串口设置失败,错误代码: %d", GetLastError());
+			MessageBox(NULL, szBuf, TEXT("ERROR"), MB_OK);
+			return false;
+		}
+
+	}
+
+	//在读写串口前,用 PurgeComm 函数清空缓冲区
+	PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_TXABORT | PURGE_TXABORT);
+
+	m_bOpen = true;
+
+	return true;
+
+}
+
+// 关闭串口
+
+void CSerialPort::closeComm()
+{
+	CloseHandle(m_hComm);
+}
+
+
+// 向串口发送数据
+bool CSerialPort::writeToComm(BYTE data[], DWORD dwLength)
+{
+#ifdef _DEBUG
+	assert(m_bOpen == true || dwLength > 0);
+	//return false;
+#endif // _DEBUG
+	DWORD dwError = 0;
+	if (ClearCommError(m_hComm, &dwError, NULL) && dwError > 0)
+	{
+		PurgeComm(m_hComm, PURGE_TXABORT | PURGE_TXCLEAR);
+	}
+
+	DWORD dwTx = 0;
+	BOOL ret = FALSE;
+	ret = WriteFile(m_hComm, data, dwLength, &dwTx, NULL);
+
+	if (ret == FALSE)
+	{
+		TCHAR szBuf[1024] = { 0 };
+		wsprintf(szBuf, _T("读取数据失败,错误代码: %d"), GetLastError());
+		MessageBox(NULL, szBuf, L"ERROR", MB_OK);
+
+		return false;
+	}
+
+	return true;
+
+
+}
+
+// 从串口中读取数据
+
+bool CSerialPort::readFromComm(char buffer[], DWORD dwLength)
+{
+#ifdef _DEBUG
+	assert(m_bOpen == true || dwLength > 0);
+	//return false;
+#endif // _DEBUG
+
+	COMSTAT comStat;
+	DWORD dwError = 0;
+	if (ClearCommError(m_hComm, &dwError, &comStat) && dwError > 0)
+	{
+		PurgeComm(m_hComm, PURGE_RXABORT | PURGE_RXCLEAR);
+	}
+
+	DWORD dwRx = 0;		// 读入的字节数
+	BOOL ret = FALSE;
+	BYTE* byReadData = new BYTE[dwLength];
+	char szTmp[4] = { 0 };
+	int sizeOfBytes = sizeof(szTmp);
+	ret = ReadFile(m_hComm, byReadData, dwLength, &dwRx, NULL);	// 读入数据
+
+	if (ret == TRUE)
+	{
+		LOG_INFO("read num:" << dwRx);
+		for (int i = 0; i < dwRx; ++i)
+		{
+			sprintf_s(szTmp, "%02x", byReadData[i]);
+			strcat_s(buffer, sizeOfBytes*dwLength, szTmp);
+		}
+
+		// 释放内存
+		delete byReadData;
+
+		return true;
+	}
+	else
+	{
+		TCHAR szBuf[1024] = { 0 };
+		wsprintf(szBuf, _T("数据读取失败,错误代码: %d"), GetLastError());
+		MessageBox(NULL, szBuf, L"Error", MB_OK);
+
+		return false;
+	}
+
+	return true;
+}

+ 93 - 0
zhipuzi_pay_plugin/tool/CSerialPort.h

@@ -0,0 +1,93 @@
+#ifndef _SERIAL_H
+#define _SERIAL_H
+
+#include <string>
+#include <Windows.h>
+#include <cstddef>
+#include <cstdlib>
+#include <cassert>
+
+typedef unsigned long ulong;
+typedef unsigned char uchar;
+
+
+class CSerialPort
+{
+public:
+	CSerialPort(
+		std::wstring portNum,		// 串口号
+		DWORD baudRate = 9600,			// 波特率
+		BYTE  byteSize = 8,				// 数据位
+		BYTE  parityBit = NOPARITY,		// 检验位
+		BYTE  stopBit = ONESTOPBIT		// 停止位
+	);
+
+	~CSerialPort();
+
+public:
+
+	bool openComm();										// 打开串口
+	void closeComm();										// 关闭串口
+	bool writeToComm(BYTE data[], DWORD dwLegnth);			// 发送数据
+	bool readFromComm(char buffer[], DWORD dwLength);		// 读取数据
+
+private:
+
+	HANDLE m_hComm;		// 通信设备
+	std::wstring m_portNum; // 串口号
+	DWORD m_dwBaudRate; // 波特率
+	BYTE  m_byteSize;	// 数据位
+	BYTE  m_parityBit;  // 校验位
+	BYTE  m_stopBit;	// 停止位
+	bool  m_bOpen;		// 串口开关标志
+private:
+
+	enum BufferSize
+	{
+		MIN_BUFFER_SIZE = 256,
+		BUFFER_SIZE = 512,
+		MAX_BUFFER_SIZE = 1024
+	};
+
+	// 设置串口号
+	void setPortNum(const std::wstring &portNum)
+	{
+		this->m_portNum = portNum;
+	}
+	// 设置波特率
+	void setBaudRate(const ulong baudRate)
+	{
+		this->m_dwBaudRate = baudRate;
+	}
+	// 设置数据位
+	void setByteSize(const uchar byteSize)
+	{
+		this->m_byteSize = byteSize;
+	}
+	// 设置检验位
+	void setParityBit(const uchar parityBit)
+	{
+		this->m_parityBit = parityBit;
+	}
+	// 设置停止位
+	void setStopBit(const uchar stopBit)
+	{
+		this->m_stopBit = stopBit;
+	}
+
+	// 获取串口号
+	std::wstring getPortNum() { return m_portNum; }
+	// 获取波特率
+	ulong getBaudRate() { return m_dwBaudRate; }
+	// 获取数据位
+	uchar getByteSize() { return m_byteSize; }
+	// 获取检验位
+	uchar getParityBit() { return m_parityBit; }
+	// 获取停止位
+	uchar getStopBit() { return m_stopBit; }
+};
+
+
+
+
+#endif		// _SERIAL_H

+ 9 - 7
zhipuzi_pay_plugin/wnd/CLoginWnd.cpp

@@ -2,6 +2,8 @@
 #include "CLoginWnd.h"
 #include "CUpdateWnd.h"
 
+#include "CValueWnd.h"
+
 #include "CMessageboxWnd.h"
 
 void CLoginWnd::Init()
@@ -545,17 +547,17 @@ void CLoginWnd::HandleLogin()
 
 void CLoginWnd::LoginSuccess()
 {
-    CMainWnd* pFrame = new CMainWnd();
-    if(pFrame == NULL)
-    {
-        return;
-    }
+	CValueWnd* pFrame = new CValueWnd();
+	if (pFrame == NULL)
+	{
+		return;
+	}
 
     pFrame->SetIcon(IDI_ICON_DUILIB);
-    pFrame->Create(NULL, _T("智铺子收银插件"), UI_WNDSTYLE_FRAME, 0L, 0, 0, 1024, 768);
+    pFrame->Create(NULL, _T("智铺子收银插件金额"), UI_WNDSTYLE_FRAME, 0L, 0, 0, 0, 0);
     pFrame->CenterWindow();
 
-    ::ShowWindow(*pFrame, SW_SHOWMAXIMIZED);
+    ::ShowWindow(*pFrame, SW_SHOWNORMAL);
 
     Close();
 }

+ 67 - 4
zhipuzi_pay_plugin/wnd/CValueWnd.cpp

@@ -1,6 +1,10 @@
 #include "../pch/pch.h"
 #include "CValueWnd.h"
 
+#include "../tool/CComHelper.h"
+
+#include "../tool/CSerialPort.h"
+
 void CValueWnd::Notify(TNotifyUI& msg)
 {
 	if (msg.sType == _T("click"))
@@ -13,6 +17,67 @@ void CValueWnd::Notify(TNotifyUI& msg)
 	}
 }
 
+void CValueWnd::Init()
+{
+	std::thread(&CValueWnd::ReadValue, this).detach();
+}
+
+void CValueWnd::ReadValue()
+{
+	//读取串口的模式
+	CComHelper helper;
+	std::vector<std::wstring> comVector = helper.getComPort();
+
+	for (std::vector<std::wstring>::iterator it = comVector.begin(); it != comVector.end(); it++)
+	{
+		std::wstring comName = *it;
+		LOG_INFO("comName:" << comName.c_str());
+	}
+
+	int nReadType = 1;
+
+	if (nReadType == 1)
+	{
+		std::wstring com_name = L"COM1";
+		CSerialPort serial(com_name.c_str(), 2400);
+
+		bool ret = serial.openComm();
+		if (!ret)
+		{
+			LOG_INFO("open com failed!");
+			return;
+		}
+
+		HANDLE m_hComm = CreateFile(L"COM1",
+			GENERIC_READ | GENERIC_WRITE, //允许读和写
+			FILE_SHARE_READ | FILE_SHARE_WRITE,		//独占方式
+			NULL, OPEN_EXISTING,   //打开而不是创建
+			0,   //同步方式
+			NULL
+		);
+
+		if (m_hComm == INVALID_HANDLE_VALUE)
+		{
+			int error = GetLastError();
+			int a = error;
+
+		}
+
+		while (true)
+		{
+			//开始读取串口的数据			
+			char a[10] = { 0 };
+			serial.readFromComm(a, 1);
+
+			LOG_INFO("a:" << a);
+
+			Sleep(200);
+		}
+
+		serial.closeComm();
+	}
+}
+
 LRESULT CValueWnd::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
 {
 	LONG styleValue = ::GetWindowLong(*this, GWL_STYLE);
@@ -35,6 +100,8 @@ LRESULT CValueWnd::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHand
 	// 把自己加入到CPaintManagerUI的m_aNotifiers数组中,用于处理Notify函数
 	m_pm.AddNotifier(this);
 
+	Init();
+
 	return 0;
 }
 
@@ -141,10 +208,6 @@ LRESULT CValueWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
 	case WM_NCHITTEST:
 		lRes = OnNcHitTest(uMsg, wParam, lParam, bHandled);
 		break;
-
-	case WM_SIZE:
-		lRes = OnSize(uMsg, wParam, lParam, bHandled);
-		break;
 	default:
 		bHandled = FALSE;
 	}

+ 4 - 0
zhipuzi_pay_plugin/wnd/CValueWnd.h

@@ -42,6 +42,10 @@ public:
 
 	LRESULT MessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, bool& bHandled);
 
+	void Init();
+
+	void ReadValue();
+
 public:
 	CPaintManagerUI m_pm;
 };

+ 9 - 21
zhipuzi_pay_plugin/zhipuzi_pay_plugin.cpp

@@ -1,7 +1,6 @@
 #include "pch/pch.h"
 
 #include "wnd/CLoginWnd.h"
-#include "wnd/CValueWnd.h"
 
 #include <curl/curl.h>
 
@@ -36,27 +35,16 @@ int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
     }
 
     //先显示登录窗口
-    //CLoginWnd* pLogin = new CLoginWnd();
-    //if(pLogin == NULL)
-    //{
-    //    return 0;
-    //}
-
-    //pLogin->Create(NULL, _T("智铺子收银插件登录"), UI_WNDSTYLE_DIALOG, 0, 0, 0, 0, 0, NULL);
-    //pLogin->SetIcon(IDI_ICON_DUILIB);
-    //pLogin->CenterWindow();
-    //pLogin->ShowModal();
-
-	CValueWnd* pLogin = new CValueWnd();
-	if (pLogin == NULL)
-	{
-		return 0;
-	}
+    CLoginWnd* pLogin = new CLoginWnd();
+    if(pLogin == NULL)
+    {
+        return 0;
+    }
 
-	pLogin->Create(NULL, _T("收银金额"), UI_WNDSTYLE_CHILD, 0, 0, 0, 0, 0, NULL);
-	pLogin->SetIcon(IDI_ICON_DUILIB);
-	pLogin->CenterWindow();
-	pLogin->ShowModal();
+    pLogin->Create(NULL, _T("智铺子收银插件登录"), UI_WNDSTYLE_DIALOG, 0, 0, 0, 0, 0, NULL);
+    pLogin->SetIcon(IDI_ICON_DUILIB);
+    pLogin->CenterWindow();
+    pLogin->ShowModal();
 
     CPaintManagerUI::MessageLoop();
 

+ 4 - 0
zhipuzi_pay_plugin/zhipuzi_pay_plugin.vcxproj

@@ -219,6 +219,8 @@ copy $(ProjectDir)conf\ $(SolutionDir)bin\$(Platform)\$(Configuration)\conf\</Co
     </PostBuildEvent>
   </ItemDefinitionGroup>
   <ItemGroup>
+    <ClInclude Include="tool\CSerialPort.h" />
+    <ClInclude Include="tool\CComHelper.h" />
     <ClInclude Include="wnd\CValueWnd.h" />
     <ClInclude Include="wnd\CMessageboxWnd.h" />
     <ClInclude Include="wnd\CUpdateWnd.h" />
@@ -250,6 +252,8 @@ copy $(ProjectDir)conf\ $(SolutionDir)bin\$(Platform)\$(Configuration)\conf\</Co
     <ClInclude Include="control\OrderListUI.h" />
   </ItemGroup>
   <ItemGroup>
+    <ClCompile Include="tool\CSerialPort.cpp" />
+    <ClCompile Include="tool\CComHelper.cpp" />
     <ClCompile Include="wnd\CValueWnd.cpp" />
     <ClCompile Include="wnd\CMessageboxWnd.cpp" />
     <ClCompile Include="wnd\CUpdateWnd.cpp" />

+ 12 - 0
zhipuzi_pay_plugin/zhipuzi_pay_plugin.vcxproj.filters

@@ -102,6 +102,12 @@
     <ClInclude Include="wnd\CValueWnd.h">
       <Filter>头文件</Filter>
     </ClInclude>
+    <ClInclude Include="tool\CComHelper.h">
+      <Filter>头文件</Filter>
+    </ClInclude>
+    <ClInclude Include="tool\CSerialPort.h">
+      <Filter>头文件</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="pch\pch.cpp">
@@ -182,6 +188,12 @@
     <ClCompile Include="wnd\CValueWnd.cpp">
       <Filter>源文件</Filter>
     </ClCompile>
+    <ClCompile Include="tool\CComHelper.cpp">
+      <Filter>源文件</Filter>
+    </ClCompile>
+    <ClCompile Include="tool\CSerialPort.cpp">
+      <Filter>源文件</Filter>
+    </ClCompile>
   </ItemGroup>
   <ItemGroup>
     <Image Include="resource\zhipuzi.ico">