// DialogFactCfg.cpp: 实现文件 // #include "pch.h" #include "MTerm2.h" #include "DialogFactCfg.h" #include "afxdialogex.h" #include "MTerm2Doc.h" // CDialogFactCfg 对话框 IMPLEMENT_DYNAMIC(CDialogFactCfg, CDialogEx) CDialogFactCfg::CDialogFactCfg(CWnd* pParent /*=nullptr*/) : CDialogEx(IDD_DIALOG_FACT_CFG, pParent) { InitDataItems(); } CDialogFactCfg::~CDialogFactCfg() { } void CDialogFactCfg::DoDataExchange(CDataExchange* pDX) { CDialogEx::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CDialogFactCfg, CDialogEx) ON_BN_CLICKED(IDC_BUTTON_CONN, &CDialogFactCfg::OnBnClickedButtonConn) ON_BN_CLICKED(IDC_BUTTON_DISCONN, &CDialogFactCfg::OnBnClickedButtonDisconn) ON_BN_CLICKED(IDC_BUTTON_READDATA, &CDialogFactCfg::OnBnClickedButtonReaddata) ON_BN_CLICKED(IDC_BUTTON_WRITEDATA, &CDialogFactCfg::OnBnClickedButtonWritedata) ON_BN_CLICKED(IDC_BUTTON1, &CDialogFactCfg::OnBnClickedBtn1) ON_BN_CLICKED(IDC_BUTTON3, &CDialogFactCfg::OnBnClickedBtn3) ON_BN_CLICKED(IDC_BUTTON4, &CDialogFactCfg::OnBnClickedBtn4) ON_BN_CLICKED(IDC_BUTTON5, &CDialogFactCfg::OnBnClickedBtn5) ON_BN_CLICKED(IDC_BUTTON_CLEAR, &CDialogFactCfg::OnBnClickedBtnClear) ON_BN_CLICKED(IDC_BUTTON_ADD, &CDialogFactCfg::OnBnClickedBtnAdd) ON_BN_CLICKED(IDC_BUTTON_BACKSPACE, &CDialogFactCfg::OnBnClickedBtnBackSpace) ON_BN_CLICKED(IDC_BUTTON_0, &CDialogFactCfg::OnBnClickedButton0) ON_BN_CLICKED(IDC_BUTTON1_DOT, &CDialogFactCfg::OnBnClickedButton1Dot) ON_BN_CLICKED(IDC_BUTTON_EQ, &CDialogFactCfg::OnBnClickedButtonEq) END_MESSAGE_MAP() // CDialogFactCfg 消息处理程序 int CDialogFactCfg::AddDataItem(int nOffset, int nSize, int nType, CString sName) { FactoryDataItems[nFactoryDataItemsCount] = { nOffset,nSize,nType,sName }; nFactoryDataItemsCount++; return 0; } int CDialogFactCfg::InitDataItems() { AddDataItem(0, 2, typeBIN, _T("开始标志")); AddDataItem(2, 2, typeINT, _T("序号")); AddDataItem(4, 2, typeHEX, _T("ModelNo")); AddDataItem(6, 2, typeHEX, _T("ModelVer")); AddDataItem(8, 4, typeUINT, _T("nLotNo")); AddDataItem(12, 4, typeDATETIME, _T("nProductDate")); AddDataItem(16, 4, typeHEX, _T("nSN1")); AddDataItem(20, 4, typeHEX, _T("nProtocalVer")); AddDataItem(24, 4, typeHEX, _T("nDefaultFuncr")); AddDataItem(28, 16, typeString, _T("sModelStr")); AddDataItem(44, 16, typeString, _T("sLot_No")); AddDataItem(60, 16, typeString, _T("sSNStr")); AddDataItem(76, 2, typeHEX, _T("CRC1")); AddDataItem(78, 2, typeHEX, _T("EndSign")); return 0; } int CDialogFactCfg::FindIdByName(CString sName) { for (int i = 0; i < nFactoryDataItemsCount; i++) { if (FactoryDataItems[i].sName == sName) { return i; } } return -1; } int CDialogFactCfg::GetDataByOffset(void * pDataBlock, int nOffset) { return 0; } int CDialogFactCfg::GetDataByName(void * pDataBlock, CString sName) { return 0; } CString CDialogFactCfg::GetDataStrByName(void * pDataBlock, CString sName) { UCHAR * pBuf2 = (UCHAR *)pDataBlock; CString s1; UCHAR buf3[256]; CString s3; int nIdx = FindIdByName(sName); if (nIdx >= 0) { CTime ctime1; int nType = FactoryDataItems[nIdx].nType; int nSize = FactoryDataItems[nIdx].nSize; int nOffset = FactoryDataItems[nIdx].nOffset; int nValue; UINT uValue; switch (nType) { case typeINT: if (nSize == 1) { nValue = *((char *)(pBuf2 + nOffset)); } else if (nSize == 2) { nValue = *(short *)(pBuf2 + nOffset); } else if (nSize == 3) { } else if (nSize == 4) { nValue = *(int *)(pBuf2 + nOffset); } s1.Format(_T("%d"), nValue); break; case typeUINT: if (nSize == 1) { uValue = *((UCHAR *)(pBuf2 + nOffset)); } else if (nSize == 2) { uValue = *(USHORT *)(pBuf2 + nOffset); } else if (nSize == 3) { } else if (nSize == 4) { uValue = *(UINT *)(pBuf2 + nOffset); } s1.Format(_T("%u"), uValue); break; case typeHEX: if (nSize == 1) { uValue = *((UCHAR *)(pBuf2 + nOffset)); s1.Format(_T("%02X"), uValue); } else if (nSize == 2) { uValue = *(USHORT *)(pBuf2 + nOffset); s1.Format(_T("%04X"), uValue); } else if (nSize == 3) { } else if (nSize == 4) { uValue = *(UINT *)(pBuf2 + nOffset); s1.Format(_T("%08X"), uValue); } break; case typeBIN: if (nSize == 1) { uValue = *((UCHAR *)(pBuf2 + nOffset)); s1.Format(_T("%02X"), uValue); } else if (nSize == 2) { uValue = *(USHORT *)(pBuf2 + nOffset); s1.Format(_T("%04X"), uValue); } else if (nSize == 3) { } else if (nSize == 4) { uValue = *(UINT *)(pBuf2 + nOffset); s1.Format(_T("%08X"), uValue); } break; case typeFLOAT: break; case typeFIX: break; case typeBIT: break; case typeString: for (int j = 0; j < nSize; j++) { buf3[j] = pBuf2[nOffset + j]; } buf3[nSize] = 0; s3 = buf3; s1.Format(_T("%s"), s3); break; case typeDATETIME: nValue = *(UINT *)(pBuf2 + nOffset); ctime1 = nValue; ctime1 = CStringToCTime(_T("2000-01-01 08:00:00")); ctime1 += nValue; s3 = ctime1.Format(_T("%Y-%m-%d %H:%M:%S")); s1.Format(_T("%s"), s3); break; default: break; } } return s1; } int CDialogFactCfg::SetDataByName(void * pDataBlock, CString sName, UINT uValue) { UCHAR * pBuf2 = (UCHAR *)pDataBlock; // UCHAR buf3[256]; int nIdx = FindIdByName(sName); if (nIdx >= 0) { CTime ctime1; CTime ctime2; CTimeSpan ctime3; int nType = FactoryDataItems[nIdx].nType; int nSize = FactoryDataItems[nIdx].nSize; int nOffset = FactoryDataItems[nIdx].nOffset; int nValue; switch (nType) { case typeINT: if (nSize == 1) { *((char *)(pBuf2 + nOffset)) = uValue; } else if (nSize == 2) { *(short *)(pBuf2 + nOffset) = uValue; } else if (nSize == 3) { } else if (nSize == 4) { *(int *)(pBuf2 + nOffset) = uValue; } break; case typeUINT: if (nSize == 1) { *((UCHAR *)(pBuf2 + nOffset)) = uValue; } else if (nSize == 2) { *(USHORT *)(pBuf2 + nOffset) = uValue; } else if (nSize == 3) { } else if (nSize == 4) { *(UINT *)(pBuf2 + nOffset) = uValue; } break; case typeBIN: if (nSize == 1) { *((UCHAR *)(pBuf2 + nOffset)) = uValue; } else if (nSize == 2) { *(USHORT *)(pBuf2 + nOffset) = uValue; } else if (nSize == 3) { } else if (nSize == 4) { *(UINT *)(pBuf2 + nOffset) = uValue; } break; case typeFLOAT: break; case typeFIX: break; case typeBIT: break; case typeString: break; case typeDATETIME: ctime1 = CStringToCTime(_T("2000-01-01 08:00:00")); ctime2 = uValue; ctime3 = ctime2 - ctime1; nValue = ctime3.GetTotalSeconds(); *(UINT *)(pBuf2 + nOffset) = uValue; break; default: break; } } return 0; } int CDialogFactCfg::SetDataByName(void * pDataBlock, CString sName, CString sValue) { UCHAR * pBuf2 = (UCHAR *)pDataBlock; // UCHAR buf3[256]; CStringA s3; int nIdx = FindIdByName(sName); if (nIdx >= 0) { CTime ctime1; CTime ctime2; CTimeSpan ctime3; int nType = FactoryDataItems[nIdx].nType; int nSize = FactoryDataItems[nIdx].nSize; int nOffset = FactoryDataItems[nIdx].nOffset; int nValue; int len; switch (nType) { case typeINT: if (nSize == 1) { *((char *)(pBuf2 + nOffset)) = _tstoi(sValue); } else if (nSize == 2) { *(short *)(pBuf2 + nOffset) = _tstoi(sValue); } else if (nSize == 3) { } else if (nSize == 4) { *(int *)(pBuf2 + nOffset) = _tstoi(sValue); } break; case typeUINT: if (nSize == 1) { *((UCHAR *)(pBuf2 + nOffset)) = _tstoi(sValue); } else if (nSize == 2) { *(USHORT *)(pBuf2 + nOffset) = _tstoi(sValue); } else if (nSize == 3) { } else if (nSize == 4) { *(UINT *)(pBuf2 + nOffset) = _tstoi(sValue); } break; case typeBIN: if (nSize == 1) { *((UCHAR *)(pBuf2 + nOffset)) = _tstoi(sValue); } else if (nSize == 2) { *(USHORT *)(pBuf2 + nOffset) = _tstoi(sValue); } else if (nSize == 3) { } else if (nSize == 4) { *(UINT *)(pBuf2 + nOffset) = _tstoi(sValue); } break; case typeFLOAT: break; case typeFIX: break; case typeBIT: break; case typeString: s3 = sValue; len = s3.GetLength(); for (int j = 0; j < nSize; j++) { if (j < len) { pBuf2[nOffset + j] = s3[j]; }else { pBuf2[nOffset + j] = ' '; } } break; case typeDATETIME: ctime1 = CStringToCTime(_T("2000-01-01 08:00:00")); ctime2 = CStringToCTime(sValue);; ctime3 = ctime2 - ctime1; nValue = ctime3.GetTotalSeconds(); *(UINT *)(pBuf2 + nOffset) = nValue; break; default: break; } } return 0; } CMTerm2Doc * CDialogFactCfg::GetDocument() { // TODO: 在此处添加实现代码. CMDIFrameWnd *pFrame = (CMDIFrameWnd*)AfxGetApp()->m_pMainWnd; CMDIChildWnd *pChild = (CMDIChildWnd *)pFrame->GetActiveFrame(); CView *pV = (CView*)pChild->GetActiveView(); CMTerm2Doc* pDoc = (CMTerm2Doc *)(pV->GetDocument()); return pDoc; } int CDialogFactCfg::Connect() { // TODO: 在此处添加实现代码. CString s1; CMTerm2Doc* pDoc = (CMTerm2Doc *)GetDocument(); int r = pDoc->Connect(); s1.Format(_T("Open %s = %d"), pDoc->myHvSerialPort1.m_strResult, r); SysLog(s1); if (r == pDoc->myHvSerialPort1.R_OK) { pDoc->MyKLink1.Open(); return false; } return true; return 0; } int CDialogFactCfg::Disconnect() { // TODO: 在此处添加实现代码. CMTerm2Doc* pDoc = (CMTerm2Doc *)GetDocument(); pDoc->DisConnect(); pDoc->MyKLink1.Close(); return 0; } int CDialogFactCfg::ReadData() { CMTerm2Doc* pDoc = (CMTerm2Doc *)GetDocument(); unsigned short buf1[1024]; int res = pDoc->MyKLink1.GetInfo(1, 0, 0, 16, buf1); SysLog(pDoc->MyKLink1.m_resultStr); CString s1; s1.Format(_T("GetInfo = %d "), res); if (res == KLink::KL_OK) { s1.Append(_T("\r\n")); for (int i = 0; i < 16 / 2; i++) { s1.AppendFormat(_T("%04X "), buf1[i]); } } SysLog(s1); s1.Format(_T("%04X"), buf1[0]); SetDlgItemText(IDC_EDIT_DEVICE_TYPE, s1); s1.Format(_T("%04X"), buf1[1]); SetDlgItemText(IDC_EDIT_FIRM_VER, s1); s1.Format(_T("%04X"), buf1[2]); SetDlgItemText(IDC_EDIT_KLINK_VER, s1); s1.Format(_T("%04X"), buf1[3]); SetDlgItemText(IDC_EDIT_CAP, s1); CString s2; CString s3; unsigned char buf2[1024]; res = pDoc->MyKLink1.GetUID(1, 0, 0, 12, (unsigned short *)buf2); SysLog(pDoc->MyKLink1.m_resultStr); s1.Format(_T("GetUID = %d "), res); if (res == KLink::KL_OK) { s1.Append(_T("\r\n")); for (int i = 0; i < 12; i++) { s1.AppendFormat(_T("%02X "), buf2[i]); s2.AppendFormat(_T("%02X"), buf2[12-i-1]); // s3.AppendFormat(_T("%c"), buf2[12 - i - 1]); } } SysLog(s1); SetDlgItemText(IDC_EDIT_DEVICE_UID, s2); res = pDoc->MyKLink1.GetSN(1, 0, 0, 4, (unsigned short *)buf2); SysLog(pDoc->MyKLink1.m_resultStr); s1.Format(_T("GetSN = %d "), res); s2.Empty(); if (res == KLink::KL_OK) { s1.Append(_T("\r\n")); for (int i = 0; i < 4; i++) { s1.AppendFormat(_T("%02X "), buf2[i]); s2.AppendFormat(_T("%02X"), buf2[4-i-1]); // s3.AppendFormat(_T("%c"), buf2[12 - i - 1]); } } SysLog(s1); SetDlgItemText(IDC_EDIT_ORG_FACT_SN, s2); res = pDoc->MyKLink1.GetFactoryData(1, 0, 0, 80, (unsigned short *)buf2); SysLog(pDoc->MyKLink1.m_resultStr); s1.Format(_T("GetFactoryData = %d "), res); s2.Empty(); if (res == KLink::KL_OK) { s1.Append(_T("\r\n")); for (int i = 0; i < 80; i++) { s1.AppendFormat(_T("%02X "), buf2[i]); s2.AppendFormat(_T("%02X"), buf2[4 - i - 1]); // s3.AppendFormat(_T("%c"), buf2[12 - i - 1]); } } SysLog(s1); // SetDlgItemText(IDC_EDIT_ORG_FACT_SN, s2); // unsigned char buf3[1024]; CTime ctime1; for (int i = 0; i < nFactoryDataItemsCount; i++) { // int nType = FactoryDataItems[i].nType; // int nSize = FactoryDataItems[i].nSize; // int nOffset = FactoryDataItems[i].nOffset; CString sName = FactoryDataItems[i].sName; s3 = GetDataStrByName(buf2,sName); s1.Format(_T("%d %s %s"), i, sName, s3); SysLog(s1); } s3=GetDataStrByName(buf2, _T("nLotNo")); SetDlgItemText(IDC_EDIT_ORG_LOT_NO, s3); s3 = GetDataStrByName(buf2, _T("nProductDate")); SetDlgItemText(IDC_EDIT_ORG_FAC_DATE, s3); return 0; } int CDialogFactCfg::WriteData() { // TODO: 在此处添加实现代码. CString s1; CMTerm2Doc* pDoc = (CMTerm2Doc *)GetDocument(); unsigned short buf1[1024]; // unsigned char buf2[1024]; // unsigned char buf3[1024]; /* AddDataItem(0, 2, typeBIN, _T("开始标志")); AddDataItem(2, 2, typeINT, _T("序号")); AddDataItem(4, 2, typeBIN, _T("ModelNo")); AddDataItem(6, 2, typeBIN, _T("ModelVer")); AddDataItem(8, 4, typeUINT, _T("nLotNo")); AddDataItem(12, 4, typeDATETIME, _T("nProductDate")); AddDataItem(16, 4, typeBIN, _T("nSN1")); AddDataItem(20, 4, typeBIN, _T("nProtocalVer")); AddDataItem(24, 4, typeBIN, _T("nDefaultFuncr")); AddDataItem(28, 16, typeString, _T("sModelStr")); AddDataItem(44, 16, typeString, _T("sLot_No")); AddDataItem(60, 16, typeString, _T("sSNStr")); AddDataItem(76, 2, typeBIN, _T("CRC1")); AddDataItem(78, 2, typeBIN, _T("EndSign")); */ UINT uValue; SetDataByName(buf1, _T("开始标志"), 0x55AA); SetDataByName(buf1, _T("序号"), 0x0002); SetDataByName(buf1, _T("ModelNo"), 0x0008); SetDataByName(buf1, _T("ModelVer"), 0x0100); GetDlgItemText(IDC_EDIT_LOT_NO, s1); uValue = _tstoi(s1); SetDataByName(buf1, _T("nLotNo"), uValue); SetDataByName(buf1, _T("nProductDate"), 0x2996c000); GetDlgItemText(IDC_EDIT_FACT_SN, s1); uValue = _tstoi(s1); SetDataByName(buf1, _T("nSN1"), uValue); SetDataByName(buf1, _T("nProtocalVer"), 0x01000100); SetDataByName(buf1, _T("nDefaultFuncr"), 0x00000001); SetDataByName(buf1, _T("sModelStr"), _T("12122121")); SetDataByName(buf1, _T("sLot_No"), _T("34344343")); SetDataByName(buf1, _T("sSNStr"), _T("56566565")); SetDataByName(buf1, _T("CRC1"), 0xEFEF); SetDataByName(buf1, _T("EndSign"), 0x55AA); int res = pDoc->MyKLink1.WriteFactoryData(1, 0, 0, 80, buf1); SysLog(pDoc->MyKLink1.m_resultStr); s1.Format(_T("WriteFactoryData = %d "), res); return 0; } void CDialogFactCfg::OnBnClickedButtonReaddata() { // TODO: 在此添加控件通知处理程序代码 ReadData(); } void CDialogFactCfg::OnBnClickedButtonConn() { // TODO: 在此添加控件通知处理程序代码 Connect(); } void CDialogFactCfg::OnBnClickedButtonDisconn() { // TODO: 在此添加控件通知处理程序代码 Disconnect(); } void CDialogFactCfg::OnBnClickedButtonWritedata() { // TODO: 在此添加控件通知处理程序代码 WriteData(); } CString saveInputStr=_T(""), saveInput1Str= _T(""); CString sDisplayStr; CString sStoreStr; bool bEditEndFlg = false; int CDialogFactCfg::CharKeyClick(CString sKey) { // TODO: 在此处添加实现代码. if (bEditEndFlg) { sDisplayStr.Empty(); bEditEndFlg = false; } sDisplayStr.Append(sKey); int len1 = sDisplayStr.GetLength(); if (len1 > 1 && sDisplayStr[0] == _T('0')) sDisplayStr = sDisplayStr.Right(sDisplayStr.GetLength() - 1); SetDlgItemText(IDC_EDIT_VIEW, sDisplayStr); return 0; } void CDialogFactCfg::OnBnClickedBtn1() { CharKeyClick(_T("1")); } void CDialogFactCfg::OnBnClickedBtn3() { CharKeyClick(_T("3")); } void CDialogFactCfg::OnBnClickedBtn4() { CharKeyClick(_T("4")); } void CDialogFactCfg::OnBnClickedBtn5() { CharKeyClick(_T("5")); } void CDialogFactCfg::OnBnClickedBtnClear() { sDisplayStr.Empty(); bEditEndFlg = false; SetDlgItemText(IDC_EDIT_VIEW, _T("0")); } void CDialogFactCfg::OnBnClickedBtnAdd() { bEditEndFlg = true; sStoreStr = sDisplayStr; //saveInput1Str = saveInputStr; //saveInputStr = ""; } void CDialogFactCfg::OnBnClickedBtnBackSpace() { int len1 = sDisplayStr.GetLength(); if (len1 > 1) { sDisplayStr = sDisplayStr.Left(sDisplayStr.GetLength() - 1); } else if (len1 == 1) { sDisplayStr = "0"; } // bEditEndFlg = false; SetDlgItemText(IDC_EDIT_VIEW, sDisplayStr); } void CDialogFactCfg::OnBnClickedButton0() { // TODO: 在此添加控件通知处理程序代码 CharKeyClick(_T("0")); } void CDialogFactCfg::OnBnClickedButton1Dot() { // TODO: 在此添加控件通知处理程序代码 CharKeyClick(_T(".")); } void CDialogFactCfg::OnBnClickedButtonEq() { // TODO: 在此添加控件通知处理程序代码 bEditEndFlg = true; float a = _tstof(sStoreStr); float b = _tstof(sDisplayStr); float c = a + b; sDisplayStr.Format(_T("%g"),c); SetDlgItemText(IDC_EDIT_VIEW, sDisplayStr); }