张洋 hai 2 semanas
pai
achega
b97b1ef5ef

+ 36 - 31
zhipuzi_pos_windows/helper/CSerialPort.h

@@ -1,4 +1,4 @@
-#pragma once
+#pragma once
 
 #include <cassert>
 
@@ -10,38 +10,38 @@ class CSerialPort
 {
 public:
     CSerialPort(
-        std::wstring portNum,		// ´®¿ÚºÅ
-        DWORD baudRate = 9600,			// ²¨ÌØÂÊ
-        BYTE  byteSize = 8,				// Êý¾Ýλ
-        BYTE  parityBit = NOPARITY,		// ¼ìÑéλ
-        BYTE  stopBit = ONESTOPBIT		// ֹͣλ
+        std::wstring portNum,		// 串��
+        DWORD baudRate = 9600,			// 波特率
+        BYTE  byteSize = 8,				// 数��
+        BYTE  parityBit = NOPARITY,		// 检验�
+        BYTE  stopBit = ONESTOPBIT		// �止�
     );
 
 	CSerialPort(
-		DWORD baudRate = 9600,			// ²¨ÌØÂÊ
-		BYTE  byteSize = 8,				// Êý¾Ýλ
-		BYTE  parityBit = NOPARITY,		// ¼ìÑéλ
-		BYTE  stopBit = ONESTOPBIT		// ֹͣλ
+		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, DWORD* nReaded);		// ¶ÁÈ¡Êý¾Ý
+    bool openComm();										// 打开串�
+    void closeComm();										// 关闭串�
+    bool writeToComm(BYTE data[], DWORD dwLegnth);			// ��数�
+    bool readFromComm(char buffer[], DWORD dwLength, DWORD* nReaded);		// 读�数�
 
 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;		// 串�开关标志
 
 public:
 
@@ -52,55 +52,60 @@ public:
         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;
     }
+
+	bool getIsOpen()
+	{
+		return m_bOpen;
+	}
 };

+ 3 - 0
zhipuzi_pos_windows/page/CDiandanPageUI.cpp

@@ -82,6 +82,9 @@ void CDiandanPageUI::InitShow()
 		chegnzhongLayout->SetVisible(true);
 
 		m_is_dianzicheng_lianjie = true;
+
+		//刷新一下重量显示
+		this->HandleChengzhongValueChange();
 	}
 }
 

+ 4 - 0
zhipuzi_pos_windows/page/CSettingPageUI.cpp

@@ -1783,6 +1783,8 @@ void CSettingPageUI::HandleSelectChangeMsg(TNotifyUI& msg)
 
 		CVerticalLayoutUI* dianzicheng_layout = static_cast<CVerticalLayoutUI*>(this->FindSubControl(_T("setting_dianzicheng_layout")));
 		dianzicheng_layout->SetVisible(true);
+
+		CChengzhongWorker::GetInstance()->RestartWork();
 	}
 	else if (name == _T("setting_dianzicheng_weilianjie"))
 	{
@@ -1790,6 +1792,8 @@ void CSettingPageUI::HandleSelectChangeMsg(TNotifyUI& msg)
 
 		CVerticalLayoutUI* dianzicheng_layout = static_cast<CVerticalLayoutUI*>(this->FindSubControl(_T("setting_dianzicheng_layout")));
 		dianzicheng_layout->SetVisible(false);
+
+		CChengzhongWorker::GetInstance()->RestartWork();
 	}
 }
 

+ 0 - 3
zhipuzi_pos_windows/tool/CAppEnv.cpp

@@ -44,12 +44,9 @@ void CAppEnv::Start()
 
 	//启动摄像头任务
 	CVideoCaptureWorker::GetInstance()->Start();
-	AddWorkerNum();
 
 	//启动AI识别任务(有2个工作线程)
 	CDiandanAIShibieWorker::GetInstance()->StartWork();
-	AddWorkerNum();
-	AddWorkerNum();
 
 	//启动通用任务
 	CCommonWorker::GetInstance()->Start();

+ 108 - 79
zhipuzi_pos_windows/worker/CChengzhongWorker.cpp

@@ -3,6 +3,8 @@
 
 #include "CDiandanAIShibieWorker.h"
 
+#include "../tool/CAppEnv.h"
+
 CChengzhongWorker::CChengzhongWorker()
 {
 }
