|
|
@@ -1,6 +1,8 @@
|
|
|
#include "../pch/pch.h"
|
|
|
#include "CChengzhongWnd.h"
|
|
|
|
|
|
+#include "../tool/CChengzhongWorker.h"
|
|
|
+
|
|
|
LRESULT CChengzhongWnd::OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
|
|
|
{
|
|
|
POINT pt;
|
|
|
@@ -310,14 +312,7 @@ void CChengzhongWnd::Notify(TNotifyUI& msg)
|
|
|
|
|
|
LRESULT CChengzhongWnd::OnChengzhongSuccess(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
|
|
|
{
|
|
|
- CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
|
|
|
- pContent->SetText(CLewaimaiString::UTF8ToUnicode(m_weight).c_str());
|
|
|
-
|
|
|
- wstring ws_Value = pContent->GetText();
|
|
|
- string strValue = CLewaimaiString::UnicodeToUTF8(ws_Value);
|
|
|
-
|
|
|
- pContent->SetSel(strValue.length(), strValue.length());//重设给光标设置位置
|
|
|
- pContent->SetFocus();
|
|
|
+ UpdateWeightFromWorker();
|
|
|
|
|
|
bHandled = TRUE;
|
|
|
return 0;
|
|
|
@@ -326,7 +321,18 @@ LRESULT CChengzhongWnd::OnChengzhongSuccess(UINT uMsg, WPARAM wParam, LPARAM lPa
|
|
|
void CChengzhongWnd::Init()
|
|
|
{
|
|
|
//启动一个线程,开始串口监听
|
|
|
- std::thread(&CChengzhongWnd::ReadChuankouValue, this).detach();
|
|
|
+ CChengzhongWorker::GetInstance()->SetHWND(this->m_hWnd);
|
|
|
+
|
|
|
+ if (CChengzhongWorker::GetInstance()->GetIsWork() == false)
|
|
|
+ {
|
|
|
+ //电子秤连接串口失败了
|
|
|
+ CLabelUI* pErrorInfo = static_cast<CLabelUI*>(m_pm.FindControl(_T("errinfo")));
|
|
|
+ pErrorInfo->SetText(L"连接电子秤失败,请检查电子秤设置");
|
|
|
+ pErrorInfo->SetVisible(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ //先设置一次已经存取的最新weight
|
|
|
+ UpdateWeightFromWorker();
|
|
|
}
|
|
|
|
|
|
std::string CChengzhongWnd::getContent()
|
|
|
@@ -350,138 +356,27 @@ void CChengzhongWnd::SetPrice(std::string price)
|
|
|
pLabel->SetText((L"单价:" + ws_price + L"元/公斤").c_str());
|
|
|
}
|
|
|
|
|
|
-void CChengzhongWnd::ReadChuankouValue()
|
|
|
+void CChengzhongWnd::UpdateWeightFromWorker()
|
|
|
{
|
|
|
- std::string setting_dianzicheng_xinghao = CSetting::GetParam("setting_dianzicheng_xinghao");
|
|
|
- std::string dianzicheng_com = CSetting::GetParam("setting_dianzicheng_com");
|
|
|
- std::string setting_dianzicheng_botelv = CSetting::GetParam("setting_dianzicheng_botelv");
|
|
|
-
|
|
|
- m_serial.setPortNum(CLewaimaiString::UTF8ToUnicode(dianzicheng_com));
|
|
|
- m_serial.setBaudRate(atoi(setting_dianzicheng_botelv.c_str()));
|
|
|
+ m_weight = CChengzhongWorker::GetInstance()->GetWeight();
|
|
|
|
|
|
- bool ret = m_serial.openComm();
|
|
|
- if (!ret)
|
|
|
+ if (m_weight.length() == 0)
|
|
|
{
|
|
|
- //这里不设置后面没法退出了
|
|
|
- m_is_watching = true;
|
|
|
-
|
|
|
- CLabelUI* pErrorInfo = static_cast<CLabelUI*>(m_pm.FindControl(_T("errinfo")));
|
|
|
- pErrorInfo->SetText(L"电子秤连接失败,请检查电子秤连接设置");
|
|
|
- pErrorInfo->SetVisible(true);
|
|
|
-
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- m_is_chuangkou_working = true;
|
|
|
- m_is_watching = true;
|
|
|
-
|
|
|
- m_chuankou_string = "";
|
|
|
-
|
|
|
- char a[100] = { 0 };
|
|
|
-
|
|
|
- //检查监控模式和波特率,如果变了就关闭掉
|
|
|
- while (m_is_watching)
|
|
|
- {
|
|
|
- memset(a, 0, 100);
|
|
|
-
|
|
|
- //开始读取串口的数据
|
|
|
- DWORD nReaded = 0;
|
|
|
-
|
|
|
- m_serial.readFromComm(a, 100, &nReaded);
|
|
|
-
|
|
|
- if (nReaded > 0)
|
|
|
- {
|
|
|
- //不同型号的电子秤,数据格式可能不一样,处理方式不一样
|
|
|
-
|
|
|
- if (setting_dianzicheng_xinghao == "dahua_acs")
|
|
|
- {
|
|
|
- //把所有读到的内容,拼接到m_chuankou_string后面,避免有的数据中间截断导致格式混乱
|
|
|
- m_chuankou_string += a;
|
|
|
-
|
|
|
- std::string show_command = "\n\r";
|
|
|
-
|
|
|
- size_t nPos = m_chuankou_string.find(show_command);
|
|
|
- if (nPos == m_chuankou_string.npos)
|
|
|
- {
|
|
|
- //没有读到足够的长度,继续读
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- std::string weight;
|
|
|
-
|
|
|
- //如果前2个不是标志符,那么就判断标志符后面的数字长度够不够5个
|
|
|
- if (m_chuankou_string.length() >= nPos + 7)
|
|
|
- {
|
|
|
- //这个情况是,标志符后面有5个数字
|
|
|
- weight = m_chuankou_string.substr(nPos + 2, 5);
|
|
|
-
|
|
|
- //然后把前面的字符都删掉
|
|
|
- m_chuankou_string = m_chuankou_string.substr(nPos + 7);
|
|
|
- }
|
|
|
- else if (nPos >= 20)
|
|
|
- {
|
|
|
- //说明前面有20个字符,首先肯定不是稳定模式而是极速模式,另外在极速模式下可以直接得到重量了
|
|
|
- weight = m_chuankou_string.substr(nPos - 20, 5);
|
|
|
-
|
|
|
- m_chuankou_string = m_chuankou_string.substr(nPos + 2);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- //标志符后面前面都不够读取重量,继续读取串口
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- if (weight.at(0) == ' ')
|
|
|
- {
|
|
|
- weight = weight.substr(1);
|
|
|
- }
|
|
|
-
|
|
|
- std::string zhengshu;
|
|
|
- std::string xiaoshu;
|
|
|
- if (weight.length() == 4)
|
|
|
- {
|
|
|
- //重量小于10公斤,第一个数字为空
|
|
|
- zhengshu = weight.substr(0, 1);
|
|
|
- xiaoshu = weight.substr(1, 3);
|
|
|
- }
|
|
|
- else if (weight.length() == 5)
|
|
|
- {
|
|
|
- //重量大于10公斤,第一个数字为空
|
|
|
- zhengshu = weight.substr(0, 2);
|
|
|
- xiaoshu = weight.substr(2, 3);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- continue;
|
|
|
- }
|
|
|
-
|
|
|
- m_weight = zhengshu + "." + xiaoshu;
|
|
|
+ CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
|
|
|
+ pContent->SetText(CLewaimaiString::UTF8ToUnicode(m_weight).c_str());
|
|
|
|
|
|
- SendMessage(WM_CHENGZHONG_SUCCESS, 0, 0);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- //暂时不支持的其他型号
|
|
|
- Sleep(200);
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- Sleep(200);
|
|
|
- }
|
|
|
- }
|
|
|
+ wstring ws_Value = pContent->GetText();
|
|
|
+ string strValue = CLewaimaiString::UnicodeToUTF8(ws_Value);
|
|
|
|
|
|
- m_is_chuangkou_working = false;
|
|
|
+ pContent->SetFocus();
|
|
|
+ pContent->SetSel(strValue.length(), strValue.length());//重设给光标设置位置
|
|
|
}
|
|
|
|
|
|
void CChengzhongWnd::SaveWeight()
|
|
|
{
|
|
|
- if (m_is_watching == false)
|
|
|
- {
|
|
|
- //串口都还没打开,禁止退出
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
CEditUI* pContent = static_cast<CEditUI*>(m_pm.FindControl(_T("content")));
|
|
|
wstring wsReason = pContent->GetText();
|
|
|
|
|
|
@@ -496,38 +391,10 @@ void CChengzhongWnd::SaveWeight()
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
- m_is_watching = false;
|
|
|
-
|
|
|
- //关闭串口,关闭之后读取串口的工作线程会退出
|
|
|
- m_serial.closeComm();
|
|
|
-
|
|
|
- //等待工作线程退出
|
|
|
- while (m_is_chuangkou_working)
|
|
|
- {
|
|
|
- Sleep(200);
|
|
|
- }
|
|
|
-
|
|
|
Close(IDOK);
|
|
|
}
|
|
|
|
|
|
void CChengzhongWnd::Quit()
|
|
|
{
|
|
|
- if (m_is_watching == false)
|
|
|
- {
|
|
|
- //串口都还没打开,禁止退出
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- m_is_watching = false;
|
|
|
-
|
|
|
- //关闭串口,关闭之后读取串口的工作线程会退出
|
|
|
- m_serial.closeComm();
|
|
|
-
|
|
|
- //等待工作线程退出
|
|
|
- while (m_is_chuangkou_working)
|
|
|
- {
|
|
|
- Sleep(200);
|
|
|
- }
|
|
|
-
|
|
|
Close(IDCANCEL);
|
|
|
}
|