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

📄 senddatagramv3.0.c

📁 这是一个linux下基于MiniGUI的手机界面设计源码
💻 C
📖 第 1 页 / 共 3 页
字号:
#ifndef _SENDDATAGRAM_C_#define _SENDDATAGRAM_C_ 0#include "../DataProcessing/MySHM.h"//#include "../DataProcessing/Wrapper.c"//#include "MainServ.c"
long int Hex[10];

int ToBCDChar(char c)
{
  if((c>='0')&&(c<='9'))
	  return c-0x30;
  else if((c>='a')&&(c<='z'))
      return c-'a'+97;
  else if((c>='A')&&(c<='Z'))
      return c-'A'+65;
}

int ToHex(char c)
{
  if((c>='0')&&(c<='9'))
	  return c-0x30;
  else if((c>='a')&&(c<='z'))
      return c-'a'+10;
  else if((c>='A')&&(c<='Z'))
    return c-'A'+10;
}


/*Reset each element of the array to -1 to initial it*/
void Reset( void )
{
   int i;

   for(i=9;i>=0;i--)
   {
		Hex[i]=-1;
   }

}

/*Convert a decimal number to a hex number character*/
 void  DecimalToHex(long int  n,int i)
{
	Hex[i]=(n%16);
	n=(n/16);
    if(n>15)
    {
		DecimalToHex(n,i+1);
    }
    else
    {
		Hex[i+1]=n;
	}

}

void ToHexChar(unsigned char **HexStr,int StrLen)
{
	char str[10];
    int i,len,rlen,t,t1,j=0,count=0;

	rlen=StrLen-1;
    *HexStr=(unsigned char *)malloc(sizeof(unsigned char) * StrLen );

	 for(i=0;i<StrLen;i++)
		(*HexStr)[i]= 0;

	for(i=9;i>=0;i--)
    {
		if(Hex[i]>=0 && Hex[i]<=9)
	    {
			str[j++]=Hex[i]+'0';
	        printf("%c\n",str[j-1]);
	    }
		else if(Hex[i]>=10 && Hex[i]<=15)
		{
			str[j++]='A'+(Hex[i]-10);
			printf("%c\n",str[j-1]);
		}
	}
	str[j]='\0';



    len=strlen(str);

	for(i=0;i<len;i++)
		printf("%c ",str[i]);

	if((len%2)!=0)
	{
		for(i=len-1;i>=0;i--)
		{
			char l,h=str[i];

			if((++count)== len)
			{
				t=ToHex(h) ;
				(*HexStr)[rlen]=t;
				printf("%X ",(*HexStr)[rlen]);
				rlen--;
			}
			else
			{
				l=str[--i];
				++count;
				t=ToHex(h);
				t1=ToHex(l);
				t=t+t1*16;
				(*HexStr)[rlen]=t;
				printf("%d %X ",(*HexStr)[rlen],t);
				rlen--;
			}

		}

	}
    else
    {

		for(i=len-1;i>=0;i--)
		{
			char l,h=str[i];

			l=str[--i];
			t=ToHex(h) ;
            t1=ToHex(l) ;
			t=t+t1*16;
			(*HexStr)[rlen--]=t;
            printf("%X ",(*HexStr)[rlen+1]);

		}
    }

	if(rlen== 0)
		(*HexStr)[0]=0;
    else if(rlen== 1)
		(*HexStr)[1]=(*HexStr)[0]=0;

    printf("\n");

    for(i=0;i<StrLen;i++)
		printf("%X ",(*HexStr)[i]);

    printf("\n");


}


/*******Function Returns the number of the content's bytes************************/
int convert( char *str,unsigned char **content,int codeType)
{

	int t,t1,i,j;
	int rlen=0,len1,len2;
	unsigned char chBuffer[MAXSIZE]="";
   
	if(str == NULL )
		return;
	else if( (len1=strlen(str))> 210)
	{
		printf("The length of data can't exceed 210 bytes!");
        	return;

	}

	*content=( unsigned char *)malloc( sizeof(unsigned char) * len1 );
 		
	if(codeType== CCHAR)/* The code type is chinese character*/
	{
		for(i=0;i<len1;i++)
		{
			if(str[i] & 0x80)
       		{
				strncat(chBuffer,str+i,2);
				i++;
			}

		}
		len2=strlen(chBuffer);

		for(j=0;j<len2;j++)
		{
			(*content)[rlen]=chBuffer[j];
			printf("%X ",chBuffer[rlen]);
			rlen++;
		}
		printf("\n");

   }
   else if(codeType== BCDCHAR)
   {
		for(i=0;i<len1;i++)
		{
			char l,h=str[i];
			if(i+1== len1)
			{
	/* Adding zero after the datagram,when it
	   misses the bits to be consist of a byte.
	*/
				t=ToBCDChar(h);
			}
			else
			{
				l=str[++i];
				t=ToBCDChar(h);
				t1=ToBCDChar(l);
				t=t+t1*16;
			}
			(*content)[rlen++]=t;
			printf("%X ",(*content)[rlen-1]);

		}
   }

   return rlen;


}




