// MultiTerminal2Doc.cpp: CMTerm2Doc 类的实现 // #include "pch.h" #include "framework.h" // SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的 // ATL 项目中进行定义,并允许与该项目共享文档代码。 #ifndef SHARED_HANDLERS #include "MTerm2.h" #endif #include "MTerm2Doc.h" #include "MTerm2View.h" #include "MTerm2LdsView.h" #include "MTerm2BldView.h" #include "MTerm2BnlView.h" #include "MTerm2CoilView.h" #include "MTerm2DataView.h" #include "MTerm2CtrlView.h" #include "MTerm2TestView.h" #include "MTerm2ProgTxt.h" #include "MTerm2CommDevView.h" #include #include "CDialogCommSet1.h" #include "HvSerialPort.h" #include #ifdef _DEBUG #define new DEBUG_NEW #endif // CMTerm2Doc CMTerm2Doc::stTypeDef CMTerm2Doc::CoilTypeDef[] = { {KLCoilTypeX,"X"}, {KLCoilTypeY,"Y"}, {KLCoilTypeR,"R"}, {KLCoilTypeLX,"LX"}, {KLCoilTypeLY,"LY"}, {KLCoilTypeLR,"LR"}, {KLCoilTypeSR,"SR"}, {KLCoilTypeC,"C"}, {KLCoilTypeT,"T"}, }; int CMTerm2Doc::nCoilTypeDefCount = sizeof(CMTerm2Doc::CoilTypeDef) / sizeof(stTypeDef); CMTerm2Doc::stTypeDef CMTerm2Doc::DataTypeDef[] = { {KLDataTypeDEC,"K"}, {KLDataTypeHEX,"H"}, {KLDataTypeWX,"WX"}, {KLDataTypeWY,"WY"}, {KLDataTypeWR,"WR"}, {KLDataTypeDT,"DT"}, {KLDataTypeWLX,"WLX"}, {KLDataTypeWLY,"WLY"}, {KLDataTypeSDT,"SDT"}, {KLDataTypeWSR,"WSR"}, {KLDataTypeEV,"EV"}, {KLDataTypeSV,"SV"}, {KLDataTypeLD,"LD"}, }; int CMTerm2Doc::nDataTypeDefCount = sizeof(CMTerm2Doc::DataTypeDef) / sizeof(stTypeDef); CMTerm2Doc::stOpDef CMTerm2Doc::OpDef[] = { {OP_NOP,"NOP",0}, {OP_ST,"ST",1,KLParamCoil}, {OP_ST_,"ST/",1,KLParamCoil}, {OP_AN,"AN",1,KLParamCoil}, {OP_AN_,"AN/",1,KLParamCoil}, {OP_OR,"OR",1,KLParamCoil}, {OP_OR_,"OR/",1,KLParamCoil}, {OP_NOT,"NOT"}, {OP_ANS,"ANS"}, {OP_ORS,"ORS"} , {OP_PSHS,"PSHS"}, {OP_RDS,"RDS"}, {OP_POPS,"POPS"}, {OP_OUT,"OUT",1,KLParamCoil}, {OP_SET,"SET",1,KLParamCoil}, {OP_RESET,"RESET",1,KLParamCoil}, {OP_DF,"DF"}, {OP_DF_,"DF/"}, {OP_ST_EQ,"ST=",2,KLParamWord,"="}, {OP_ST_LT,"ST<",2,KLParamWord,"<"}, {OP_ST_GT,"ST>",2,KLParamWord,">"}, {OP_ST_LE,"ST<=",2,KLParamWord,"<="}, {OP_ST_GE,"ST>=",2,KLParamWord,">="}, {OP_ST_NE,"ST<>",2,KLParamWord,"<>"}, {OP_AN_EQ,"AN=",2,KLParamWord,"="}, {OP_AN_LT,"AN<",2,KLParamWord,"<"}, {OP_AN_GT,"AN>",2,KLParamWord,">"}, {OP_AN_LE,"AN<=",2,KLParamWord,"<="}, {OP_AN_GE,"AN>=",2,KLParamWord,">="}, {OP_AN_NE,"AN<>",2,KLParamWord,"<>"}, {OP_OR_EQ,"OR=",2,KLParamWord,"="}, {OP_OR_LT,"OR<",2,KLParamWord,"<"}, {OP_OR_GT,"OR>",2,KLParamWord,">"}, {OP_OR_LE,"OR<=",2,KLParamWord,"<="}, {OP_OR_GE,"OR>=",2,KLParamWord,">="}, {OP_OR_NE,"OR<>",2,KLParamWord,"<>"}, {OP_TML,"TML",2,KLParamWord,"TML"}, {OP_TMR,"TMR",2,KLParamWord,"TMR"}, {OP_TMX,"TMX",2,KLParamWord,"TMX"}, {OP_TMY,"TMY",2,KLParamWord,"TMY"}, {OP_MV,"MV",2,KLParamWord,"MV"}, {OP_INC,"INC",1,KLParamWord,"+1"}, {OP_ADD2,"ADD2",2,KLParamWord,"+"}, {OP_ADD3,"ADD3",3,KLParamWord,"+"}, {OP_DEC,"DEC",1,KLParamWord,"-1"}, {OP_SUB2,"SUB2",2,KLParamWord,"-"}, {OP_SUB3,"SUB3",3,KLParamWord,"-"}, {OP_MUL,"MUL",3,KLParamWord,"X"}, {OP_DIV,"DIV",3,KLParamWord,"/"}, }; int CMTerm2Doc::nOpDefCount = sizeof(CMTerm2Doc::OpDef) / sizeof(stOpDef); IMPLEMENT_DYNCREATE(CMTerm2Doc, CDocument) BEGIN_MESSAGE_MAP(CMTerm2Doc, CDocument) ON_COMMAND(ID_UPLOAD_FROM_PLC, &CMTerm2Doc::OnUploadFromPlc) ON_COMMAND(ID_ONLINE, &CMTerm2Doc::OnOnline) ON_UPDATE_COMMAND_UI(ID_ONLINE, &CMTerm2Doc::OnUpdateOnline) ON_COMMAND(ID_OFFLINE, &CMTerm2Doc::OnOffline) ON_UPDATE_COMMAND_UI(ID_OFFLINE, &CMTerm2Doc::OnUpdateOffline) ON_COMMAND(ID_PLC_RUN, &CMTerm2Doc::OnPlcRun) ON_UPDATE_COMMAND_UI(ID_PLC_RUN, &CMTerm2Doc::OnUpdatePlcRun) ON_COMMAND(ID_DOWNLOAD_TO_PLC, &CMTerm2Doc::OnDownloadToPlc) ON_UPDATE_COMMAND_UI(ID_DOWNLOAD_TO_PLC, &CMTerm2Doc::OnUpdateDownloadToPlc) ON_COMMAND(ID_COMUNICATION_SET, &CMTerm2Doc::OnComunicationSet) ON_COMMAND(ID_PLC_SYSREG_SET, &CMTerm2Doc::OnPlcSysregSet) ON_COMMAND(ID_MENU_VIEW_LDS, &CMTerm2Doc::OnMenuViewLds) ON_COMMAND(ID_MENU_VIEW_BLD, &CMTerm2Doc::OnMenuViewBld) ON_COMMAND(ID_MENU_VIEW_BNL, &CMTerm2Doc::OnMenuViewBnl) ON_COMMAND(ID_MENU_SHOW_CONSOLE, &CMTerm2Doc::OnMenuShowConsole) ON_COMMAND(ID_MENU_COMM_TEST, &CMTerm2Doc::OnMenuCommTest) ON_COMMAND(ID_SIMULATE, &CMTerm2Doc::OnSimulate) ON_UPDATE_COMMAND_UI(ID_SIMULATE, &CMTerm2Doc::OnUpdateSimulate) END_MESSAGE_MAP() // CMTerm2Doc 构造/析构 CMTerm2Doc::CMTerm2Doc() noexcept { // TODO: 在此添加一次性构造代码 MyKLink1.SetSendCallBackFunc(std::bind(&CMTerm2Doc::SendPacket, this, std::placeholders::_1, std::placeholders::_2)); MyKLink1.SetRecvCallBackFunc(std::bind(&CMTerm2Doc::RecvPacket, this, std::placeholders::_1, std::placeholders::_2)); StartTime = myHvSerialPort1.GetTimeMs(); } CMTerm2Doc::~CMTerm2Doc() { } int CMTerm2Doc::SendPacket(void * pBuf, int Len) { return myHvSerialPort1.Send((char *)pBuf, Len); } int CMTerm2Doc::RecvPacket(void * pBuf, int Len) { int j = myHvSerialPort1.Recv((char *)pBuf, Len); if (j <= 0) { CString s1; s1.Format(_T("Read %d R=%d %s"), Len, j, myHvSerialPort1.m_strResult); SysLog(s1); } return j; } BOOL CMTerm2Doc::OnNewDocument() { if (!CDocument::OnNewDocument()) return FALSE; // TODO: 在此添加重新初始化代码 // (SDI 文档将重用该文档) // LoadFromFile(_T("0prog1.txt")); return TRUE; } BOOL CMTerm2Doc::OnOpenDocument(LPCTSTR lpszPathName) { // if (!CDocument::OnOpenDocument(lpszPathName)) // return FALSE; // TODO: 在此添加您专用的创建代码 CString s1; s1.Format(_T("OnOpenDocument %s"), lpszPathName); SysLog(s1); LoadFromFile(lpszPathName); return TRUE; } BOOL CMTerm2Doc::OnSaveDocument(LPCTSTR lpszPathName) { // TODO: 在此添加专用代码和/或调用基类 CString s1; s1.Format(_T("OnSaveDocument %s"), lpszPathName); SysLog(s1); SaveToFile(lpszPathName); return TRUE; // return CDocument::OnSaveDocument(lpszPathName); } void CMTerm2Doc::OnCloseDocument() { // TODO: 在此添加专用代码和/或调用基类 // CString s1; // s1.Format(_T("OnCloseDocument %s")); // SysLog(s1); CDocument::OnCloseDocument(); } // CMTerm2Doc 序列化 void CMTerm2Doc::Serialize(CArchive& ar) { CString s1; if (ar.IsStoring()) { // TODO: 在此添加存储代码 ar.GetFile(); s1.Format(_T("Serialize Saving")); SysLog(s1); } else { // TODO: 在此添加加载代码 s1.Format(_T("Serialize Loading")); SysLog(s1); } } #ifdef SHARED_HANDLERS // 缩略图的支持 void CMTerm2Doc::OnDrawThumbnail(CDC& dc, LPRECT lprcBounds) { // 修改此代码以绘制文档数据 dc.FillSolidRect(lprcBounds, RGB(255, 255, 255)); CString strText = _T("TODO: implement thumbnail drawing here"); LOGFONT lf; CFont* pDefaultGUIFont = CFont::FromHandle((HFONT) GetStockObject(DEFAULT_GUI_FONT)); pDefaultGUIFont->GetLogFont(&lf); lf.lfHeight = 36; CFont fontDraw; fontDraw.CreateFontIndirect(&lf); CFont* pOldFont = dc.SelectObject(&fontDraw); dc.DrawText(strText, lprcBounds, DT_CENTER | DT_WORDBREAK); dc.SelectObject(pOldFont); } // 搜索处理程序的支持 void CMTerm2Doc::InitializeSearchContent() { CString strSearchContent; // 从文档数据设置搜索内容。 // 内容部分应由“;”分隔 // 例如: strSearchContent = _T("point;rectangle;circle;ole object;"); SetSearchContent(strSearchContent); } void CMTerm2Doc::SetSearchContent(const CString& value) { if (value.IsEmpty()) { RemoveChunk(PKEY_Search_Contents.fmtid, PKEY_Search_Contents.pid); } else { CMFCFilterChunkValueImpl *pChunk = nullptr; ATLTRY(pChunk = new CMFCFilterChunkValueImpl); if (pChunk != nullptr) { pChunk->SetTextValue(PKEY_Search_Contents, value, CHUNK_TEXT); SetChunkValue(pChunk); } } } #endif // SHARED_HANDLERS // CMTerm2Doc 诊断 #ifdef _DEBUG void CMTerm2Doc::AssertValid() const { CDocument::AssertValid(); } void CMTerm2Doc::Dump(CDumpContext& dc) const { CDocument::Dump(dc); } #endif //_DEBUG // CMTerm2Doc 命令 int CMTerm2Doc::LoadFromFile(CString sFilePathName) { // TODO: 在此处添加实现代码. CFile file1; CFileException e; bool r = file1.Open(sFilePathName, CFile::modeRead, &e); if (r) { int l = (int)file1.GetLength(); CStringA s1A; file1.Read(s1A.GetBuffer(l + 2048), l); file1.Close(); s1A.ReleaseBuffer(l); TransFileToProg(s1A); UpdateAllViews(NULL); } return 0; } int CMTerm2Doc::SaveToFile(CString sFilePathName) { // TODO: 在此处添加实现代码. CString s1; CFile file1; CFileException e; bool r = file1.Open(sFilePathName, CFile::modeReadWrite, &e); if (r) { CStringA s1A; CStringA sSectionNameA; //写入系统配置 GetSectionTxt(SectionSysCfg, sSectionNameA); // s1A = "[" + sSectionNameA + "]\r\n"; file1.Write(s1A, s1A.GetLength()); //写入程序 GetSectionTxt(SectionProg, sSectionNameA); // s1A = "[" + sSectionNameA + "]\r\n"; file1.Write(s1A, s1A.GetLength()); TransToTxt(s1A); file1.Write(s1A, s1A.GetLength()); //写入注释 GetSectionTxt(SectionAnno, sSectionNameA); // s1A = "[" + sSectionNameA + "]\r\n"; file1.Write(s1A, s1A.GetLength()); for (int i = 0; i < nCoilAnnoCount; i++) { if (mCoilAnnos[i].sAnno.IsEmpty()) { continue; } s1 = mCoilAnnos[i].sCoilName + _T("\t") + mCoilAnnos[i].sAnno+ _T("\r\n"); s1A = s1; file1.Write(s1A, s1A.GetLength()); } //写入触点监控列表 GetSectionTxt(SectionCoilList, sSectionNameA); // s1A = "[" + sSectionNameA + "]\r\n"; file1.Write(s1A, s1A.GetLength()); //写入数据监控列表 GetSectionTxt(SectionDataList, sSectionNameA); // s1A = "[" + sSectionNameA + "]\r\n"; file1.Write(s1A, s1A.GetLength()); file1.Close(); } return 0; } int CMTerm2Doc::TxtToSection(CStringA txt) { for (int i = 0; i < nSectionDefCount; i++) { if (SectionDef[i].SectionName == txt) { return SectionDef[i].nSectionType; } } return SectionNone; } int CMTerm2Doc::GetSectionTxt(int nSectionType, CStringA & txt) { for (int i = 0; i < nSectionDefCount; i++) { if (SectionDef[i].nSectionType == nSectionType) { txt = SectionDef[i].SectionName; return true; } } return false; } int CMTerm2Doc::GetSectionPos(int nSectionType, int * nSectionLine, int * nSectionLines) { for (int i = 0; i < nSectionCount; i++) { if (Sections[i].nSectionType == nSectionType) { *nSectionLine = Sections[i].nSectionLineNo; *nSectionLines = Sections[i].nLines; return true; } } return false; } int CMTerm2Doc::TxtToOp(CStringA optxt, int* ParamCount, int* ParamType) { for (int i = 0; i < nOpDefCount; i++){ if (OpDef[i].OpTxt == optxt) { *ParamCount = OpDef[i].nParamCount; *ParamType = OpDef[i].nParamType; return OpDef[i].nOpType1; } } return -1; } int CMTerm2Doc::OpToTxt(int nOp, CStringA & OpTxt) { for (int i = 0; i < nOpDefCount; i++){ if (OpDef[i].nOpType1 == nOp) { OpTxt = OpDef[i].OpTxt; return true; } } return false; } int CMTerm2Doc::OpToShowTxt(int nOp, CStringA & OpShowTxt) { for (int i = 0; i < nOpDefCount; i++) { if (OpDef[i].nOpType1 == nOp) { OpShowTxt = OpDef[i].ShowTxt; return true; } } return false; } int CMTerm2Doc::TxtToCoilType(CStringA Typetxt, int* nCoilType, int* nCoilAddr) { Typetxt.MakeUpper(); for (int i = 0; i < nCoilTypeDefCount; i++) { if (Typetxt.Find( CoilTypeDef[i].TypeTxt)==0) { *nCoilType = CoilTypeDef[i].nType; *nCoilAddr = atoi(Typetxt.Mid(CoilTypeDef[i].TypeTxt.GetLength())); return CoilTypeDef[i].nType; } } return false;; } int CMTerm2Doc::CoilTypeToTxt(int nType, CStringA & typeTxt) { for (int i = 0; i < nCoilTypeDefCount; i++) { if (CoilTypeDef[i].nType == nType){ typeTxt = CoilTypeDef[i].TypeTxt; return true; } } return false; } int CMTerm2Doc::TxtToDataType(CStringA Typetxt, int * nDataType, int* nDataAddr) { Typetxt.MakeUpper(); for (int i = 0; i < nDataTypeDefCount; i++) { if (Typetxt.Find(DataTypeDef[i].TypeTxt) == 0) { *nDataType = DataTypeDef[i].nType; *nDataAddr = atoi(Typetxt.Mid(DataTypeDef[i].TypeTxt.GetLength())); return true; } } return false; } int CMTerm2Doc::DataTypeToTxt(int nType, CStringA & typeTxt) { for (int i = 0; i < nDataTypeDefCount; i++) { if (DataTypeDef[i].nType == nType) { typeTxt = DataTypeDef[i].TypeTxt; return true; } } return false; } int CMTerm2Doc::GetAnno(unsigned short nType, unsigned short nAddr, CString & sAnno) { // TODO: 在此处添加实现代码. // 二分法 查找。 // map. for (int i = 0; i < nCoilAnnoCount; i++) { if (mCoilAnnos[i].nType == nType && mCoilAnnos[i].nAddr == nAddr) { sAnno = mCoilAnnos[i].sAnno; return 1; } } return 0; } int CMTerm2Doc::SetAnno(CString sCoilName, CString sAnno) { // TODO: 在此处添加实现代码. // 二分法 查找。 // map. int bValid = 0; int nType = 0, nAddr = 0; CStringA s1A; s1A = sCoilName; if (TxtToCoilType(s1A, &nType, &nAddr) || TxtToDataType(s1A, &nType, &nAddr)) { bValid = 1; } if (!bValid) return false; for (int i = 0; i < nCoilAnnoCount; i++) { if (mCoilAnnos[i].nType == nType && mCoilAnnos[i].nAddr == nAddr) { mCoilAnnos[i].sAnno = sAnno; return 1; } } mCoilAnnos[nCoilAnnoCount].nType = nType; mCoilAnnos[nCoilAnnoCount].nAddr = nAddr; mCoilAnnos[nCoilAnnoCount].sCoilName = sCoilName; mCoilAnnos[nCoilAnnoCount].sAnno = sAnno; nCoilAnnoCount++; return 2; } int CMTerm2Doc::SetAnno(unsigned short nType, unsigned short nAddr, CString sCoilName, CString sAnno) { // TODO: 在此处添加实现代码. // 二分法 查找。 // map. for (int i = 0; i < nCoilAnnoCount; i++) { if (mCoilAnnos[i].nType == nType && mCoilAnnos[i].nAddr == nAddr) { mCoilAnnos[i].sAnno = sAnno; return 1; } } mCoilAnnos[nCoilAnnoCount].nType = nType; mCoilAnnos[nCoilAnnoCount].nAddr = nAddr; mCoilAnnos[nCoilAnnoCount].sCoilName = sCoilName; mCoilAnnos[nCoilAnnoCount].sAnno = sAnno; nCoilAnnoCount++; return 0; } int CMTerm2Doc::TransLinesToProg(const CStringArray & txtLines, int StartLine , int ProgLines) { CString s1; int txtLineCount = (int)txtLines.GetSize(); //读取程序 章节 int nProgStartLine = 0; int nProgLines = 0; nProgStartLine = StartLine; if (ProgLines == -1) nProgLines = txtLineCount; else nProgLines = ProgLines; int nProgPos = 0; for (int i = nProgStartLine; i < nProgStartLine + nProgLines; i++) { CString sLine; sLine = txtLines.GetAt(i); sLine.Trim(); sLine.Replace(_T("\t"), _T(" ")); sLine.Replace(_T(" "), _T(" ")); sLine.Replace(_T(" "), _T(" ")); CStringArray strarr2; DivideStringToArray(sLine, _T(" "), strarr2); if (strarr2.GetSize() == 0) continue; CString sCmd; sCmd = strarr2.GetAt(0); sCmd.MakeUpper(); CStringA s1A; s1A = sCmd; int nParamCount, nParamType; int nType, nAddr; int k = TxtToOp(s1A, &nParamCount, &nParamType); if (k >= 0) { Progs[nProgPos].nOpType1 = k; for (int j = 0; j < 3 && j < nParamCount && j < strarr2.GetSize() - 1; j++) { s1A = strarr2.GetAt(j + 1); s1A.MakeUpper(); Progs[nProgPos].Params[j].sParamStr = s1A; if (nParamType == KLParamCoil) { TxtToCoilType(s1A, &nType, &nAddr); Progs[nProgPos].Params[j].nParamType = nType; Progs[nProgPos].Params[j].nParamAddr = nAddr; } else if (nParamType == KLParamWord) { TxtToDataType(s1A, &nType, &nAddr); Progs[nProgPos].Params[j].nParamType = nType; Progs[nProgPos].Params[j].nParamAddr = nAddr; } else { } } Progs[nProgPos].nParamCount = (int)strarr2.GetSize() - 1; Progs[nProgPos].PairTo = 0; nProgPos++; } } m_nProgSteps = nProgPos; // 先扫描分开的程序段 int stpos[100] = { 0 }; int nSts = 0; int StackDeeps[512] = { 0 }; int nCurStackDeep = 0; // 查找匹配指令和地址 s1.Format(_T("DOC::Trans to Prog ")); SysLog(s1); for (int i = 0; i < m_nProgSteps; i++) { int nOpType = Progs[i].nOpType1; int nParamCount = Progs[i].nParamCount; switch (Progs[i].nOpType1) { case OP_ST: case OP_ST_: case OP_ST_EQ: case OP_ST_NE: case OP_ST_LT: case OP_ST_GT: case OP_ST_LE: case OP_ST_GE: nCurStackDeep++; s1.Format(_T("St Deep %d pos %d "), nCurStackDeep, i); //SysLog(s1); stpos[nSts] = i; nSts++; break; case OP_ANS: case OP_ORS: Progs[stpos[nSts - 1]].PairTo = i; // CMTerm2Doc::OP_ANS; s1.Format(_T("Pair deep %d %d -- %d"), nCurStackDeep, stpos[nSts - 1], i); //SysLog(s1); nCurStackDeep--; nSts--; break; case OP_PSHS: break; case OP_POPS: break; break; default: break; } StackDeeps[i] = nCurStackDeep; } s1.Format(_T("Remaining STs %d"), nSts); SysLog(s1); for (int i = 0; i < nSts; i++) { s1.Format(_T("[%d] %d "), i, stpos[i]); SysLog(s1); } s1.Format(_T("Pairs")); SysLog(s1); for (int i = 0; i < m_nProgSteps; i++) { int nPairTo = Progs[i].PairTo; if (nPairTo > 0) { s1.Format(_T("%d -- %d type:%d"), i, nPairTo, Progs[nPairTo].nOpType1); SysLog(s1); } } TransProgToBin(); return 0; } int CMTerm2Doc::TransTxtToProg(CStringA ProgTxtA) { CString s1; CString sProg; sProg = ProgTxtA; CStringArray txtLines; sProg.Replace(_T("\r\n"), _T("\n")); sProg.Replace(_T("\r"), _T("\n")); DivideStringToArray(sProg, _T("\n"), txtLines); TransLinesToProg(txtLines); return 0; } int CMTerm2Doc::ReadAnnoFromTxt(CStringA AnnoTxtA) { // 读取注释章节 CString s1; CString sProg; sProg = AnnoTxtA; CStringArray txtLines; sProg.Replace(_T("\r\n"), _T("\n")); sProg.Replace(_T("\r"), _T("\n")); DivideStringToArray(sProg, _T("\n"), txtLines); int txtLineCount = (int)txtLines.GetSize(); nCoilAnnoCount = 0; int nAnnoSectionLine = 0; int nAnnoStartLine = 0; int nAnnoLines = 0; int bAnnoSection = GetSectionPos(SectionAnno, &nAnnoSectionLine, &nAnnoLines); if (bAnnoSection) { nAnnoStartLine = nAnnoSectionLine + 1; } for (int i = nAnnoStartLine; i < nAnnoStartLine + nAnnoLines; i++) { CString sLine; sLine = txtLines.GetAt(i); sLine.Trim(); int DivPos = sLine.Find(_T("\t")); if (DivPos < 1) continue; CString sCoilName; sCoilName = sLine.Left(DivPos); sCoilName.MakeUpper(); CString sAnno; sAnno = sLine.Mid(DivPos + 1); // SetAnno(sCoilName, sAnno); CStringA s1A; s1A = sCoilName; int nParamType, nAddr; TxtToCoilType(s1A, &nParamType, &nAddr) || TxtToDataType(s1A, &nParamType, &nAddr); mCoilAnnos[nCoilAnnoCount].nType = nParamType; mCoilAnnos[nCoilAnnoCount].nAddr = nAddr; mCoilAnnos[nCoilAnnoCount].sCoilName = sCoilName; mCoilAnnos[nCoilAnnoCount].sAnno = sAnno; nCoilAnnoCount++; } return 0; } int CMTerm2Doc::TransFileToProg(CStringA ProgTxtA) { CString s1; CString sProg; sProg = ProgTxtA; CStringArray txtLines; sProg.Replace(_T("\r\n"), _T("\n")); sProg.Replace(_T("\r"), _T("\n")); DivideStringToArray(sProg, _T("\n"), txtLines); nSectionCount = 0; int nCurSection = 0; int txtLineCount = (int)txtLines.GetSize(); for (int i = 0; i < txtLineCount; i++) { CString sLine; sLine = txtLines.GetAt(i); sLine.Trim(); int nRightBracket; if (sLine.Find(_T("[")) == 0 && (nRightBracket = sLine.Find(_T("]"))) > 1) { //找到 [ ] 标志 if (nSectionCount > 0) { Sections[nSectionCount - 1].nLines = i - Sections[nSectionCount - 1].nSectionLineNo - 1; s1.Format(_T("- Line %d Section %d Ends, %d Lines"), i, nSectionCount -1 ,Sections[nSectionCount - 1].nLines); SysLog(s1); } CString sSectionName; CStringA sSectionNameA; sSectionName = sLine.Mid(1, nRightBracket - 1); sSectionNameA = sSectionName; int theSection = TxtToSection(sSectionNameA); s1.Format(_T("+ Line %d Section %d : [%s] type:%d "), i + 1, nSectionCount, sSectionName, theSection); SysLog(s1); Sections[nSectionCount].nSectionType = theSection; Sections[nSectionCount].nSectionLineNo = i; nSectionCount++; } } if (nSectionCount > 0) { Sections[nSectionCount - 1].nLines = txtLineCount - Sections[nSectionCount - 1].nSectionLineNo - 1; s1.Format(_T("Section Ends at Line %d Lines %d "), txtLineCount, Sections[nSectionCount - 1].nLines); SysLog(s1); } //读取程序 章节 int nProgSectionLine = 0; int nProgStartLine = 0; int nProgLines = 0; int bProgSection = GetSectionPos(SectionProg, &nProgSectionLine, &nProgLines); if (bProgSection) { nProgStartLine = nProgSectionLine + 1; } else { nProgStartLine = 0; nProgLines = txtLineCount; } TransLinesToProg(txtLines,nProgStartLine,nProgLines); // 读取注释章节 nCoilAnnoCount = 0; int nAnnoSectionLine = 0; int nAnnoStartLine = 0; int nAnnoLines = 0; int bAnnoSection = GetSectionPos(SectionAnno, &nAnnoSectionLine, &nAnnoLines); if (bAnnoSection) { nAnnoStartLine = nAnnoSectionLine + 1; } for (int i = nAnnoStartLine; i < nAnnoStartLine + nAnnoLines; i++) { CString sLine; sLine = txtLines.GetAt(i); sLine.Trim(); int DivPos = sLine.Find(_T("\t")); if (DivPos < 1) continue; CString sCoilName; sCoilName = sLine.Left(DivPos); sCoilName.MakeUpper(); CString sAnno; sAnno = sLine.Mid(DivPos + 1); // SetAnno(sCoilName, sAnno); CStringA s1A; s1A = sCoilName; int nParamType,nAddr; TxtToCoilType(s1A, &nParamType,&nAddr)||TxtToDataType(s1A, &nParamType, &nAddr); mCoilAnnos[nCoilAnnoCount].nType = nParamType; mCoilAnnos[nCoilAnnoCount].nAddr = nAddr; mCoilAnnos[nCoilAnnoCount].sCoilName = sCoilName; mCoilAnnos[nCoilAnnoCount].sAnno = sAnno; nCoilAnnoCount++; } return 0; } int CMTerm2Doc::TransToTxt(CStringA &ProgTxt) { CStringA s1, s2; for (int i = 0; i < m_nProgSteps; i++) { OpToTxt(Progs[i].nOpType1,s1); if (Progs[i].nParamCount > 0) { s1.Append("\t" + Progs[i].Params[0].sParamStr); } if (Progs[i].nParamCount > 1) { s1.Append("\t" + Progs[i].Params[1].sParamStr); } if (Progs[i].nParamCount > 2) { s1.Append("\t" + Progs[i].Params[2].sParamStr); } s1.Append("\r\n"); s2 += s1; } ProgTxt = s2; return 0; } int CMTerm2Doc::TransProgToBin() { CString s1; stBinProg2 * pBinProg2; stBinProg15 * pBinProg15; stBinProg3 * pBinProg3; nBinProgSteps = 0; for (int i = 0; i < m_nProgSteps; i++) { int nOpType = Progs[i].nOpType1; Progs[i].nBinStep = nBinProgSteps; int nParamType1 = Progs[i].Params[0].nParamType; int nParamAddr1 = Progs[i].Params[0].nParamAddr; int nParamType2, nParamAddr2; int nParamType3, nParamAddr3; if (Progs[i].nParamCount > 1) { nParamType2 = Progs[i].Params[1].nParamType; nParamAddr2 = Progs[i].Params[1].nParamAddr; } if (Progs[i].nParamCount > 2) { nParamType3 = Progs[i].Params[2].nParamType; nParamAddr3 = Progs[i].Params[2].nParamAddr; } switch (nOpType) { case OP_NONE: break; //case OP_NOP: break; //无参数 指令 case OP_NOT: case OP_ANS: case OP_ORS: case OP_PSHS: case OP_RDS: case OP_POPS: case OP_DF: case OP_DF_: BinProgs[nBinProgSteps].nOp = nOpType; nBinProgSteps += 1; break; // 1参数指令 case OP_ST: case OP_ST_: case OP_AN: case OP_AN_: case OP_OR: case OP_OR_: BinProgs[nBinProgSteps].nOp = nOpType; BinProgs[nBinProgSteps].nParamType = nParamType1; BinProgs[nBinProgSteps].nParamAddr = nParamAddr1; nBinProgSteps += 1; break; // 1 参数 输出 case OP_OUT: case OP_SET: case OP_RESET: BinProgs[nBinProgSteps].nOp = nOpType; BinProgs[nBinProgSteps].nParamType = nParamType1; BinProgs[nBinProgSteps].nParamAddr = nParamAddr1; nBinProgSteps += 1; break; // 比较指令 case OP_ST_EQ: case OP_ST_NE: case OP_ST_LT: case OP_ST_GT: case OP_ST_LE: case OP_ST_GE: case OP_AN_EQ: case OP_AN_NE: case OP_AN_LT: case OP_AN_GT: case OP_AN_LE: case OP_AN_GE: case OP_OR_EQ: case OP_OR_NE: case OP_OR_LT: case OP_OR_GT: case OP_OR_LE: case OP_OR_GE: pBinProg2 = (stBinProg2 *)(&BinProgs[nBinProgSteps]); BinProgs[nBinProgSteps].nOp = nOpType; BinProgs[nBinProgSteps].nParamType = 0; BinProgs[nBinProgSteps].nParamAddr = nParamAddr1; BinProgs[nBinProgSteps+1].nOp = nParamType1; BinProgs[nBinProgSteps+1].nParamType = nParamType2; BinProgs[nBinProgSteps+1].nParamAddr = nParamAddr2; nBinProgSteps += 2; break; // 定时器 case OP_TML: case OP_TMR: case OP_TMX: case OP_TMY: pBinProg15 = (stBinProg15 *)(&BinProgs[nBinProgSteps]); BinProgs[nBinProgSteps].nOp = nOpType; BinProgs[nBinProgSteps].nParamType = nParamAddr1; BinProgs[nBinProgSteps].nParamAddr = nParamAddr2; BinProgs[nBinProgSteps + 1].nOp = nParamType2; BinProgs[nBinProgSteps + 1].nParamType = 0; BinProgs[nBinProgSteps + 1].nParamAddr = 0; nBinProgSteps += 2; break; // 1 参数高级指令 case OP_INC: case OP_DEC: pBinProg15 = (stBinProg15 *)(&BinProgs[nBinProgSteps]); pBinProg15->nOp = nOpType; pBinProg15->nOpNum = 0; pBinProg15->nParamType1 = nParamType1; pBinProg15->nParamAddr1 = nParamAddr1; pBinProg15->resvr1 = 0; pBinProg15->resvr2 = 0; nBinProgSteps += 2; break; // 2参数高级指令 case OP_MV: case OP_ADD2: case OP_SUB2: pBinProg2 = (stBinProg2 *)(&BinProgs[nBinProgSteps]); pBinProg2->nOp = nOpType; pBinProg2->nOpNum = 0; pBinProg2->nParamType1 = nParamType1; pBinProg2->nParamAddr1 = nParamAddr1; pBinProg2->nParamType2 = nParamType2; pBinProg2->nParamAddr2 = nParamAddr2; nBinProgSteps += 2; break; // 3 参数高级指令 case OP_ADD3: case OP_SUB3: case OP_MUL: case OP_DIV: pBinProg3 = (stBinProg3 *)(&BinProgs[nBinProgSteps]); pBinProg3->nOp = nOpType; pBinProg3->nOpNum = 0; pBinProg3->nParamType1 = nParamType1; pBinProg3->nParamAddr1 = nParamAddr1; pBinProg3->nParamType2 = nParamType2; pBinProg3->nParamAddr2 = nParamAddr2; pBinProg3->nParamType3 = nParamType3; pBinProg3->nParamAddr3 = nParamAddr3; nBinProgSteps += 3; break; default: break; } } s1.Format(_T("%d steps to %d binSteps "), m_nProgSteps, nBinProgSteps); DbgLog(s1); return 0; } int CMTerm2Doc::TransBinToProg() { return 0; } void CMTerm2Doc::OnUploadFromPlc() { // TODO: 在此添加命令处理程序代码 int r; CString s1; if (!m_bOnline) { r = Connect(); MyKLink1.Open(); } if (!m_bOnline) return; r = MyKLink1.ReadRunStat(1,0,0,32,(unsigned short *)&MyKLink1.KMRunStat); // AfxMessageBox(_T("UploadFrom PLC From Doc")); CString s2; s2.Format(_T("Cur Programs %d \r\n"), nBinProgSteps); unsigned short * pBinBuf2 = (unsigned short *)BinProgs; for (int i = 0; i < nBinProgSteps * 2; i += 8) { s1.Format(_T("%03X: "), i); for (int j = 0; j < 8 && i+j < nBinProgSteps * 2; j++) { s1.AppendFormat(_T("%04X "), pBinBuf2[i + j]); } s2 += s1 + _T("\r\n"); } DbgLog(s2); int nBinSteps = MyKLink1.KMRunStat.nBinProgSize; s1.Format(_T("program to upload Size %d "), nBinSteps); DbgLog(s1); USHORT Buf3[2048]; s2.Empty(); int nUploadSize = nBinSteps *2; int nSteps = 64; for (int i = 0; i < nUploadSize; i += nSteps) { if (i + nSteps > nUploadSize) { nSteps = nUploadSize - i; } s1.Format(_T("2 Uploading %d to %d "), i, i + nSteps); DbgLog(s1); int r = MyKLink1.ReadProgram(1, 2, i, nSteps*2 , Buf3+i); s1.Format(_T("Download r = %d "), r); DbgLog(s1); //Update Progress Bar } s2.Format(_T(" Uploaded from bank 2 \r\n")); for (int i = 0; i < nBinSteps * 2; i += 8) { s1.Format(_T("%03X: "), i); for (int j = 0; j < 8 && i + j < nBinSteps * 2; j++) { s1.AppendFormat(_T("%04X "), Buf3[i + j]); } s2 += s1 + _T("\r\n"); } DbgLog(s2); for (int i = 0; i < nBinSteps * 2; i++) { ((USHORT *)BinProgs)[i] = Buf3[i]; } nBinProgSteps = nBinSteps; TransBinToProg(); UpdateAllViews(NULL); } int CMTerm2Doc::DownloadToPLC() { // TODO: 在此处添加实现代码. int r; CString s1; if (!m_bOnline) { r=Connect(); MyKLink1.Open(); } if (!m_bOnline) return 0; if (m_bPlcRunning) { r = AfxMessageBox(_T("PLC Running, Stop it?"), MB_YESNO); if (r == IDYES) { StopPLC();} else { return 0; } } // 下载程序 s1.Format(_T("Start Download Program ...")); DbgLog(s1); r = MyKLink1.StartProgram(1,2); s1.Format(_T("Result = %d"),r); DbgLog(s1); int DownloadSize = nBinProgSteps; int Steps = 16; for (int i = 0; i < DownloadSize; i += Steps) { //int Steps = 16; if (i + Steps > DownloadSize) { Steps = DownloadSize - i; } s1.Format(_T("Downloading %d to %d "), i, i + Steps); DbgLog(s1); r = MyKLink1.WriteProgram(1, 2, (i)*4, Steps * 4, (USHORT *)&BinProgs[i]); s1.Format(_T("Download r = %d "), r); DbgLog(s1); //Update Progress Bar } s1.Format(_T("Finish Downloading ")); DbgLog(s1); r = MyKLink1.FinishProgram(1,2,nBinProgSteps); s1.Format(_T("Download Finished r = %d "), r); DbgLog(s1); // 下载注释 // 下载系统寄存器配置 return 0; } void CMTerm2Doc::OnDownloadToPlc() { // TODO: 在此添加命令处理程序代码 DownloadToPLC(); } void CMTerm2Doc::OnUpdateDownloadToPlc(CCmdUI *pCmdUI) { // TODO: 在此添加命令更新用户界面处理程序代码 } void CMTerm2Doc::OnOnline() { // TODO: 在此添加命令处理程序代码 int r; if (!m_bOnline) { r = Connect(); MyKLink1.Open(); } if (!m_bOnline) return; // m_bOnline = true; UpdateAllViews(NULL, UpdataHint::UpdateStat); } void CMTerm2Doc::OnUpdateOnline(CCmdUI *pCmdUI) { // TODO: 在此添加命令更新用户界面处理程序代码 pCmdUI->SetCheck(m_bOnline == true); } void CMTerm2Doc::OnOffline() { // TODO: 在此添加命令处理程序代码 int r; if (m_bOnline) { r = DisConnect(); MyKLink1.Close(); } m_bOnline = false; UpdateAllViews(NULL, UpdataHint::UpdateStat); } void CMTerm2Doc::OnUpdateOffline(CCmdUI *pCmdUI) { // TODO: 在此添加命令更新用户界面处理程序代码 pCmdUI->SetCheck(m_bOnline == false); } void CMTerm2Doc::OnSimulate() { // TODO: 在此添加命令处理程序代码 if (!m_bSimulate) { myKMachine1.Download(BinProgs, nBinProgSteps); myKMachine1.StartPLC(); m_bSimulate = true; m_bOnline = true; StartPLC(); } else { m_bSimulate = false; m_bOnline = false; StopPLC(); } UpdateAllViews(NULL, UpdataHint::UpdateStat); } void CMTerm2Doc::OnUpdateSimulate(CCmdUI *pCmdUI) { // TODO: 在此添加命令更新用户界面处理程序代码 pCmdUI->SetCheck(m_bSimulate == true); } int CMTerm2Doc::StartPLC() { // TODO: 在此处添加实现代码. for (int i = 0; i < KLDataWXCount; i++) { KMem.WX[i] = 0; } for (int i = 0; i < KLDataWYCount; i++) { KMem.WY[i] = 0; } for (int i = 0; i < KLDataWRCount; i++) { KMem.WR[i] = 0; } for (int i = 0; i < KLDataDTCount; i++) { KMem.DT[i] = 0; } for (int i = 0; i < TOTALTIMERS; i++) { KMem.Timers[i] = { 0 }; } nScanCount = 0; m_bPlcRunning = 1; return 0; } int CMTerm2Doc::StopPLC() { // TODO: 在此处添加实现代码. m_bPlcRunning = false; return 0; } void CMTerm2Doc::OnPlcRun() { // TODO: 在此添加命令处理程序代码 if (m_bPlcRunning) { StopPLC(); } else { StartPLC(); } UpdateAllViews(NULL, UpdataHint::UpdateStat); } void CMTerm2Doc::OnUpdatePlcRun(CCmdUI *pCmdUI) { // TODO: 在此添加命令更新用户界面处理程序代码 pCmdUI->SetCheck(m_bPlcRunning == true); if (!m_bPlcRunning) pCmdUI->SetText(_T("PLC模式[PROG]")); else pCmdUI->SetText(_T("PLC模式[RUN]")); } int CMTerm2Doc::SetCommParam() { // TODO: 在此处添加实现代码. CString s1; s1.Format(_T("Doc SetupComm")); SysLog(s1); CDialogCommSet1 dialog1; INT_PTR r = dialog1.DoModal(); if (r == IDOK ) { nComPort=dialog1.m_nComNum; nBaud=dialog1.m_nBaudRate; ComSettings=dialog1.m_Settings; s1.Format(_T("dialog return port %d baud %d setting %S"), nComPort, nBaud, ComSettings); SysLog(s1); m_bCommParamSet = true; } else { return -1; } return 0; } int CMTerm2Doc::Connect() { // TODO: 在此处添加实现代码. CString s1; if (!m_bCommParamSet) { if (SetCommParam()) return -1; } if (m_bOnline) return 0; myHvSerialPort1.m_nPort = nComPort; myHvSerialPort1.m_nBaudRate = nBaud; myHvSerialPort1.m_Settings = "8,N,1"; int r = myHvSerialPort1.Open(); s1.Format(_T("Open %s = %d"), myHvSerialPort1.m_strResult, r); SysLog(s1); if (r == myHvSerialPort1.R_OK) { m_bOnline = true; // MyKLink1.Open(); // m_static_connect.SetCtlColor(RGB(0, 255, 0)); return 0; } // m_bOnline = true; return 0; } int CMTerm2Doc::DisConnect() { // TODO: 在此处添加实现代码. if (!m_bOnline) return -1; myHvSerialPort1.Close(); // MyKLink1.Close(); m_bOnline = false; return 0; } void CMTerm2Doc::OnComunicationSet() { // TODO: 在此添加命令处理程序代码 SetCommParam(); } void CMTerm2Doc::OnPlcSysregSet() { // TODO: 在此添加命令处理程序代码 } CView * CMTerm2Doc::FindView(CRuntimeClass * pClass) { CView* pView=NULL; POSITION pos = GetFirstViewPosition(); while (pos != NULL) { pView = GetNextView(pos); if (pView->IsKindOf(pClass)) break; } if (!pView->IsKindOf(pClass)) { //AfxMessageBox("Connt Locate the View."); return NULL; } return pView; } ///* void CMTerm2Doc::OnMenuViewLds() { // TODO: 在此添加命令处理程序代码 CString s1; CView * pView; pView = FindView(RUNTIME_CLASS(CMTerm2View)); if (pView != NULL) { ((CMDIFrameWndEx *)AfxGetMainWnd())->MDIActivate(pView->GetParent()); // pView->SetActiveWindow(); } else { s1 = GetTitle(); //AfxMessageBox(s1); ASSERT_VALID(theApp.pDocTemplate); CFrameWnd * pFrame = theApp.pDocTemplate->CreateNewFrame(this, NULL); ASSERT_KINDOF(CFrameWnd, pFrame); theApp.pDocTemplate->InitialUpdateFrame(pFrame, this); } } void CMTerm2Doc::OnMenuViewBld() { // TODO: 在此添加命令处理程序代码 CString s1; CView * pView; pView = FindView(RUNTIME_CLASS(CMTerm2BldView)); if (pView != NULL) { ((CMDIFrameWndEx *)AfxGetMainWnd())->MDIActivate(pView->GetParent()); // pView->SetActiveWindow(); } else { s1 = GetTitle(); //AfxMessageBox(s1); ASSERT_VALID(theApp.m_pBldViewTemplate); CFrameWnd * pFrame = theApp.m_pBldViewTemplate->CreateNewFrame(this, NULL); ASSERT_KINDOF(CFrameWnd, pFrame); theApp.m_pBldViewTemplate->InitialUpdateFrame(pFrame, this); } } void CMTerm2Doc::OnMenuViewBnl() { // TODO: 在此添加命令处理程序代码 CString s1; CView * pView; pView = FindView(RUNTIME_CLASS(CMTerm2BnlView)); if (pView != NULL) { ((CMDIFrameWndEx *)AfxGetMainWnd())->MDIActivate(pView->GetParent()); // pView->SetActiveWindow(); } else { s1 = GetTitle(); //AfxMessageBox(s1); ASSERT_VALID(theApp.m_pBnlViewTemplate); CFrameWnd * pFrame = theApp.m_pBnlViewTemplate->CreateNewFrame(this, NULL); ASSERT_KINDOF(CFrameWnd, pFrame); theApp.m_pBnlViewTemplate->InitialUpdateFrame(pFrame, this); } } //*/ void CMTerm2Doc::OnMenuShowConsole() { // TODO: 在此添加命令处理程序代码 CString s1; CView * pView; pView = FindView(RUNTIME_CLASS(CMTerm2CtrlView)); if (pView != NULL) { ((CMDIFrameWndEx *)AfxGetMainWnd())->MDIActivate(pView->GetParent()); // pView->SetActiveWindow(); } else { s1 = GetTitle(); //AfxMessageBox(s1); ASSERT_VALID(theApp.m_pCtrlViewTemplate); CFrameWnd * pFrame = theApp.m_pCtrlViewTemplate->CreateNewFrame(this, NULL); ASSERT_KINDOF(CFrameWnd, pFrame); theApp.m_pCtrlViewTemplate->InitialUpdateFrame(pFrame, this); } } void CMTerm2Doc::OnMenuCommTest() { // TODO: 在此添加命令处理程序代码 CString s1; CView * pView; pView = FindView(RUNTIME_CLASS(CMTerm2CommDevView)); if (pView != NULL) { ((CMDIFrameWndEx *)AfxGetMainWnd())->MDIActivate(pView->GetParent()); // pView->SetActiveWindow(); } else { s1 = GetTitle(); //AfxMessageBox(s1); ASSERT_VALID(theApp.m_pCommDevViewTemplate); CFrameWnd * pFrame = theApp.m_pCommDevViewTemplate->CreateNewFrame(this, NULL); ASSERT_KINDOF(CFrameWnd, pFrame); theApp.m_pCommDevViewTemplate->InitialUpdateFrame(pFrame, this); } } const unsigned short bitMasks[16] = { 0x1 << 0, 0x1 << 1, 0x1 << 2, 0x1 << 3, 0x1 << 4, 0x1 << 5, 0x1 << 6, 0x1 << 7, 0x1 << 8, 0x1 << 9, 0x1 << 10, 0x1 << 11, 0x1 << 12, 0x1 << 13, 0x1 << 14, 0x1 << 15, }; inline void SetAddrBit(unsigned short * pW, unsigned char bitAddr) { (*pW) |= bitMasks[bitAddr & 0xf]; } inline void ResetBit(unsigned short * pW, unsigned char bitAddr) { (*pW) &= ~bitMasks[bitAddr & 0xf]; } static inline void SetBitValue(unsigned short * pW, unsigned char bitAddr, unsigned char Value) { if (Value) { SetAddrBit(pW, bitAddr); } else { ResetBit(pW, bitAddr); } } static inline unsigned char GetBitValue(unsigned short W, unsigned char bitAddr) { if (W&bitMasks[bitAddr & 0xf]) return 1; else return 0; } int CMTerm2Doc::GetCoilValue(int nCoilType, int nCoilAddr) { // TODO: 在此处添加实现代码. if (m_bOnline && 0) { unsigned char value = 0; MyKLink1.ReadBit(1, nCoilType, nCoilAddr, &value); return value; } int nWordAddr = nCoilAddr >> 4; int nBitAddr = nCoilAddr & 0x0F; switch (nCoilType) { case KLCoilTypeX: if (nCoilAddr >= KLCoilXCount) return 0; return GetBitValue(KMem.WX[nWordAddr], nBitAddr); break; case KLCoilTypeY: if (nCoilAddr >= KLCoilYCount) return 0; return GetBitValue(KMem.WY[nWordAddr], nBitAddr); break; case KLCoilTypeR: if (nCoilAddr >= KLCoilRCount) return 0; return GetBitValue(KMem.WR[nWordAddr], nBitAddr); break; case KLCoilTypeLX: if (nCoilAddr >= KLCoilLXCount) return 0; return GetBitValue(KMem.WLX[nWordAddr], nBitAddr); break; case KLCoilTypeLY: if (nCoilAddr >= KLCoilLYCount) return 0; return GetBitValue(KMem.WLY[nWordAddr], nBitAddr); break; case KLCoilTypeT: if (nCoilAddr >= KLCoilTCount) return 0; return GetBitValue(KMem.WT[nWordAddr], nBitAddr); break; case KLCoilTypeC: if (nCoilAddr >= KLCoilCCount) return 0; return GetBitValue(KMem.WC[nWordAddr], nBitAddr); break; case KLCoilTypeLR: if (nCoilAddr >= KLCoilLRCount) return 0; return GetBitValue(KMem.WLR[nWordAddr], nBitAddr); break; case KLCoilTypeSR: if (nCoilAddr >= KLCoilSRCount) return 0; return GetBitValue(KMem.WSR[nWordAddr], nBitAddr); break; } return 0; } int CMTerm2Doc::SetCoilValue(int nCoilType, int nCoilAddr, int nCoilValue) { // TODO: 在此处添加实现代码. if (m_bSimulate) { return myKMachine1.SetCoilValue(nCoilType, nCoilAddr, nCoilValue); }else if (m_bOnline) { unsigned char value =nCoilValue; int res = 0; res = MyKLink1.WriteBit(1, nCoilType, nCoilAddr, value); return res; } int nWordAddr = nCoilAddr >> 4; int nBitAddr = nCoilAddr & 0x0F; switch (nCoilType) { case KLCoilTypeX: if (nCoilAddr >= KLCoilXCount) return 0; SetBitValue(&KMem.WX[nWordAddr], nBitAddr, nCoilValue); break; case KLCoilTypeY: if (nCoilAddr >= KLCoilYCount) return 0; SetBitValue(&KMem.WY[nWordAddr], nBitAddr, nCoilValue); break; case KLCoilTypeR: if (nCoilAddr >= KLCoilRCount) return 0; SetBitValue(&KMem.WR[nWordAddr], nBitAddr, nCoilValue); break; case KLCoilTypeLX: if (nCoilAddr >= KLCoilLXCount) return 0; SetBitValue(&KMem.WLX[nWordAddr], nBitAddr, nCoilValue); break; case KLCoilTypeLY: if (nCoilAddr >= KLCoilLYCount) return 0; SetBitValue(&KMem.WLY[nWordAddr], nBitAddr, nCoilValue); break; case KLCoilTypeT: if (nCoilAddr >= KLCoilTCount) return 0; SetBitValue(&KMem.WT[nWordAddr], nBitAddr, nCoilValue); break; case KLCoilTypeC: if (nCoilAddr >= KLCoilCCount) return 0; SetBitValue(&KMem.WC[nWordAddr], nBitAddr, nCoilValue); break; case KLCoilTypeLR: if (nCoilAddr >= KLCoilLRCount) return 0; SetBitValue(&KMem.WLR[nWordAddr], nBitAddr, nCoilValue); break; case KLCoilTypeSR: if (nCoilAddr >= KLCoilSRCount) return 0; SetBitValue(&KMem.WSR[nWordAddr], nBitAddr, nCoilValue); break; } if (m_bOnline) { // MyKLink1.WriteBit(1, nCoilType, nCoilAddr, nCoilValue); } return 0; } int CMTerm2Doc::GetVarData(int nDataType, int nDataAddr) { // TODO: 在此处添加实现代码. if (m_bOnline && 0) { unsigned short value[20]; unsigned short svalue = 0; if (nDataType == KLDataTypeDEC || nDataType == KLDataTypeHEX){ return nDataAddr; } MyKLink1.ReadDataWord(1, 2, nDataType, nDataAddr, value);// (unsigned char *)&KMem.DT[nDataAddr]); svalue = value[0]; return svalue; } switch (nDataType) { case KLDataTypeDEC: case KLDataTypeHEX: return nDataAddr; break; case KLDataTypeWX: if (nDataAddr >= KLDataWXCount) return 0; return KMem.WX[nDataAddr]; break; case KLDataTypeWY: if (nDataAddr >= KLDataWYCount) return 0; return KMem.WY[nDataAddr]; break; case KLDataTypeWR: if (nDataAddr >= KLDataWRCount) return 0; return KMem.WR[nDataAddr]; break; case KLDataTypeWLX: if (nDataAddr >= KLDataWLCount) return 0; return KMem.WLX[nDataAddr]; break; case KLDataTypeWLY: if (nDataAddr >= KLDataWLCount) return 0; return KMem.WLY[nDataAddr]; break; case KLDataTypeDT: if (nDataAddr >= KLDataDTCount) return 0; return (signed short)KMem.DT[nDataAddr]; break; case KLDataTypeSDT: if (nDataAddr >= KLDataSDTCount) return 0; return KMem.SDT[nDataAddr]; break; case KLDataTypeWSR: if (nDataAddr >= KLCoilLRCount) return 0; return KMem.WSR[nDataAddr]; break; case KLDataTypeSV: if (nDataAddr >= KLDataSVCount) return 0; return KMem.SV[nDataAddr]; break; case KLDataTypeEV: if (nDataAddr >= KLDataEVCount) return 0; return KMem.EV[nDataAddr]; break; case KLDataTypeLD: if (nDataAddr >= KLDataLDCount) return 0; return KMem.DT[nDataAddr]; break; case KLDataSysCfg: if (nDataAddr >= KLCoilSRCount) return 0; return KMem.SDT[nDataAddr]; break; case KLDataTypeFlash: if (nDataAddr >= KLCoilSRCount) return 0; return KMem.SDT[nDataAddr]; break; case KLDataTypeTest: if (nDataAddr >= KLCoilSRCount) return 0; return KMem.SDT[nDataAddr]; break; } return 0; } int CMTerm2Doc::SetVarData(int nDataType, int nDataAddr, int nDataValue) { // TODO: 在此处添加实现代码. if (m_bSimulate) { return myKMachine1.SetVarData(nDataType, nDataAddr, nDataValue); }else if (m_bOnline) { // unsigned char value[20]; unsigned short svalue[10]; svalue[0]=nDataValue; int res = 0; res = MyKLink1.WriteDataWord(1, 2, nDataType, nDataAddr, svalue); return res; } switch (nDataType) { // case KLDataTypeDEC: // case KLDataTypeHEX: // break; case KLDataTypeWX: if (nDataAddr >= KLDataWXCount) return 0; KMem.WX[nDataAddr] = nDataValue; break; case KLDataTypeWY: if (nDataAddr >= KLDataWYCount) return 0; KMem.WY[nDataAddr] = nDataValue; break; case KLDataTypeWR: if (nDataAddr >= KLDataWRCount) return 0; KMem.WR[nDataAddr] = nDataValue; break; case KLDataTypeWLX: if (nDataAddr >= KLDataWLCount) return 0; KMem.WLX[nDataAddr] = nDataValue; break; case KLDataTypeWLY: if (nDataAddr >= KLDataWLCount) return 0; KMem.WLY[nDataAddr] = nDataValue; break; case KLDataTypeDT: if (nDataAddr >= KLDataDTCount) return 0; KMem.DT[nDataAddr] = nDataValue; break; case KLDataTypeSDT: if (nDataAddr >= KLDataSDTCount) return 0; KMem.SDT[nDataAddr] = nDataValue; break; case KLDataTypeWSR: if (nDataAddr >= KLCoilLRCount) return 0; KMem.WSR[nDataAddr] = nDataValue; break; case KLDataTypeSV: if (nDataAddr >= KLDataSVCount) return 0; KMem.SV[nDataAddr] = nDataValue; break; case KLDataTypeEV: if (nDataAddr >= KLDataEVCount) return 0; KMem.EV[nDataAddr] = nDataValue; break; case KLDataTypeLD: if (nDataAddr >= KLDataLDCount) return 0; KMem.DT[nDataAddr] = nDataValue; break; case KLDataSysCfg: if (nDataAddr >= KLCoilSRCount) return 0; KMem.SDT[nDataAddr] = nDataValue; break; case KLDataTypeFlash: if (nDataAddr >= KLCoilSRCount) return 0; KMem.SDT[nDataAddr] = nDataValue; break; case KLDataTypeTest: if (nDataAddr >= KLCoilSRCount) return 0; KMem.SDT[nDataAddr] = nDataValue; break; } return 0; } int CMTerm2Doc::AddMonitorCoil(int nCoilType, int nCoilAddr, int nCount) { // TODO: 在此处添加实现代码. return 0; } int CMTerm2Doc::AddMonitorData(int nDataType, int nDataAddr, int nDataCount) { // TODO: 在此处添加实现代码. return 0; } int CMTerm2Doc::ClearMonitor() { // TODO: 在此处添加实现代码. return 0; } int CMTerm2Doc::DoPLCMonitor() { // TODO: 在此处添加实现代码. if (m_bSimulate) { int nDataType = MyKLink1.KLDataTypeWX; int nDataAddr = 0; int nDataCount = 4; int res; for (int i = 0; i < nDataCount; i++) { KMem.WX[i] = myKMachine1.KMem.WX[i]; } nDataType = MyKLink1.KLDataTypeWY; nDataAddr = 0; nDataCount = 4; for (int i = 0; i < nDataCount; i++) { KMem.WY[i] = myKMachine1.KMem.WY[i]; } nDataType = MyKLink1.KLDataTypeWR; nDataAddr = 0; nDataCount = 10; for (int i = 0; i < nDataCount; i++) { KMem.WR[i] = myKMachine1.KMem.WR[i]; } nDataType = MyKLink1.KLDataTypeDT; nDataAddr = 0; nDataCount = 80; for (int i = 0; i < nDataCount; i++) { KMem.DT[i] = myKMachine1.KMem.DT[i]; } nDataType = MyKLink1.KLDataTypeSV; nDataAddr = 0; nDataCount = 40; for (int i = 0; i < nDataCount; i++) { KMem.SV[i] = myKMachine1.KMem.SV[i]; } //res = MyKLink1.ReadDataWord(1, nDataCount, nDataType, nDataAddr, KMem.SV);// (unsigned char *)&KMem.DT[nDataAddr]); nDataType = MyKLink1.KLDataTypeEV; nDataAddr = 0; nDataCount = 40; for (int i = 0; i < nDataCount; i++) { KMem.EV[i] = myKMachine1.KMem.EV[i]; } // res = MyKLink1.ReadDataWord(1, nDataCount, nDataType, nDataAddr, KMem.EV);// (unsigned char *)&KMem.DT[nDataAddr]); }else if (m_bOnline) { int nDataType = MyKLink1.KLDataTypeWX; int nDataAddr = 0; int nDataCount = 4; int res; res = MyKLink1.ReadDataWord(1, nDataCount, nDataType, nDataAddr, KMem.WX);// (unsigned char *)&KMem.DT[nDataAddr]); nDataType = MyKLink1.KLDataTypeWY; nDataAddr = 0; nDataCount = 4; res = MyKLink1.ReadDataWord(1, nDataCount, nDataType, nDataAddr, KMem.WY);// (unsigned char *)&KMem.DT[nDataAddr]); nDataType = MyKLink1.KLDataTypeWR; nDataAddr = 0; nDataCount = 10; res = MyKLink1.ReadDataWord(1, nDataCount, nDataType, nDataAddr, KMem.WR);// (unsigned char *)&KMem.DT[nDataAddr]); nDataType = MyKLink1.KLDataTypeDT; nDataAddr = 0; nDataCount = 80; res = MyKLink1.ReadDataWord(1, nDataCount, nDataType, nDataAddr, KMem.DT);// (unsigned char *)&KMem.DT[nDataAddr]); nDataType = MyKLink1.KLDataTypeSV; nDataAddr = 0; nDataCount = 40; res = MyKLink1.ReadDataWord(1, nDataCount, nDataType, nDataAddr, KMem.SV);// (unsigned char *)&KMem.DT[nDataAddr]); nDataType = MyKLink1.KLDataTypeEV; nDataAddr = 0; nDataCount = 40; res = MyKLink1.ReadDataWord(1, nDataCount, nDataType, nDataAddr, KMem.EV);// (unsigned char *)&KMem.DT[nDataAddr]); } return 0; } void CMTerm2Doc::OnTimer(UINT_PTR nIDEvent) { if (m_bSimulate && m_bPlcRunning) { //ProcessPLCProg(Progs, m_nProgSteps); myKMachine1.ProcPLC(); // update PLC Stat } }