zoukankan      html  css  js  c++  java
  • 简单协议解析

     INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)
     {
        /* In order to detect unexpected events during development,
           it is recommended to set a breakpoint on the following instruction.
        */
       u8 RX_Data;
        if(UART1_GetITStatus(UART1_IT_RXNE )!= RESET)  
        {
          /*接收中断*/
         RX_Data =UART1_ReceiveData8();
         if(Rs485_Rx_Finished_Flag==0)
         {
            if(UART1_RX_Count == 0)
            {
              if(RX_Data == 0xFF)
              {
                  UART1_RX_Buffer[UART1_RX_Count++] = RX_Data;
              }
              else
              {
                  UART1_RX_Count = 0;
                  Checksum = 0;
              }
            }
            else if(UART1_RX_Count == 1)
            {
              if(RX_Data == 0xFF)
              {
                   UART1_RX_Buffer[UART1_RX_Count++] = RX_Data;
              }
              else
              {
                   UART1_RX_Count = 0;
                   Checksum = 0;
              }
            }
            else if(UART1_RX_Count == 2)		//模块类型
            {
              if((RX_Data == Module_type_485)||(RX_Data == Module_type_EQ1)||(RX_Data == Module_type_EQ2))
              {
                      UART1_RX_Buffer[UART1_RX_Count++] = RX_Data;
                      Checksum += RX_Data;
              }
              else
              {
                      UART1_RX_Count = 0;
                      
                      Checksum = 0;
              }
            }
            else if(UART1_RX_Count == 3)		//ID号
            {
              if((RX_Data == Module_ID)||(RX_Data == 0xFE))
              {
                      UART1_RX_Buffer[UART1_RX_Count++] = RX_Data;
                      Checksum += RX_Data;
              }
              else
              {
                      UART1_RX_Count = 0;
                      Checksum = 0;
              }			
            }
            else if(UART1_RX_Count == 4)		//长度=数据长度N+2,整包长度= 长度+5。
            {
                UART1_RX_Buffer[UART1_RX_Count++] = RX_Data;
                Length_Rx_485 = RX_Data;
                Checksum += RX_Data;			
            }		
            else if(UART1_RX_Count == 5)		//指令,PING:0x01,READ DATA:0x02,WRITE DATA:0x03,REG WRITE:0x04,ACTION:0x05,RESET:0x06,SYNC WRITE:0x07。
            {
                if((RX_Data == PING)||(RX_Data == READ_DATA)||(RX_Data == WRITE_DATA)||(RX_Data == REG_WRITE)||(RX_Data == ACTION)||(RX_Data == RESET)||(RX_Data == SYNC_WRITE))
                {
                        UART1_RX_Buffer[UART1_RX_Count++] = RX_Data;
                        Checksum += RX_Data;
                }
                else
                {
                        UART1_RX_Count = 0;
                        Checksum = 0;
                }
            } 
            else
            {
                UART1_RX_Buffer[UART1_RX_Count++] = RX_Data;
                if(UART1_RX_Count<(Length_Rx_485 + 5))
                {
                  Checksum += RX_Data;
                }
                
                else if(UART1_RX_Count >= (Length_Rx_485 + 5))
                {
                  if((255-Checksum) == RX_Data)
                  {
                      UART1_RX_Count = 0;
                      Length_Rx_485 = 0;
                      Checksum = 0;
                      Rs485_Rx_Finished_Flag = 1;
                      return;
                  }
                  else
                  {
                      UART1_RX_Count = 0;
                      Length_Rx_485 = 0;                 
                      Checksum = 0;
                      Rs485_Rx_Finished_Flag = 0;
                      return;					
                  }
                } 
            }
         }
        }
     }
    

      

  • 相关阅读:
    牛客网·剑指offer 从尾到头打印链表(JAVA)
    牛客网·剑指offer 替换空格(JAVA)
    简单的用户登录后台程序编写
    牛客网&剑指offer 二维数组中的查找(JAVA)
    洛谷 P1603 斯诺登的密码(JAVA)
    【回溯法】八皇后问题(递归和非递归)
    如何使用SecureCRT让Vim有颜色?
    js 转base64字符串为文件
    springboot 测试类
    oracle 登录、重启服务
  • 原文地址:https://www.cnblogs.com/prayer521/p/7563187.html
Copyright © 2011-2022 走看看