zhangyang 6 лет назад
Родитель
Сommit
204fd2c525

+ 0 - 0
bin/Win32/Debug/zhipuzi_pay_plugin/log/pos_error.log


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


+ 103 - 101
zhipuzi_pay_plugin/tool/CSerialPort.cpp

@@ -2,17 +2,17 @@
 #include "CSerialPort.h"
 #include "CSerialPort.h"
 
 
 CSerialPort::CSerialPort(
 CSerialPort::CSerialPort(
-	const std::wstring portNum,
-	DWORD baudRate /* = 9600 */,
-	BYTE byteSize /* = 8 */,
-	BYTE parityBit /* = NOPARITY */,
-	BYTE stopBit /* = ONESTOPBIT */
+    const std::wstring portNum,
+    DWORD baudRate /* = 9600 */,
+    BYTE byteSize /* = 8 */,
+    BYTE parityBit /* = NOPARITY */,
+    BYTE stopBit /* = ONESTOPBIT */
 ) : m_portNum(portNum),
 ) : m_portNum(portNum),
-m_dwBaudRate(baudRate),
-m_byteSize(byteSize),
-m_parityBit(parityBit),
-m_stopBit(stopBit),
-m_bOpen(false)
+    m_dwBaudRate(baudRate),
+    m_byteSize(byteSize),
+    m_parityBit(parityBit),
+    m_stopBit(stopBit),
+    m_bOpen(false)
 {
 {
 
 
 }
 }
