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

📄 ppi_protocol.cpp

📁 部分破解西门子PPI通信协议的测试程序
💻 CPP
字号:
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "string.h"
#include "dos.h"
#include "PPI.H"


#define  TRUE  1
#define  FALSE 0
#define  COUNT 5

extern unsigned char ReadByte(unsigned char *);
extern unsigned char SendByte(unsigned char );
extern unsigned char SendStr(unsigned char *mess,int n);
extern unsigned char ReceiveStr(unsigned char *buff,int n);

unsigned char FCS(unsigned char *,unsigned short int);
unsigned int  Package(unsigned int ,Param ,long int );
unsigned char communication(unsigned char *,unsigned int,unsigned int,long int *);
void Comm_Reset(void);
unsigned char PPI_Protocol(unsigned char StationNO,V_M Cell,\
			   Param ParaM,long int *Value);

unsigned char Reset_CMD[]={0x10,0x02,0x00,0x49,0x4B,0x16,0xDC,0x00,0x00};
unsigned char RW_CMD[]={0x68,0x23,0x23,0x68,0x02,0x00,0x5C,0x32,0x01,\
			0x00,0x00,0x00,0x00,0x00,0x0E,0x00,0x08,0x05,\
			0x01,0x12,0x0A,0x10,0x06,0x00,0x01,0x00,0x01,\
			0x84,0x00,0x01,0x90,0x00,0x04,0x00,0x20,0x00,\
			0x00,0x07,0xD0,0xF1,0x16};
unsigned char RW_OK[]={0x10,0x02,0x00,0x7C,0x7E,0x16};
unsigned char RW_Result[31];
unsigned char PPI_Protocol(unsigned char StationNO,V_M Cell,\
			   Param ParaM,long int *Value)
{
   unsigned int length;
   unsigned char flag;

   RW_CMD[4]=StationNO;
   RW_OK[1]=StationNO;
   Reset_CMD[1]=StationNO;

   switch(ParaM.Op_Type)   /*  start check data type. */
   {
     case      Read:
		   {
		      ParaM.Op_Type=Read;
		      length=Package(Cell.v,ParaM,*Value);

		      flag=communication(RW_CMD,33,length,Value);
		      if(!flag)
			    return FALSE;		      
		      break;
		   }
     case     Write:
		   {
		      ParaM.Op_Type=Write;
		      length=Package(Cell.v,ParaM,*Value);
		      
              flag=communication(RW_CMD,length,24,Value);
		      if(!flag)
			    return FALSE;		      
		      break;
		   }
     case     SetOn:
		   {
		      ParaM.Op_Type=Write;
		      length=Package(Cell.v,ParaM,0x01);

              flag=communication(RW_CMD,length,24,Value);
		      if(!flag)
			    return FALSE;		      
		      break;
		   }
     case    ClrOff:
		   {
		      ParaM.Op_Type=Write;
		      length=Package(Cell.v,ParaM,0x00);

              flag=communication(RW_CMD,length,24,Value);      
              if(!flag)
			    return FALSE;		      
		      break;
		   }
     case  MomentOn:
		   {
		      ParaM.Op_Type=Write;
		      length=length=Package(Cell.v,ParaM,0x01);

		      flag=communication(RW_CMD,length,24,Value);
              if(!flag)
			    return FALSE;

		      delay(50);
		      length=Package(Cell.v,ParaM,0x00);

		      flag=communication(RW_CMD,length,24,Value);
              if(!flag)
			    return FALSE;		      
		      break;
		   }
     case    Invert:
		   {
		      ParaM.Op_Type=Read;
		      length=Package(Cell.v,ParaM,*Value);

		      flag=communication(RW_CMD,33,length,Value);
              if(!flag)
			    return FALSE;

		      delay(50);
		      ParaM.Op_Type=Write;
		      length=Package(Cell.v,ParaM,*Value^0x01);

		      flag=communication(RW_CMD,length,24,Value);
              if(!flag)
                return FALSE;		      
		     break;
		   }
     default :return FALSE;
	     // break;
   }                       /*  end check data type. */
   return  TRUE;
}

unsigned char FCS(unsigned char *Receive,unsigned short int length)
{
   unsigned char SUM;
   int i;

   SUM=*Receive;
   for(i=1;i<length;i++)
    SUM+=*(Receive+i);

   return  SUM;
}

