gusucode.com > 通用协议编解码模块C#源码程序 > 通用协议编解码模块/codec_src/codec_test/codec_test/Test/CodecTest/MsgBlockTest.cpp
// MsgBlockTest.cpp: implementation of the MsgBlockTest class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "CodecTest.h" #include "MsgBlockTest.h" #include "conio.h" #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif using namespace std; int BcdToAsc(char *szAsc, const BYTE *szBcd, LONG nBcdLen, BOOL bOdd) { LONG n = 0; nBcdLen = bOdd ? (nBcdLen * 2) : (nBcdLen * 2 - 1); for (n = 0; n < nBcdLen; n ++) { if (n % 2 == 0) szAsc[n] = (szBcd[n/2] & 0xF) + '0'; else szAsc[n] = ((szBcd[n/2] >> 4) & 0xF) + '0'; } szAsc[n] = '\0'; return n; } void MsgBlockTest() { // ICodec *pCodec = CodecServ::CreateBlock("Root"); ICodec *pCodec = g_CreateBlock("Root"); if (pCodec) { if (pCodec->CreateMsg() == CDC_OK) { IMsgNode *pNode = NULL; pCodec->GetInterface(IN_IMsgNode, (void**)&pNode); MsgNodeTest(pNode); while (TRUE) { cout << "Press any key to perform a Access Test, <Esc> to quit." << endl; int ch = ::getch(); if (ch == 0x1B) break; MsgAccessTest(pNode); } cout << "Quit!" << endl; } else { cerr << "Error: CreateMsg Failed!" << endl; } pCodec->DestroyInstance(); } else { cerr << "Error: CreateBlock Failded!" << endl; } } void MsgNodeTest(IMsgNode *pNode) { if (pNode) { const BYTE *pValue = NULL; LONG nLen = 0; DWORD nValue = 0; IEnumTable* pEnumTable = NULL; CString strValue; CString strLen; CString strName = pNode->GetNodeInfo()->GetNodeName(); CString strType; CString strPosition; switch (pNode->GetNodeInfo()->GetNodeType()) { case (mntNumber): strType = "Number"; break; case (mntOctet): strType = "Octet String"; break; case (mntASCII): strType = "ASCII String"; break; case (mntBCD): strType = "BCD String"; break; default: strType = "Unknown"; } strPosition.Format("%d:%d", pNode->GetNodeInfo()->GetNodeByteStart(), pNode->GetNodeInfo()->GetNodeBitStart()); int nNameStart = 0; for (nNameStart = 0; nNameStart < strName.GetLength(); nNameStart++) { if ((strName[nNameStart] >= 'A') && (strName[nNameStart] <= 'Z')) break; } cout << GetIndent() << "Node Name: " << ((LPCTSTR)strName/*+nNameStart*/) << "(" << (LPCTSTR)strType << ")" << endl; cout << GetIndent() << "Node Position: " << (LPCTSTR)strPosition << ";"; switch (pNode->GetNodeInfo()->GetNodeType()) { case (mntNumber): pNode->GetNodeValue(nValue); if (pNode->GetNodeInfo()->GetNodeLength(mluBit) > 24) strValue.Format("0x%08X", nValue); else if (pNode->GetNodeInfo()->GetNodeLength(mluBit) > 16) strValue.Format("0x%06X", nValue); else if (pNode->GetNodeInfo()->GetNodeLength(mluBit) > 8) strValue.Format("0x%04X", nValue); else strValue.Format("0x%02X", nValue); pEnumTable = pNode->GetNodeInfo()->GetEnumTable(); if (pEnumTable) { LPCTSTR szEnum = pEnumTable->GetEnumName(pEnumTable->FindEnum(nValue)); if (! szEnum) szEnum = "Unknown"; strValue.Format("%s(%s)", strValue, szEnum); } strLen.Format("%d Bit", pNode->GetNodeInfo()->GetNodeLength(mluBit)); break; case (mntBCD): pNode->GetNodeValue(&pValue, nLen); { char szASC[100]; BcdToAsc(szASC, pValue, nLen, TRUE); strValue = "\"" + CString(szASC) + "\""; strLen.Format("%d Byte", pNode->GetNodeInfo()->GetNodeLength(mluByte)); } break; case (mntOctet): pNode->GetNodeValue(&pValue, nLen); { LONG n; CString str; for (n = 0; n < nLen; n ++) { str.Format("%02X", pValue[n]); if (n == 0) strValue = str; else strValue = strValue + " " + str; } } strValue = "\"" + strValue + "\""; strLen.Format("%d Byte", pNode->GetNodeInfo()->GetNodeLength(mluByte)); break; case (mntASCII): pNode->GetNodeValue(&pValue, nLen); strValue = "\"" + CString((LPCTSTR)pValue, nLen) + "\""; strLen.Format("%d Byte", pNode->GetNodeInfo()->GetNodeLength(mluByte)); break; default: ASSERT(FALSE); break; } cout << (LPCTSTR)strLen << endl; cout << GetIndent() << "Node Value: " << (LPCTSTR)strValue << endl; if (pNode->GetChildCount() > 0) { cout << GetIndent() << "Children Count: " << pNode->GetChildCount() << endl << endl; IncreaseIndent(); COLPOS nPos = pNode->FindChild(0L); while (nPos) { MsgNodeTest(pNode->GetChildAt(nPos)); nPos = pNode->FindNextChild(nPos); } DecreaseIndent(); } cout << endl; } } void MsgAccessTest(IMsgNode *pNode) { TCHAR szLocator[201]; cout << GetIndent() << "Input Node Locator: "; cout.flush(); scanf("%s", szLocator); TCHAR szValue[201]; IMsgNode *pTarget = pNode->LocateNode(szLocator); if (pTarget) { cout << "Press 'Y' to Change the Value, else to View it." << endl; int ch = ::getch(); if ((ch == 'y') || (ch == 'Y')) { cout << GetIndent() << "Input New Value: "; cout.flush(); scanf("%s", szValue); switch (pTarget->GetNodeInfo()->GetNodeType()) { case (mntNumber): { DWORD nValue = ::atol(szValue); pTarget->SetNodeValue(nValue); } break; case (mntOctet): case (mntASCII): case (mntBCD): pTarget->SetNodeValue((BYTE*)szValue, ::strlen(szValue)); break; default: ASSERT(FALSE); break; } } MsgNodeTest(pTarget); } else { cout << GetIndent() << "Target Not Found!" << endl; } }