@@ -26,48 +26,48 @@ CSerialPort::~CSerialPort()
 
 
 bool CSerialPort::openComm()
 bool CSerialPort::openComm()
 {
 {
-	m_hComm = CreateFile(m_portNum.c_str(),
-		GENERIC_READ | GENERIC_WRITE, //允许读和写
-		0,		//独占方式
-		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;
+    m_hComm = CreateFile(m_portNum.c_str(),
+                         GENERIC_READ | GENERIC_WRITE, //允许读和写
+                         0,		//独占方式
+                         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;
 
 
 }
 }
 
 
@@ -75,7 +75,7 @@ bool CSerialPort::openComm()
 
 
 void CSerialPort::closeComm()
 void CSerialPort::closeComm()
 {
 {
-	CloseHandle(m_hComm);
+    CloseHandle(m_hComm);
 }
 }
 
 
 
 
@@ -83,29 +83,30 @@ void CSerialPort::closeComm()
 bool CSerialPort::writeToComm(BYTE data[], DWORD dwLength)
 bool CSerialPort::writeToComm(BYTE data[], DWORD dwLength)
 {
 {
 #ifdef _DEBUG
 #ifdef _DEBUG
-	assert(m_bOpen == true || dwLength > 0);
-	//return false;
+    assert(m_bOpen == true || dwLength > 0);
+    //return false;
 #endif // _DEBUG
 #endif // _DEBUG
-	DWORD dwError = 0;
-	if (ClearCommError(m_hComm, &dwError, NULL) && dwError > 0)
-	{
-		PurgeComm(m_hComm, PURGE_TXABORT | PURGE_TXCLEAR);
-	}
+    DWORD dwError = 0;
 
 
-	DWORD dwTx = 0;
-	BOOL ret = FALSE;
-	ret = WriteFile(m_hComm, data, dwLength, &dwTx, NULL);
+    if(ClearCommError(m_hComm, &dwError, NULL) && dwError > 0)
+    {
+        PurgeComm(m_hComm, PURGE_TXABORT | PURGE_TXCLEAR);
+    }
 
 
-	if (ret == FALSE)
-	{
-		TCHAR szBuf[1024] = { 0 };
-		wsprintf(szBuf, _T("读取数据失败,错误代码: %d"), GetLastError());
-		MessageBox(NULL, szBuf, L"ERROR", MB_OK);
+    DWORD dwTx = 0;
+    BOOL ret = FALSE;
+    ret = WriteFile(m_hComm, data, dwLength, &dwTx, NULL);
 
 
-		return false;
-	}
+    if(ret == FALSE)
+    {
+        TCHAR szBuf[1024] = { 0 };
+        wsprintf(szBuf, _T("读取数据失败,错误代码: %d"), GetLastError());
+        MessageBox(NULL, szBuf, L"ERROR", MB_OK);
 
 
-	return true;
+        return false;
+    }
+
+    return true;
 
 
 
 
 }
 }
@@ -115,36 +116,37 @@ bool CSerialPort::writeToComm(BYTE data[], DWORD dwLength)
 bool CSerialPort::readFromComm(char buffer[], DWORD dwLength)
 bool CSerialPort::readFromComm(char buffer[], DWORD dwLength)
 {
 {
 #ifdef _DEBUG
 #ifdef _DEBUG
-	assert(m_bOpen == true || dwLength > 0);
-	//return false;
+    assert(m_bOpen == true || dwLength > 0);
+    //return false;
 #endif // _DEBUG
 #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;
-	ret = ReadFile(m_hComm, buffer, dwLength, &dwRx, NULL);	// 读入数据
-
-	if (ret == TRUE)
-	{
-		LOG_INFO("read num:" << dwRx);
-		
-
-		return true;
-	}
-	else
-	{
-		TCHAR szBuf[1024] = { 0 };
-		wsprintf(szBuf, _T("数据读取失败,错误代码: %d"), GetLastError());
-		MessageBox(NULL, szBuf, L"Error", MB_OK);
-
-		return false;
-	}
-
-	return true;
+    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;
+    ret = ReadFile(m_hComm, buffer, dwLength, &dwRx, NULL);	// 读入数据
+
+    if(ret == TRUE)
+    {
+        LOG_INFO("read num:" << dwRx);
+
+
+        return true;
+    }
+    else
+    {
+        TCHAR szBuf[1024] = { 0 };
+        wsprintf(szBuf, _T("数据读取失败,错误代码: %d"), GetLastError());
+        MessageBox(NULL, szBuf, L"Error", MB_OK);
+
+        return false;
+    }
+
+    return true;
 }
 }

+ 79 - 74
zhipuzi_pay_plugin/tool/CSerialPort.h

@@ -1,10 +1,5 @@
-#ifndef _SERIAL_H
-#define _SERIAL_H
+#pragma once
 
 
-#include <string>
-#include <Windows.h>
-#include <cstddef>
-#include <cstdlib>
 #include <cassert>
 #include <cassert>
 
 
 typedef unsigned long ulong;
 typedef unsigned long ulong;
@@ -14,80 +9,90 @@ typedef unsigned char uchar;
 class CSerialPort
 class CSerialPort
 {
 {
 public:
 public:
-	CSerialPort(
-		std::wstring portNum,		// 串口号
-		DWORD baudRate = 9600,			// 波特率
-		BYTE  byteSize = 8,				// 数据位
-		BYTE  parityBit = NOPARITY,		// 检验位
-		BYTE  stopBit = ONESTOPBIT		// 停止位
-	);
+    CSerialPort(
+        std::wstring portNum,		// 串口号
+        DWORD baudRate = 9600,			// 波特率
+        BYTE  byteSize = 8,				// 数据位
+        BYTE  parityBit = NOPARITY,		// 检验位
+        BYTE  stopBit = ONESTOPBIT		// 停止位
+    );
 
 
-	~CSerialPort();
+    ~CSerialPort();
 
 
 public:
 public:
 
 
-	bool openComm();										// 打开串口
-	void closeComm();										// 关闭串口
-	bool writeToComm(BYTE data[], DWORD dwLegnth);			// 发送数据
-	bool readFromComm(char buffer[], DWORD dwLength);		// 读取数据
+    bool openComm();										// 打开串口
+    void closeComm();										// 关闭串口
+    bool writeToComm(BYTE data[], DWORD dwLegnth);			// 发送数据
+    bool readFromComm(char buffer[], DWORD dwLength);		// 读取数据
 
 
 private:
 private:
 
 
-	HANDLE m_hComm;		// 通信设备
-	std::wstring m_portNum; // 串口号
-	DWORD m_dwBaudRate; // 波特率
-	BYTE  m_byteSize;	// 数据位
-	BYTE  m_parityBit;  // 校验位
-	BYTE  m_stopBit;	// 停止位
-	bool  m_bOpen;		// 串口开关标志
+    HANDLE m_hComm;		// 通信设备
+    std::wstring m_portNum; // 串口号
+    DWORD m_dwBaudRate; // 波特率
+    BYTE  m_byteSize;	// 数据位
+    BYTE  m_parityBit;  // 校验位
+    BYTE  m_stopBit;	// 停止位
+    bool  m_bOpen;		// 串口开关标志
 private:
 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
+    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;
+    }
+};

+ 2 - 2
zhipuzi_pay_plugin/wnd/CValueWnd.cpp

@@ -66,8 +66,8 @@ void CValueWnd::ReadValue()
 		while (true)
 		while (true)
 		{
 		{
 			//¿ªÊ¼¶ÁÈ¡´®¿ÚµÄÊý¾Ý			
 			//¿ªÊ¼¶ÁÈ¡´®¿ÚµÄÊý¾Ý			
-			char a[100] = { 0 };
-			serial.readFromComm(a, 100);
+			char a[1024] = { 0 };
+			serial.readFromComm(a, 1024);
 
 
 			LOG_INFO("a:" << a);
 			LOG_INFO("a:" << a);