unsigned int  Package(unsigned int cell,Param ParaM,long int value)
{
  unsigned int RT_Length;
  ChaiZhuang Value;
  V_M  MCell;

  Value.number=value;
  MCell.v=cell;
  switch(ParaM.Data_Type)
  {//1
    case  0:
	     {//2
	      RW_CMD[26]=0x01;        /*  cell type */
	      RW_CMD[27]=0x84;
	      RW_CMD[29]=MCell.v*8/256;  /*  address */
	      RW_CMD[30]=MCell.v*8%256;

	      switch(ParaM.Op_Type)
	      {
	       case  Read:
			{
			 RW_CMD[1]=0x1B;   /* LE */
			 RW_CMD[2]=0x1B;   /* LER */
			 RW_CMD[16]=0x00;  /* different between R/W */
			 RW_CMD[17]=0x04;
			 RW_CMD[32]=0x16;  /* ED */
				     switch(ParaM.Byte_Num)
			 {
			   case    1:
				    {
				      RW_CMD[22]=0x02;    //B/W/D?
				      RT_Length=28;
				      break;
				    }
			   case    2:
				    {
				      RW_CMD[22]=0x04;    //B/W/D?
				      RT_Length=29;
				      break;
				    }
			   case    4:
				    {
				      RW_CMD[22]=0x06;    //B/W/D?
				      RT_Length=31;
				      break;
				    }
			   default  : break;
			 }
			 RW_CMD[31]=FCS(RW_CMD+4,27);
			 break;
			}
	       case Write:
			{
			 RW_CMD[17]=0x05;
			 RW_CMD[31]=0x00;
			 RW_CMD[32]=0x04;
				     switch(ParaM.Byte_Num)
						 {
			   case    1:
				    {
				      RW_CMD[1]=0x20;   /* LE */
				      RW_CMD[2]=0x20;   /* LER */
				      RW_CMD[16]=0x05;
				      RW_CMD[22]=0x02;
				      RW_CMD[33]=0x00;
				      RW_CMD[34]=0x08;
				      RW_CMD[37]=0x16;  /* ED */

				      RW_CMD[35]=Value.ch[0];

				      RT_Length=38;
				      break;
				    }
			   case    2:
				    {
				      RW_CMD[1]=0x21;   /* LE */
				      RW_CMD[2]=0x21;   /* LER */
				      RW_CMD[16]=0x06;
				      RW_CMD[22]=0x04;
				      RW_CMD[33]=0x00;
				      RW_CMD[34]=0x10;
				      RW_CMD[38]=0x16;  /* ED */

				      RW_CMD[35]=Value.ch[1];
				      RW_CMD[36]=Value.ch[0];

				      RT_Length=39;
				      break;
				    }
			   case    4:
				    {
				      RW_CMD[1]=0x23;   /* LE */
				      RW_CMD[2]=0x23;   /* LER */
				      RW_CMD[16]=0x08;
				      RW_CMD[22]=0x06;
				      RW_CMD[33]=0x00;
				      RW_CMD[34]=0x20;
				      RW_CMD[40]=0x16;  /* ED */

				      RW_CMD[35]=Value.ch[3];
				      RW_CMD[36]=Value.ch[2];
				      RW_CMD[37]=Value.ch[1];
				      RW_CMD[38]=Value.ch[0];

				      RT_Length=41;
				      break;
				    }
			   default  : break;
						 }
			   RW_CMD[RT_Length-2]=FCS(RW_CMD+4,RT_Length-6);
			   break;
			}
		  default   :  break;
	      }
	      break;
	     }//2
    case  1:
	     {//3
	      RW_CMD[26]=0x00;        /*  cell type */
	      RW_CMD[27]=0x83;

	      switch(ParaM.Op_Type)
	      {
	       case  Read:
			 {
			  RW_CMD[1]=0x1B;   /* LE */
			  RW_CMD[2]=0x1B;   /* LER */
			  RW_CMD[16]=0x00;  /* different between R/W */
			  RW_CMD[17]=0x04;
			  RW_CMD[32]=0x16;  /* ED */

				      switch(ParaM.Byte_Num)
			  {
			    case  0:
				   {
				     RW_CMD[22]=0x01;     //Bit/Byte?

						     RW_CMD[29]=(MCell.m.M_byte*8+MCell.m.M_bit)/256;   /*  address */
						     RW_CMD[30]=(MCell.m.M_byte*8+MCell.m.M_bit)%256;
				     RT_Length=28;
				     break;
				   }
			    case  1:
				   {
				     RW_CMD[22]=0x02;     //Bit/Byte?

						     RW_CMD[29]=MCell.m.M_byte*8/256;                 /*  address */
						     RW_CMD[30]=MCell.m.M_byte*8%256;
				     RT_Length=28;
				     break;
				   }
			    default: break;
			  }
			  RW_CMD[31]=FCS(RW_CMD+4,27);
			  break;
			 }
	       case Write:
			 {
			   RW_CMD[1]=0x20;   /* LE */
			   RW_CMD[2]=0x20;   /* LER */
			   RW_CMD[16]=0x05;
			   RW_CMD[17]=0x05;
			   RW_CMD[37]=0x16;
				       RT_Length=38;

				       switch(ParaM.Byte_Num)
			   {
			    case  0:
				   {
				     RW_CMD[22]=0x01;     //Bit/Byte?
						     RW_CMD[29]=(MCell.m.M_byte*8+MCell.m.M_bit)/256;   /*  address */
						     RW_CMD[30]=(MCell.m.M_byte*8+MCell.m.M_bit)%256;
				     RW_CMD[31]=0x00;
				     RW_CMD[32]=0x03;
				     RW_CMD[33]=0x00;
				     RW_CMD[34]=0x01;

				     RW_CMD[35]=Value.ch[0];

				     break;
				   }
			    case  1:
				   {
				     RW_CMD[22]=0x02;     //Bit/Byte?
						     RW_CMD[29]=MCell.m.M_byte*8/256;   /*  address */
						     RW_CMD[30]=MCell.m.M_byte*8%256;
				     RW_CMD[31]=0x00;
				     RW_CMD[32]=0x04;
				     RW_CMD[33]=0x00;
				     RW_CMD[34]=0x08;

				     RW_CMD[35]=Value.ch[0];

				     break;
				   }
			    default: break;
			   }
			   RW_CMD[RT_Length-2]=FCS(RW_CMD+4,RT_Length-6);
			   break;
			 }
	       default : break;
			  }
	      }//3
  default : break;
  }//1
  return  RT_Length;
}

