⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abcomms.cpp

📁 用于开发Modbus工业级通信
💻 CPP
📖 第 1 页 / 共 3 页
字号:
   if (!m_useBCCchecks)
   {
	   // send CRC
	   *(WORD*)telegram = crc;
	   telegramLength = 2;
	   Send(telegramLength, telegram, FALSE, NULL);
   }
   else
   {
	   // send a bcc
	   *(BYTE*)telegram = bcc;
	   telegramLength = 1;
	   Send(telegramLength, telegram, FALSE, NULL);
   }
   return(TRUE);
}

// ----------------------------- SetEngineState -----------------------------
void CABCommsProcessor::SetEngineState(__EngineStates stateStep)
{
   switch (stateStep)
   {
   case ENG_STATE_IDLE:    // wait for start
         m_CPUstateEngineStepname = "idle";
         break;
   case ENG_STATE_RECEIVE:   // RX rest of telegram + ETX + CRC
         m_CPUstateEngineStepname = "rx request body";
         break;
   case ENG_STATE_SENDACK:   // sending an ACK
         m_CPUstateEngineStepname = "ack request";
         break;
   case ENG_STATE_RESPOND:   // sending msg response
         m_CPUstateEngineStepname = "respond";
         break;
   case ENG_STATE_FINALACK:   // wait for final ACK
         m_CPUstateEngineStepname = "wait for ack";
         break;
   case ENG_STATE_MASTERIDLE: // master eneded last cycle, will start next transaction without waiting for an RX
         m_CPUstateEngineStepname = "master end-of-cycle";
         m_masterHasWork = TRUE; // skip the RX, and do the DoMaster() again, 
                                 // since we just done one transaction, and want to do another right away
         break;
   default:
         m_CPUstateEngineStepname = "unknown!!!";
         break;
   }
   m_CPUstateEngineStep = stateStep;
}



// ------------------------- GetNextTransactionID ----------------------------
WORD CABCommsProcessor::GetNextTransactionID(BYTE station)
{
   return (++m_masterTN[station]);
}

// -------------------------- GetCurrentTransactionID -------------------------
WORD CABCommsProcessor::GetCurrentTransactionID(BYTE station)
{
   return (m_masterTN[station]);
}

// -------------------------- SetCurrentTransactionID -------------------------
WORD CABCommsProcessor::SetCurrentTransactionID(BYTE station, WORD id)
{
WORD last = m_masterTN[station];
   m_masterTN[station] = id;
   return (last);
}

// ------------------------------ RSDataDebugger ------------------------------
void CABCommsProcessor::RSDataDebugger(const BYTE * buffer, LONG length, int transmit)
{
CString debuggerString;
BYTE *data;
byte hiNib,loNib;

   //convert BIN-ary to ASCII for display
   data = new BYTE[(length*2)+1];
   for (int i = 0; i < length; i++)
   {
       hiNib = ( *(buffer+i) >>4) & 0x0f;
       loNib = ( *(buffer+i)    ) & 0x0f;
       data[(i*2)]   = ( (hiNib < 0x0A) ? ('0' + hiNib) : ('A' + hiNib-10) );
       data[(i*2)+1] = ( (loNib < 0x0A) ? ('0' + loNib) : ('A' + loNib-10) );
   }

   data[(length*2)] = '\0';

   if (transmit)
   {
      m_lastTX = data;
      if (length)
         pGlobalDialog->OnCharactersSent();
   }
   else
   {
      if (length)
         m_lastRX = data;
      if (length)
         pGlobalDialog->OnCharactersReceived();
   }

//   m_DiagnosticsFile.OutputScopeText((char*)data, (transmit ? FOREGROUND_IYELLOW: FOREGROUND_ICYAN));
   //call parent
   CCommsProcessor::RSDataDebugger(buffer, length, transmit);

   delete (data);
} // RSDataDebugger

// ------------------------------------------ Scentence ----------------------------
CString &Scentence(LPCTSTR text, BOOL startScentence)
{
static CString scentence;

   scentence = text;
   if (startScentence)
   {
      if (islower(text[0]))
         scentence.SetAt(0, toupper(text[0]));
   }
   else
   {
      if (isupper(text[0]))
         scentence.SetAt(0, tolower(text[0]));
   }
   return(scentence);
}