/* fixedLen means the length of fixed part of datagram*/ 
/* while length means the length of dynamic part.     */
/*													  */
void  *SendDataGram( void *arg  ) 
/*void  SendDataGram( char *cmd,char *srcID,char *desID,unsigned char *content,char *locAltitude, char *antAltitude,char *airPressure,char *temperature,char *commFrequency,char isAnswer,int codeType,int altimetryMode,int heightType,int loc_Altitude
Sign,int tempratureValSign,int length,int fixedLen )*/
{
	int i,j,k,count=0;
	unsigned frequency,TemperatureVal;
	long int sum,AirPressureVal;
	unsigned long Loc_AltitudeVal,Ant_AltitudeVal;
    unsigned char checksum,c1,c2,temp;
	unsigned char *DataGram;
	unsigned char CMD[5],*SRCID,*DESID,*Len,Type[1],IsAnswer[1],*Frequency,*LocAltitude,*AntAltitude,*AirPressure,*Temperature;

	Reset( );
	memcpy(CMD, sendArg.cmd, 5);

	if((DataGram= (unsigned char *)malloc( sizeof(unsigned char) *(sendArg.fixedLen+sendArg.length)) ) == NULL)
    {
		printf("Not enough memory to allocate buffer\n");
        	exit(1);  /* terminate program if out of memory */
    }

	/****Add the Command code & convert to ASC value as the header*****/
	for(i=0;i<5;i++)
	{
		if(i==0)
		{
			CMD[0]=36;/*36 is the ASC value of '$'*/
		}
	    else
		{
			CMD[i]=ToBCDChar(CMD[i]);
		}
	}
	strcpy(DataGram,CMD);
	count+=sizeof(CMD);

	/***********Judge the command type to deal with them separately********************/
	switch( CMD[1] )
	{
	case 'D':
		/******apply the request for location's information.*/

		/* The fixed part's length of corresponding datagram*/
		/* is  21  bytes and caculate  the   total   length!*/

		DecimalToHex(sendArg.fixedLen+sendArg.length,0);
		ToHexChar(&Len,2);

		/*Put the main string and the substring together!*/
		for(i=0;i<2;i++)
		{
     			printf("%X ", Len[i]);
     			DataGram[count++]=Len[i];
		}
   		DataGram[count]='\0';
   		printf("\n");

		printf("Now1 it is:");
		for(i=0;i<count;i++)
                   printf("%X ",DataGram[i]);
		printf("\n");
		free( Len );
		Len= NULL;

		/*****Add the user source ID**********************/
		sum=0;
        	sum = atol(sendArg.srcID);        	printf("The decimal num is %ld\n",sum);
		if(sum >1048576 )
		{
			printf("User ID has exceeded the valid value border!" );
			return;

		}
		Reset( );
		printf("The decimal num is %ld\n",sum);
		DecimalToHex(sum,0);
		ToHexChar(&SRCID,3);


		for(i=0;i<3;i++)
		{
			if(i==0)
			{
			  SRCID[i] &=0x1F;
			}
     		printf("%X ", SRCID[i]);
     		DataGram[count++]=SRCID[i];

   		}
   		DataGram[count]='\0';
   		printf("\n");

		printf("Now2 it is:");
		for(i=0;i<count;i++)
			printf("%X ",DataGram[i]);
        	printf("\n");
		free(SRCID);
		SRCID= NULL;


		/****Add the type of information to datagram*****/
		switch( sendArg.dwsq->altimetryMode )
		{
		case ALTIMETRYMODE_CHOOSEN:

			if( sendArg.dwsq->heightType == HEIGHTINDICTATION_NORMAL )
			{
				/* Set the type of information*/

				/* Set the AltimetryMode to ALTIMETRYMODE_CHOOSEN */
				DataGram[count] = DataGram[count] & 0xF7 &0xFB;
				/* Set the HeightType to HEIGHTINDICTATION_NORMAL */
				DataGram[count] = DataGram[count] & 0xFE & 0xFD;
				/* Secure the former 4 bits to zero */
				DataGram[count++] &= 0x0F;

				DataGram[count]='\0';
   				printf("\n");


				/* Set the location Altitude */
				Loc_AltitudeVal= 0;
				Loc_AltitudeVal= atol( sendArg.dwsq->locAltitude );				printf("**************Loc_AltitudeVal %ld***********\n",Loc_AltitudeVal);
				Reset( );
				DecimalToHex(Loc_AltitudeVal,0);
				ToHexChar( &LocAltitude,2 );

				for( i=0;i<2;i++ )
				{
					if( i == 0)
					{
						/* Set the sign of  Altitude value */
						if( sendArg.dwsq->loc_AltitudeSign == 0 )
							LocAltitude[i] &= 0x7F;
						else if( sendArg.dwsq->loc_AltitudeSign == 1 )
							LocAltitude[i] |= 0x80;
					}
     				printf("%X ", LocAltitude[i]);
     				DataGram[count++]= LocAltitude[i];
				}
   				DataGram[count]='\0';
   				printf("\n");
				free( LocAltitude );
				LocAltitude= NULL;

				printf("Now3 it is:");
				for(i=0;i<count;i++)
					printf("%X ",DataGram[i]);
				printf("\n");

				for( i=0;i<2;i++ )
					DataGram[count++]= 0x00;
				DataGram[count]='\0';
   				printf("\n");

				/* Set the data of Air Pressure & temperature*/
				for( i=0;i<4;i++ )
						DataGram[count++]= 0x00;
				DataGram[count]='\0';
   				printf("\n");
				
				/* Set the frequency of the message sent to the terminal */
				frequency=0;
				frequency=atol(sendArg.dwsq->commFrequency);

				Reset( );
				printf("The decimal num is %ud\n",frequency);
				DecimalToHex(frequency,0);
				ToHexChar(&Frequency,2);


				for(i=0;i<2;i++)
				{
			
     				printf("%X ", Frequency[i]);
     				DataGram[count++]= Frequency[i];

   				}
   				DataGram[count]='\0';
   				printf("\n");
				free( Frequency );
				Frequency= NULL;


				/* And caculate the checksum */
				for(i=0;i<count;i++)
					checksum^=DataGram[i];
				DataGram[count++]=checksum;
				DataGram[count]='\0';

				/* Send the datagram */
				printf("Final it is:");
				for(i=0;i<count;i++)
					printf("%X ",DataGram[i]);				printf("\n");


		
			}
			else if( sendArg.dwsq->heightType == HEIGHTINDICTATION_UPPERAIR )
			{
				/* Set the type of information*/

				/* Set the AltimetryMode to ALTIMETRYMODE_CHOOSEN */
				DataGram[count] = DataGram[count] & 0xF7 & 0xFB;
				/* Set the HeightType to HEIGHTINDICTATION_UPPERAIR */
				DataGram[count] = DataGram[count] | 0x01 & 0xFD;
				/* Secure the former 4 bits to zero */
				DataGram[count++] &= 0x0F;

				DataGram[count]='\0';
   				printf("\n");


				/* Set the location Altitude */
				Loc_AltitudeVal= 0;
				Loc_AltitudeVal= atol( sendArg.dwsq->locAltitude );
				Reset( );
				DecimalToHex(Loc_AltitudeVal,0);
				ToHexChar( &LocAltitude,2 );

				for( i=0;i<2;i++ )
				{
					if( i == 0)
					{
						/* Set the sign of  Altitude value */
						if( sendArg.dwsq->loc_AltitudeSign == 0 )
							LocAltitude[i] &= 0x7F;
						else if( sendArg.dwsq->loc_AltitudeSign == 1 )
							LocAltitude[i] |= 0x80;
					}
     				printf("%X ", LocAltitude[i]);
     				DataGram[count++]= LocAltitude[i];
				}
   				DataGram[count]='\0';
   				printf("\n");
				free( LocAltitude );
				LocAltitude= NULL;

				printf("Now3 it is:");
				for(i=0;i<count;i++)
					printf("%X ",DataGram[i]);
				printf("\n");

				for( i=0;i<2;i++ )
					DataGram[count++]= 0x00;
				DataGram[count]='\0';
   				printf("\n");

				/* Set the data of Air Pressure & temperature*/
				for( i=0;i<4;i++ )
						DataGram[count++]= 0x00;
				DataGram[count]='\0';
   				printf("\n");
				
				/* Set the frequency of the message sent to the terminal */
				frequency=0;
				frequency=atol(sendArg.dwsq->commFrequency);

				Reset( );
				printf("The decimal num is %ud\n",frequency);
				DecimalToHex(frequency,0);
				ToHexChar(&Frequency,2);


				for(i=0;i<2;i++)
				{
			
     				printf("%X ", Frequency[i]);
     				DataGram[count++]= Frequency[i];

   				}
   				DataGram[count]='\0';
   				printf("\n");
				free( Frequency );
				Frequency= NULL;


				/* And caculate the checksum */
				for(i=0;i<count;i++)
					checksum^=DataGram[i];
				DataGram[count++]=checksum;
				DataGram[count]='\0';

				/* Send the datagram */
				printf("Final it is:");
				for(i=0;i<count;i++)
					printf("%X ",DataGram[i]);

			}

			break;

		case ALTIMETRYMODE_NONE:

			if( sendArg.dwsq->heightType == HEIGHTINDICTATION_NORMAL )
			{
				/* Set the type of information*/

				/* Set the AltimetryMode to ALTIMETRYMODE_NONE */
				DataGram[count] = DataGram[count] & 0xF7 | 0x04;
				/* Set the HeightType to HEIGHTINDICTATION_NORMAL */
				DataGram[count] = DataGram[count] & 0xFE & 0xFD;
				/* Secure the former 4 bits to zero */
				DataGram[count++] &= 0x0F;

				DataGram[count]='\0';
   				printf("\n");


				/* Set the antennna Altitude */

				/* Set the higher 16 bits to zero */
				for( i=0;i<2;i++ )
					DataGram[count++]= 0x00;
				DataGram[count]='\0';
   				printf("\n"); 

				/* Set the lower  16 bits to store the antenna height */
				Ant_AltitudeVal= 0;
				Ant_AltitudeVal= atol( sendArg.dwsq->antAltitude );
				Reset( );
				DecimalToHex(Ant_AltitudeVal,0);
				ToHexChar( &AntAltitude,2 );

				for( i=0;i<2;i++ )
				{
					printf("%X ", AntAltitude[i]);
     				DataGram[count++]= AntAltitude[i];
				}
   				DataGram[count]='\0';
   				printf("\n");
				free( AntAltitude );
				AntAltitude= NULL;

				printf("Now3 it is:");
				for(i=0;i<count;i++)
					printf("%X ",DataGram[i]);
				printf("\n");

			
				/* Set the data of Air Pressure & temperature*/
				for( i=0;i<4;i++ )
						DataGram[count++]= 0x00;
				DataGram[count]='\0';
   				printf("\n");
				
				/* Set the frequency of the message sent to the terminal */
				frequency=0;
				frequency=atol(sendArg.dwsq->commFrequency);

				Reset( );
				printf("The decimal num is %u\n",frequency);
				DecimalToHex(frequency,0);
				ToHexChar(&Frequency,2);

				for(i=0;i<2;i++)
				{
			
     				printf("%X ", Frequency[i]);
     				DataGram[count++]= Frequency[i];

   				}
   				DataGram[count]='\0';
   				printf("\n");
				free( Frequency );
				Frequency= NULL;


				/* And caculate the checksum */
				for(i=0;i<count;i++)
					checksum^=DataGram[i];
				DataGram[count++]=checksum;
				DataGram[count]='\0';

				/* Send the datagram */
				printf("Final it is:");
				for(i=0;i<count;i++)
					printf("%X ",DataGram[i]);

	
			}
			else if( sendArg.dwsq->heightType == HEIGHTINDICTATION_UPPERAIR )
			{
				/* Set the type of information*/

				/* Set the AltimetryMode to ALTIMETRYMODE_NONE */
				DataGram[count] = DataGram[count] & 0xF7 | 0x04;
				/* Set the HeightType to HEIGHTINDICTATION_UPPERAIR */
				DataGram[count] = DataGram[count] | 0x01 & 0xFD;
				/* Secure the former 4 bits to zero */
				DataGram[count++] &= 0x0F;

				DataGram[count]='\0';
   				printf("\n");


				/* Set the antennna Altitude */

				/* Set the all 4 bytes to store the antenna height */
				Ant_AltitudeVal= 0;
				Ant_AltitudeVal= atol( sendArg.dwsq->antAltitude );

⌨️ 快捷键说明

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