unsigned char communication(unsigned char *RW_CMD,unsigned int T_Length,\
				unsigned int R_Length,long int *Value)
{
  unsigned char character;
  int i,count=0;
  ChaiZhuang R_Value;

  R_Value.number=0;

  do   //communication and meet communication failure to repeat. 
  {
    SendStr(RW_CMD,T_Length);      // transmit the require frame
    ReadByte(&character);
    if(character!=0xE5)  // receive the respond frame
      {
	printf("\n Meet error!\n");
	Comm_Reset();
       //	delay(1000);
	continue;
      }
    delay(50);
    SendStr(RW_OK,6);             // transmit the OK  CMD
  
    character=ReceiveStr(RW_Result,R_Length); 
    if(!character) //  whether the receive is normal
      {
       printf("\n Meet error!\n"); 
       Comm_Reset();
       delay(500);
       continue;
      }
  
    if(*(RW_Result+21)!=0xFF)          // whether the require is normal executed
      {
       printf("\n Meet error!\n");
       Comm_Reset();
       delay(500);
       continue;
      }
    if((*(RW_Result+1)!=R_Length-6)||(*(RW_Result+2)!=R_Length-6))
      {
       printf("\n Meet error!\n");	  
       Comm_Reset();
       delay(500);
       continue;
      }
    if(*(RW_Result+R_Length-2)!=FCS(RW_Result+4,R_Length-6))   // whether the communication link is normal
      {
       printf("\n Meet error!\n");
       Comm_Reset();
       delay(500);
       continue;
      }
    break;
  }while(++count<COUNT);   //communication and meet communication failure to repeat.
  
  if(count==COUNT)             // whether reach the max repeat count
    {
     Comm_Reset();
     printf("\nFailure!\n");
     return  FALSE;
    }

  for(i=0;i<=int(R_Length-28);i++)  // when communication and respond is normal,get data
    R_Value.ch[R_Length-28-i]=*(RW_Result+25+i);   // assemble the data

  *Value=R_Value.number;       // put the data to preset cell

  return  TRUE;
}

void Comm_Reset(void)
{
  SendStr(Reset_CMD,6);
  ReceiveStr(RW_Result,6);
  delay(100);
  SendStr(Reset_CMD+6,3);
}

⌨️ 快捷键说明

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