#pragma once #include "stdafx.h" #include #include #include #include using namespace std::placeholders; namespace DIOS::Dev::Detail::SYNBOX { static const int TIMEOUTVALUE = 100; //using JSONString = std::string; //namespace DevDAP = DIOS::Dev::Detail::DAP; static const int MAX_COMMAND_LEN = 100; static const int msTimeOut_Lock = 500; static int CalChecksum(const char* cCmd, int nCmdSize) { int nSum = 0; for (int i = 0; i < nCmdSize; ++i) { nSum += cCmd[i]; } return nSum % 256; } static bool FormatCmd(char* cCmd, int& nSize) { if (nSize < 1) return false; char temp[MAX_COMMAND_LEN]; for (int i = 0; i < nSize; ++i) { cCmd[i] = toupper(cCmd[i]); } memcpy((void*)(temp), (void*)cCmd, nSize); temp[nSize] = char(0x0d); nSize++; temp[nSize] = char(0x0a); nSize++; //strcpy_s((char *)cCmd,nSize,(char *)temp); memcpy((void*)cCmd, (void*)temp, nSize); return true; } //把指令生产Char和hex字符串 static bool CmdtoString(const char* cCmd, int nCmdSize, std::string& strCmd) { std::string strTemp; strCmd = ""; for (int i = 0; i < nCmdSize; i++) { strTemp = ""; if ((cCmd[i] == '\r') || (cCmd[i] == '\n') || (cCmd[i] == ':')) // strTemp = ""; else strTemp = toupper(cCmd[i]); strCmd += strTemp; } return true; } static std::string GetContentFromString(std::string& strContent, char ch) { std::string strTemp; int nPos = strContent.find(ch); if (nPos == std::string::npos) { strTemp = strContent; strContent.clear(); } else { strTemp = strContent.substr(0, nPos); strContent = strContent.substr(nPos + 1, strContent.length() - nPos - 1); } return strTemp; } static int String2Hex(std::string str) { int base = 16; char* entptr; int nRes = strtol(str.c_str(), &entptr, base); return nRes; } static int ChartoInt(char uc) { int nValue; if ('0' <= uc && uc <= '9') { nValue = uc - '0'; } else if ('A' <= uc && uc <= 'F') { nValue = 10 + uc - 'A'; } else if ('a' <= uc && uc <= 'f') { nValue = 10 + uc - 'a'; } else { nValue = -1; } return nValue; } //串口指令映射表 using cmdFun = std::function ; struct tFrameMapping { enum CMDOverType { OverType_directly = 1, OverType_WaitTime = 2, OverType_WaitACK = 3 }; CMDOverType m_eOverType; //指令结束方式:1.直接退;2.等待一些时间;3.等待ACK bool m_bLogFlag; //设置日志打印 cmdFun m_fFun; //处理函数 int m_nWaitTime; //设置等待时间 HANDLE m_hAckEvent; //设置ACK事件 tFrameMapping() { m_eOverType = OverType_directly; m_bLogFlag = false; m_fFun = NULL; m_nWaitTime = 0; m_hAckEvent = NULL; } tFrameMapping(cmdFun fun, CMDOverType type,bool logFlag,int waitTime = 100) { m_bLogFlag = logFlag; switch (type) { case OverType_directly: { m_eOverType = OverType_directly; m_nWaitTime = 0; m_hAckEvent = NULL; } break; case OverType_WaitTime: { m_eOverType = OverType_WaitTime; m_nWaitTime = waitTime; m_hAckEvent = NULL; } break; case OverType_WaitACK: { m_eOverType = OverType_WaitACK; m_nWaitTime = 0; m_hAckEvent = CreateEvent(NULL, TRUE, FALSE, NULL); } break; default: break; } m_fFun = fun; } ~tFrameMapping() { CloseHandle(m_hAckEvent); m_fFun = NULL; } tFrameMapping& operator =(const tFrameMapping& value) { m_eOverType = value.m_eOverType; m_bLogFlag = value.m_bLogFlag; m_fFun = value.m_fFun; m_nWaitTime = value.m_nWaitTime; m_hAckEvent = CreateEvent(NULL, TRUE, FALSE, NULL); return *this; } void tSetWaitState(const char * CMD = NULL) { switch (m_eOverType) { case OverType_directly: { if (m_bLogFlag) { if (CMD != NULL) mLog::Debug("tSetWaitState:[{$}]Exit directly", CMD); else mLog::Debug("tSetWaitState:Exit directly"); } } break; case OverType_WaitTime: { if (m_bLogFlag) { if (CMD != NULL) mLog::Debug("tSetWaitState:[{$}]wait[{$}]ms", CMD, m_nWaitTime); else mLog::Debug("tSetWaitState:wait[{$}]ms", m_nWaitTime); } Sleep(m_nWaitTime); } break; case OverType_WaitACK: { if (m_bLogFlag) { if (CMD != NULL) mLog::Debug("tSetWaitState:[{$}]wait AckEvent 500ms", CMD); else mLog::Debug("tSetWaitState:wait AckEvent 500ms"); } DWORD reasult = WaitForSingleObject(m_hAckEvent, 500); if (WAIT_OBJECT_0 == reasult) { if (m_bLogFlag) { if (CMD != NULL) mLog::Debug("tSetWaitState:[{$}]wait AckEvent successful",CMD); else mLog::Debug("tSetWaitState:wait AckEvent successful"); } //ResetEvent(m_hAckEvent); } else if (WAIT_TIMEOUT == reasult) { if (m_bLogFlag) { if (CMD != NULL) mLog::Warn("tSetWaitState:[{$}]wait AckEvent timeout", CMD); else mLog::Warn("tSetWaitState:wait AckEvent timeout"); } } } break; default: break; } } void tCheckWaitState(const char* CMD = NULL) { switch (m_eOverType) { case OverType_directly: { if (m_bLogFlag) { if (CMD != NULL) mLog::Debug("tCheckWaitState:[{$}]Exit directly", CMD); else mLog::Debug("tCheckWaitState:Exit directly"); } } break; case OverType_WaitTime: { if (m_bLogFlag) { if (CMD != NULL) mLog::Debug("tCheckWaitState:[{$}]wait ms", CMD); else mLog::Debug("tCheckWaitState:wait ms"); } } break; case OverType_WaitACK: { if (m_bLogFlag) { if (CMD != NULL) mLog::Debug("tCheckWaitState:[{$}]activate AckEvent", CMD); else mLog::Debug("tCheckWaitState:activate AckEvent"); } //SetEvent(m_hAckEvent); PulseEvent(m_hAckEvent); } break; default: break; } } }; static std::map arFrame; //----------------------------------------------------------------------------- // DecodeFrame //----------------------------------------------------------------------------- #define CMD_OVER_FLAG 1 static bool DecodeFrame(const char* strFrame, int length) { char data[3] = { 0 }; strncpy_s(data, strFrame, 2); auto found = arFrame.find(data); if (found == arFrame.end()) { return false; } //found->second.tCheckWaitState(strFrame); found->second.m_fFun(strFrame, length);//第二个参数 不重要 return true; } static bool SetWaitAction(const char* strFrame) { char data[3] = { 0 }; strncpy_s(data, strFrame, 2); auto found = arFrame.find(data); if (found == arFrame.end()) { return false; } found->second.tSetWaitState(strFrame); } }