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

📄 send_msg.c

📁 列车入轨检测
💻 C
字号:
#include "reg52.h"

typedef unsigned char BYTE;
BYTE num_train_up[4];
BYTE num_train_down[4];
BYTE NumRoad;
/////////////////////////////////
//the train info in the train
BYTE num_train[4];
BYTE info_UpOrDown;
////////////////////////////////							 
BYTE SetInfo(BYTE tag_list1,BYTE tag_list2)
{
    BYTE temp,res=0;
    unsigned int state;
	temp=tag_list1&0x20;
	
	if(temp==0x20)  //if it's no1
	{
	   tag_list1&=0xe7;
       tag_list2&=0x26;	   
	   NumRoad=1;
	   state=tag_list1;
	   state=state<<8+tag_list2;
	   switch(state)
	   {
	      case 0xa500:  //state 1
		      res=0x11; //the train is coming
		      break; 
		  case 0xa504:  //state 2
		      res=0x12; //the train will come into ori1
		      break;
		  case 0x2506:  //state 3
		      res=0x13; //the train is coming into station
		       break;
		  case 0x2502:   //state 4
		      res=0x14; //the train is coming into ori1
 		      break;
		  case 0x2302:  //state 5 
		      res=0x15; //the train is coming into ori1
		      break;
		  case 0x2202:  //state 6
		      res=0x16; //the train's tail is coming sattion
		      break;
		  case 0x2200:  //state 7
		      res=0x17; //the train is not pass the tag
		      break;
		  case 0x2000:  //state 8
		      res=0x18; //the train has passed the tag
		      break;
		  default:    //passing
		      res=0x10;  //this is a passing train
		     break;
	   }
	}
	else  //it's no2 or no3
	{
	   temp=tag_list1&0x10;
	   if(temp==0x10)//is no2
	   {
	      NumRoad=2;
		  tag_list1&=0xfe;
          tag_list2&=0x96;
	      state=tag_list1;
		   state=state<<8+tag_list2;
		   switch(state)
		   {
		      case 0xd480:  //state 1
			      res=0x21; //the train is coming
			      break; 
			  case 0xd484:  //state 2
			      res=0x22; //the train will come into ori1
			      break;
			  case 0x5486:  //state 3
			      res=0x23; //the train is coming into station
			       break;
			  case 0x5286:   //state 4
			      res=0x24; //the train is coming into ori1
	 		      break;
			  case 0x5206:  //state 5 
			      res=0x25; //the train is coming into ori1
			      break;
			  case 0x5202:  //state 6
			      res=0x26; //the train's tail is coming sattion
			      break;
			  case 0x5200:  //state 7
			      res=0x27; //the train is not pass the tag
			      break;
			  case 0x5000:  //state 8
			      res=0x28; //the train has passed the tag
			      break;
			  default:    //passing
			      res=0x20;  //this is a passing train
			     break;
		   }
	   }
	   else  //is no3
	   {
	      NumRoad=3;
		  tag_list1&=0xfe;
          tag_list2&=0x4e;	   
		  state=tag_list1;
		   state=state<<8+tag_list2;
		   switch(state)
		   {
		      case 0xcc40:  //state 1
			      res=0x31; //the train is coming
			      break; 
			  case 0xcc44:  //state 2
			      res=0x32; //the train will come into ori1
			      break;
			  case 0x4c46:  //state 3
			      res=0x33; //the train is coming into station
			       break;
			  case 0x4a46:   //state 4
			      res=0x34; //the train is coming into ori1
	 		      break;
			  case 0x4a06:  //state 5 
			      res=0x35; //the train is coming into ori1
			      break;
			  case 0x4a02:  //state 6
			      res=0x36; //the train's tail is coming sattion
			      break;
			  case 0x4a00:  //state 7
			      res=0x37; //the train is not pass the tag
			      break;
			  case 0x4800:  //state 8
			      res=0x38; //the train has passed the tag
			      break;
			  default:    //passing
			      res=0x30;  //this is a passing train
			     break;
		   }
	   }
	}
	return res;
}
//地面装置发送
void Send_order(bit UpOrDown,BYTE tag_list1,BYTE tag_list2)
{
   int i;
   BYTE sendmsg[20];
   BYTE checkbyte=0;
   /*the byte 0,1 is the sys tag */
   sendmsg[0]=0xff;
   sendmsg[1]=0xff;
   /*the byte 2 is COMAND */
   sendmsg[2]=0x01;

   /* the byte 3 is the lenght of the info_pack,
       it's begin from No3
   */
   sendmsg[3]=7;
   /* the information of the train(is up or down)*/
   if(UpOrDown)
     sendmsg[4]=0x01;
   else
       sendmsg[4]=0x02;
   checkbyte=sendmsg[4];
   
   for(i=0;i<4;i++)
   {
      sendmsg[i+5]=num_train_up[i];
	  checkbyte+=sendmsg[i+5];
   }
  
  sendmsg[9]=SetInfo(tag_list1,tag_list2);
  checkbyte+=sendmsg[9];
  
  sendmsg[10]=checkbyte;

  //SendToCom(sendmsg,11)
}
//crc check
BYTE CrcCheck(BYTE datalist[],int len)
{
   int i;
   BYTE ret,res;
   for (i=4; i<len+4; i++ )
     ret += datalist[i];
  
   if(ret==datalist[len+4])
      res=1;
   else
      res=0;
   return res;
}
//////////////////////////////////////////////////
void CallInfo()
{
   int i;
   BYTE sendmsg[20];
   BYTE checkbyte=0;
   /*the byte 0,1 is the sys tag */
   sendmsg[0]=0xff;
   sendmsg[1]=0xff;
   /*the byte 2 is COMAND */
   sendmsg[2]=0x10;

   /* the byte 3 is the lenght of the info_pack,
       it's begin from No3
   */
   sendmsg[3]=5;
   /* the information of the train(is up or down)*/
   sendmsg[4]=info_UpOrDown;
 
   checkbyte=sendmsg[4];
   
   for(i=0;i<4;i++)
   {
      sendmsg[i+5]=num_train[i];
	  checkbyte+=sendmsg[i+5];
   }
  
 
  //the check byte
  sendmsg[9]=checkbyte;

  //SendToCom(sendmsg,10)
    
}
//the train get the info from station
BYTE GetInfoTrain(BYTE datalist[],int len)
{
   BYTE tag=0;
   BYTE templist[15];
   int i=0,j;
   while(i<len-1)
   {
      //find the syn head
      if((datalist[i]==0xff)&&(datalist[i+1]==0xff))
	  {
	      //if the pack is too short
	      if((i+3>=len)||(datalist[i+3]+4>len))
		  {
              CallInfo();
		      break;
		  }
		  for(j=0;j<datalist[i+3]+5;j++)
             templist[j]=datalist[i+j];
          //if the check is wrong,then return 0
		  if(!CrcCheck(templist,datalist[i+3]))
		     CallInfo();
		  else
		  {
		     //get the info of state of train
             tag=templist[i+8];
		  }
		  i+=datalist[i+3]+5;
	  }
	  i++;
   }
   return tag;
}
/***********************

************************/

void main()
{
  bit a;
  Send_order(a,12,23);
}

⌨️ 快捷键说明

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