// ------------------------------------------- Get232ErrorDisc ----------------------
//
CString &Get232ErrorDisc(DWORD commError)
{
DWORD mask = 0x01, index;
BOOL and = FALSE;
static CString description;
#define AND    " and ";

   description.Format ("x%X ", commError);
   for (index = 0 ; index < 16; index++)
   {
      switch (commError & mask)
      {                                                         
         case CE_BREAK    :    // The hardware detected a break condition.
            if (and)
               description += AND;
            description += Scentence("The hardware detected a break condition", !and);
            and = TRUE;
            break;
         case CE_FRAME    :    // The hardware detected a framing error.
            if (and)
               description += AND;
            description += Scentence("The hardware detected a framing error", !and);
            and = TRUE;
            break;
         case CE_IOE      :    // An I/O error occurred during communications with the device.
            if (and)
               description += AND;
            description += Scentence("An I/O error occurred during communications with the device", !and);
            and = TRUE;
            break;
         case CE_MODE     :    // The requested mode is not supported, or the hCommDev parameter is
                               // invalid. If this bit set, this is the only valid error.
            if (and)
               description += AND;
            description += Scentence("The requested mode is not supported", !and);
            and = TRUE;
            break;

         case CE_OVERRUN  :    // A character-buffer overrun has occurred. The next character is lost.
            if (and)
               description += AND;
            description += Scentence("A character-buffer overrun has occurred", !and);
            and = TRUE;
            break;
         case CE_RXOVER   :    // An input buffer overflow has occurred. There is either no room in the input
                               // buffer, or a character was received after the end-of-file (EOF) character
                               //  was received.
            if (and)
               description += AND;
            description += Scentence("An input buffer overflow has occurred", !and);
            and = TRUE;
            break;
         case CE_RXPARITY :    // The hardware detected a parity error.
            if (and)
               description += AND;
            description += Scentence("The hardware detected a parity error", !and);
            and = TRUE;
            break;
         case CE_TXFULL   :    // The application tried to transmit a character, but the output buffer was full.
            if (and)
               description += AND;
            description += Scentence("Transmision of a character failed, output buffer full", !and);
            and = TRUE;
            break;
         default          :
            break;
      }
      //next bit
      mask = mask << 1;
   }
   description += ".";
   return(description);
}

// --------------------------------- OnHWError ---------------------------------
void CABCommsProcessor::OnHWError(DWORD dwCommError)
{
   // restart interpreter
   if (0 == dwCommError)
   {
      RSDataMessage("COMM IDLE: Restarting interpretation.");
   }
   else
   {
      RSDataMessage(Get232ErrorDisc(dwCommError));
   }
   m_noiseLength = 0;
   // TODO! if recieving a response, and it is corrupted in this way, 
   // send a DLE-ENQ and stay on this step for 3 retries
   if (m_CPUstateEngineStep != ENG_STATE_IDLE)
   {
      if (dwCommError)
         Send(2, txDLE_ENQ, FALSE, NULL); // send a ??? out

      SetEngineState(ENG_STATE_IDLE);
   }
}

// ------------------------------- RSStateChanged -----------------------
void CABCommsProcessor::RSStateChanged(DWORD state)
{
   EnterCriticalSection(&stateCS);
   if (NULL==pGlobalDialog)
      return;
   pGlobalDialog->m_ServerRSState = state;
   LeaveCriticalSection(&stateCS);
} // RSStateChanged

// ------------------------------ RSDataMessage ------------------------------
void CABCommsProcessor::RSDataMessage(LPCTSTR msg)
{
CString message;
   EnterCriticalSection(&stateCS);
   message = "##";
   message += msg;
   //OutputDebugString("##");
   OutputDebugString(message);
   OutputDebugString("\n");
   if (NULL!=pGlobalDialog)
      pGlobalDialog->AddCommsDebugString(message);
   LeaveCriticalSection(&stateCS);
}