@@ -10,9 +12,14 @@ CChengzhongWorker::CChengzhongWorker()
 //启动工作线程
 void CChengzhongWorker::StartWork()
 {
+	m_worker_mutex.lock();
+
 	m_is_work = true;
 
+	m_worker_mutex.unlock();
+
 	std::thread(&CChengzhongWorker::HandleWork, this).detach();
+	CAppEnv::GetInstance()->AddWorkerNum();
 }
 
 //结束工作线程
@@ -32,88 +39,82 @@ void CChengzhongWorker::StopWork()
 
 void CChengzhongWorker::RestartWork()
 {
-	StopWork();
+	//重新获取型号,重新连接串口
+	m_dianzicheng_lianjie = CSetting::GetInstance()->GetParam("setting_dianzicheng_is_lianjie");
+	m_dianzicheng_xinghao = CSetting::GetInstance()->GetParam("setting_dianzicheng_xinghao");
 
-	StartWork();
+	if (m_dianzicheng_lianjie == "1")
+	{
+		this->RestartSerial();
+	}
+
+	this->UpdateShow("0.000");
 }
 
 void CChengzhongWorker::HandleWork()
 {
-	std::string setting_dianzicheng_xinghao = CSetting::GetInstance()->GetParam("setting_dianzicheng_xinghao");
-	std::string dianzicheng_com = CSetting::GetInstance()->GetParam("setting_dianzicheng_com");
-	std::string setting_dianzicheng_botelv = CSetting::GetInstance()->GetParam("setting_dianzicheng_botelv");
+	m_dianzicheng_lianjie = CSetting::GetInstance()->GetParam("setting_dianzicheng_is_lianjie");
+	m_dianzicheng_xinghao = CSetting::GetInstance()->GetParam("setting_dianzicheng_xinghao");
 
-	m_serial.setPortNum(CLewaimaiString::UTF8ToUnicode(dianzicheng_com));
-	m_serial.setBaudRate(atoi(setting_dianzicheng_botelv.c_str()));
-
-	bool ret = m_serial.openComm();
-	if (!ret)
+	if (m_dianzicheng_lianjie == "1")
 	{
-		//连接电子秤失败了
-		m_is_work = false;
-
-		return;
+		this->RestartSerial();
 	}
 
-	std::string m_chuankou_string = "";
-
 	while (m_is_work)
 	{
+		if (m_dianzicheng_lianjie == "0")
+		{
+			Sleep(200);
+			continue;
+		}
+
 		std::string new_weight;
 
+		std::string chuankou_string = "";
+
 		//不同型号的电子秤,数据格式可能不一样,处理方式不一样
-		if (setting_dianzicheng_xinghao == "dahua_acs")
+		if (m_dianzicheng_xinghao == "dahua_acs")
 		{
-			char a[100] = { 0 };
+			char a[22] = { 0 };
 
-			memset(a, 0, 100);
+			memset(a, 0, 22);
 
 			//开始读取串口的数据
 			DWORD nReaded = 0;
 
-			m_serial.readFromComm(a, 100, &nReaded);
+			m_serial.readFromComm(a, 22, &nReaded);
 			if (nReaded <= 0)
 			{
 				Sleep(200);
 				continue;
 			}
 
-			//把所有读到的内容,拼接到m_chuankou_string后面,避免有的数据中间截断导致格式混乱
-			m_chuankou_string += a;
+			//把所有读到的内容,拼接到chuankou_string后面,避免有的数据中间截断导致格式混乱
+			chuankou_string = a;
 
-			std::string show_command = "\n\r";
-
-			size_t nPos = m_chuankou_string.find(show_command);
-			if (nPos == m_chuankou_string.npos)
+			if (a[20] != 0x0A || a[21] != 0x0D)
 			{
-				//没有读到足够的长度,继续读
-				continue;
-			}
+				//说明这22个字节的数据不是完整的,这个时候处理方式是把错误数据全部读完,但是不处理,然后下次接着处理
+				std::string show_command = "\n\r";
 
-			std::string weight;
-
-			//如果前2个不是标志符,那么就判断标志符后面的数字长度够不够5个
-			if (m_chuankou_string.length() >= nPos + 7)
-			{
-				//这个情况是,标志符后面有5个数字
-				weight = m_chuankou_string.substr(nPos + 2, 5);
+				size_t nPos = chuankou_string.find(show_command);
+				if (nPos == chuankou_string.npos)
+				{
+					//不存在标志符,数据错误
+					this->UpdateShow("0.000");
+					continue;
+				}
 
-				//然后把前面的字符都删掉
-				m_chuankou_string = m_chuankou_string.substr(nPos + 7);
-			}
-			else if (nPos >= 20)
-			{
-				//说明前面有20个字符,首先肯定不是稳定模式而是极速模式,另外在极速模式下可以直接得到重量了
-				weight = m_chuankou_string.substr(nPos - 20, 5);
+				//把废数据读出来,不处理,下次重新读16个完整的
+				m_serial.readFromComm(a, nPos + 2, &nReaded);
 
-				m_chuankou_string = m_chuankou_string.substr(nPos + 2);
-			}
-			else
-			{
-				//标志符后面前面都不够读取重量,继续读取串口
+				this->UpdateShow("0.000");
 				continue;
 			}
 
+			std::string weight = chuankou_string.substr(0, 5);
+
 			if (weight.at(0) == ' ')
 			{
 				weight = weight.substr(1);
@@ -135,12 +136,13 @@ void CChengzhongWorker::HandleWork()
 			}
 			else
 			{
+				this->UpdateShow("0.000");
 				continue;
 			}
 
 			new_weight = zhengshu + "." + xiaoshu;
 		}
-		else if (setting_dianzicheng_xinghao == "dingjian_os2x")
+		else if (m_dianzicheng_xinghao == "dingjian_os2x")
 		{
 			char a[16] = { 0 };
 
@@ -156,30 +158,30 @@ void CChengzhongWorker::HandleWork()
 				continue;
 			}
 
-			//把所有读到的内容,拼接到m_chuankou_string后面,避免有的数据中间截断导致格式混乱
-			m_chuankou_string = a;
+			//把所有读到的内容,拼接到chuankou_string后面,避免有的数据中间截断导致格式混乱
+			chuankou_string = a;
 
 			if (a[0] != 0x01 || a[1] != 0x02 || a[13] != 0x03 || a[14] != 0x04)
 			{
 				//说明这16个字节的数据不是完整的,这个时候处理方式是把错误数据全部读完,但是不处理,然后下次接着处理
 				std::string show_command = "\x01\x02";
 
-				size_t nPos = m_chuankou_string.find(show_command);
-				if (nPos == m_chuankou_string.npos)
+				size_t nPos = chuankou_string.find(show_command);
+				if (nPos == chuankou_string.npos)
 				{
 					//不存在标志符,数据错误
-					m_chuankou_string = "";
+					this->UpdateShow("0.000");
 					continue;
 				}
 
 				//把废数据读出来,不处理,下次重新读16个完整的
 				m_serial.readFromComm(a, nPos, &nReaded);
 
-				m_chuankou_string = "";
+				this->UpdateShow("0.000");
 				continue;
 			}
 
-			std::string new_weight = m_chuankou_string.substr(4, 6);
+			std::string new_weight = chuankou_string.substr(4, 6);
 
 			if (new_weight.at(0) == '0')
 			{
@@ -189,40 +191,67 @@ void CChengzhongWorker::HandleWork()
 		else
 		{
 			//暂时不支持的其他型号
+			this->UpdateShow("0.000");
+
 			Sleep(200);
 
 			continue;
 		}
 
-		//开始更新重量显示
-		if (m_weight != new_weight)
-		{
-			//说明重量有变化了
-			m_weight = new_weight;
+		this->UpdateShow(new_weight);
+	}
 
-			m_is_wending = false;
-		}
-		else
-		{
-			//重量没有变化了,说明稳定了
-			if (m_is_wending == false)
-			{
-				//说明目前是从不稳定变为稳定,那么这里可以发起AI识别
-				CDiandanAIShibieWorker::GetInstance()->AddAIShibieTask();
-			}
+	CAppEnv::GetInstance()->DelWorkerNum();
+}
 
-			m_is_wending = true;
-		}
+void CChengzhongWorker::RestartSerial()
+{
+	//重新连接串口
+	m_serial.closeComm();
 
-		if (m_hwnd != NULL)
-		{
-			PostMessage(m_hwnd, WM_CHENGZHONG_SUCCESS, 0, 0);
-		}
+	std::string dianzicheng_com = CSetting::GetInstance()->GetParam("setting_dianzicheng_com");
+	std::string setting_dianzicheng_botelv = CSetting::GetInstance()->GetParam("setting_dianzicheng_botelv");
+
+	m_serial.setPortNum(CLewaimaiString::UTF8ToUnicode(dianzicheng_com));
+	m_serial.setBaudRate(atoi(setting_dianzicheng_botelv.c_str()));
+
+	bool ret = m_serial.openComm();
+	if (ret == false)
+	{
+		this->UpdateShow("0.000");
+	}
+}
+
+void CChengzhongWorker::UpdateShow(std::string new_weight)
+{
+	//开始更新重量显示
+	if (m_weight != new_weight)
+	{
+		//说明重量有变化了
+		m_weight = new_weight;
 
-		if (m_MainHWND != NULL)
+		m_is_wending = false;
+	}
+	else
+	{
+		//重量没有变化了,说明稳定了
+		if (m_is_wending == false)
 		{
-			PostMessage(m_MainHWND, WM_CHENGZHONG_SUCCESS, 0, 0);
+			//说明目前是从不稳定变为稳定,那么这里可以发起AI识别
+			CDiandanAIShibieWorker::GetInstance()->AddAIShibieTask();
 		}
+
+		m_is_wending = true;
+	}
+
+	if (m_hwnd != NULL)
+	{
+		PostMessage(m_hwnd, WM_CHENGZHONG_SUCCESS, 0, 0);
+	}
+
+	if (m_MainHWND != NULL)
+	{
+		PostMessage(m_MainHWND, WM_CHENGZHONG_SUCCESS, 0, 0);
 	}
 }
 

+ 12 - 6
zhipuzi_pos_windows/worker/CChengzhongWorker.h

@@ -7,6 +7,8 @@
 class CChengzhongWorker
 {
 public:
+	CChengzhongWorker();
+
 	static CChengzhongWorker * GetInstance()
 	{
 		static CChengzhongWorker instance;
@@ -30,11 +32,15 @@ public:
 	//结束工作线程
 	void StopWork();
 
-	//重启工作
+	//修改了设置,工作线程,重新连接串口
 	void RestartWork();
 
 	void HandleWork();
 
+	void RestartSerial();
+
+	void UpdateShow(std::string new_weight);
+
 	std::string GetWeight()
 	{
 		return m_weight;
@@ -42,7 +48,7 @@ public:
 
 	bool GetIsWork()
 	{
-		return m_is_work;
+		return m_serial.getIsOpen();
 	}
 
 	bool GetIsWending()
@@ -55,10 +61,7 @@ public:
 	void Qupi();
 
 private:
-	CChengzhongWorker();
-
-private:
-	bool m_is_work;
+	bool m_is_work = false;
 
 	std::mutex m_worker_mutex;
 
@@ -75,4 +78,7 @@ private:
 
 	//称重金额是否稳定
 	bool m_is_wending = false;
+
+	std::string m_dianzicheng_lianjie;
+	std::string m_dianzicheng_xinghao;
 };

+ 3 - 1
zhipuzi_pos_windows/worker/CDiandanAIShibieWorker.cpp

@@ -23,8 +23,10 @@ void CDiandanAIShibieWorker::StartWork()
 
 	//创建一个新线程,专门处理AI识别的结果,避免因为AI识别的结果处理比较慢,导致界面卡顿
 	std::thread(&CDiandanAIShibieWorker::HandleDiandanAIShibie, this).detach();
+	CAppEnv::GetInstance()->AddWorkerNum();
 
 	std::thread(&CDiandanAIShibieWorker::HandleDiandanAIShibieQueue, this).detach();
+	CAppEnv::GetInstance()->AddWorkerNum();
 }
 
 //结束工作线程
@@ -198,7 +200,7 @@ void CDiandanAIShibieWorker::HandleDiandanAIShibie()
 		//暂时不做循环识别模式
 		//this->DoAIShibie();
 
-		Sleep(100);
+		Sleep(200);
 	}
 
 	//走到这里说明线程要退出了,做一些清理工作

+ 1 - 0
zhipuzi_pos_windows/worker/CVideoCaptureWorker.cpp

@@ -20,6 +20,7 @@ void CVideoCaptureWorker::Start()
 
 	//持续获取摄像头数据,直到程序退出,这里开启一个线程来处理摄像头数据的获取,避免阻塞主线程
 	std::thread(&CVideoCaptureWorker::HandleVideoCapture, this).detach();
+	CAppEnv::GetInstance()->AddWorkerNum();
 }
 
 //停止工作