// ------------------------------ GetRALength --------------------------------
// Return the # bytes still to read
LONG CABCommsProcessor::GetRALength()
{
WORD minFrameLength = 18;

   if (this->m_useBCCchecks)
      minFrameLength --;

   switch (m_CPUstateEngineStep)
   {
      case ENG_STATE_IDLE:
      case ENG_STATE_FINALACK:
         if ((m_noiseLength==1) && (m_noiseBuffer[0] == ALLENBRADLEY_DLE))
            return(1);
          return(2);
          break;
      case ENG_STATE_RECEIVE:
         {
         LONG length=1;
            if (m_noiseLength < minFrameLength)
               length = minFrameLength-m_noiseLength;
            if ((m_noiseLength > AB_LENGTHFRAMEOFF) && 
                ((m_noiseBuffer[AB_COMMANDBYTEOFF]== ALLENBRADLEY_SLC_CMD) &&
                 (m_noiseBuffer[AB_FUNCBYTEOFF]== ALLENBRADLEY_WORD_WRITE))
               )
            {
            DWORD correctDataLen;
               // write data function, read up to # registers sent
               // calculate minimum length for a 4-byte address, the data and the ETX and CRC
               correctDataLen = m_noiseBuffer[AB_LENGTHFRAMEOFF] + 4 + (AB_LENGTHFRAMEOFF+1) + 4;
               length = correctDataLen-m_noiseLength;
               if (length <=0)
                  length=1;
            }
            return (length);
         }
         break;
   }
   return(1);
}


// --------------------------------- ProcessData -----------------------------
// Interpret MODBUS request pBuffer, and respond to it.
//
BOOL CABCommsProcessor::OnProcessData(const CHAR *pBuffer, DWORD numBytes, BOOL *discardData)
{
int   i=0;
WORD  guardword1=1;
WORD  guardword2=2;
WORD minFrameLength = 18;
WORD minRESP_FrameLength = 12;

   if (m_useBCCchecks)
   {
      minFrameLength--;
      minRESP_FrameLength--;
   }

   // build noise telegram
   if (numBytes)
   { //append recieved bytes to the noise telegram
      if (m_noiseLength + numBytes >= sizeof(m_noiseBuffer))
      {
         RSDataMessage("OVERFLOW:Restarting interpretation.");

         m_noiseLength = 0;
         SetEngineState(ENG_STATE_IDLE);
         return(TRUE);
      }
      // append bytes to noise telegram
      memcpy(&m_noiseBuffer[m_noiseLength], pBuffer, numBytes);
      m_noiseLength += numBytes;
      *discardData = TRUE;
   }
   //else
      Sleep(0);
   
//   ASSERT((m_noiseLength<50) || (0==numBytes));
   if (m_masterBusy && MasterTimedOut())
   { // SEND message timed out
      m_noiseLength = 0;
      SetEngineState(ENG_STATE_IDLE);
   }

   switch (m_CPUstateEngineStep)
   {
      case ENG_STATE_MASTERIDLE :
         SetEngineState(ENG_STATE_IDLE);
      case ENG_STATE_IDLE :
         m_messageNAKs = 0;
         if (IsMaster())
         {
            // PLC master may initiate a transaction now
            DoMaster();
            if ((m_masterBusy && (!m_masterWaiting)))
            {
               return(FALSE);
            }
            // otherwize, master is idle, Or waiting for response, continue.
         }
         if (m_noiseLength >= 2)
         {
            if (( m_noiseBuffer[0] == ALLENBRADLEY_DLE)&&
                (m_noiseBuffer[1] == ALLENBRADLEY_STX ))
            {
               RSDataMessage("DLE-STX recieved");
               SetEngineState(ENG_STATE_RECEIVE);
               if (m_noiseLength > 2)
                  return(OnProcessData((char*)m_noiseBuffer, 0, discardData));
               return TRUE;
            }
            // strip the leading char from the noise buffer
            m_noiseLength--;
            memmove(m_noiseBuffer, &m_noiseBuffer[1], m_noiseLength);
            // recurse to keep looking at the buffer untill we get a match or an empty buffer
            if (m_noiseLength >= 2)
            {
               return(OnProcessData((char*)m_noiseBuffer, 0, discardData));
            }
            return(TRUE);
         }
         //wait for more data
         return(FALSE);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -