📄 receivedatagramv2.3.c~
字号:
#include "MySHM.h"#include "Wrapper.c"void WriteHzStr(int num);
void WriteHz(unsigned char,unsigned char,int,int);
void GetHzBit(unsigned char,unsigned char);MYSHM *ptr;int tidcount=0;pthread_attr_t attr;pthread_t tid_Produce[MAXNTHREADS];
/*Definition for CN character convert
FILE *fp;
long int fpos;
char bitdata[32];
int gmode,gdrive=DETECT;*/
/* Check if the id of a thread is out of range */void CheckTidCount( void ){ if( tidCount > MAXNTHREADS ) tidCount = 0; else tidCount++;}
void *Produce( void *arg ){ int dosignal; NEWDATA *newData=(NEWDATA *)arg; int tid=pthread_self(); printf("thread child%d enter\n",tid); Pthread_mutex_lock(&newData->myshmPtr->curMesg_mutex); newData->myshmPtr->currentMesg = newData->currentMesg;printf("thread %d's type is %d,%d. \n",newData->myshmPtr->currentMesg,newData->currentMesg); switch( newData->currentMesg ) { case TYPE_OF_DWXX: //Pthread_mutex_lock(&newData->myshmPtr->DWXX_mutex); printf("$$$$$$Address of SHM to DWXX is %lX.$$$$$$$\n",newData->myshmPtr); printf("-------Later Address DWXX of newData is %lX.-------\n",newData); printf("-------Later Address DWXX of newData->dwxx is %lX.-------\n",newData->dwxx); newData->myshmPtr->dwxx = *(newData->dwxx); /* Print the Value of each member of DWXX */ printf("Hour:%d,Minute:%d,QueryAddr:%X\n",newData->myshmPtr->dwxx.hour,newData->myshmPtr->dwxx.minute,newData->myshmPtr->dwxx.queryAddr); //Pthread_mutex_unlock(&newData->myshmPtr->DWXX_mutex); free( newData->dwxx ); break; case TYPE_OF_TXXX: //Pthread_mutex_lock(&newData->myshmPtr->TXXX_mutex); printf("$$$$$$Address of SHM to DWXX is %lX.$$$$$$$\n",newData->myshmPtr); printf("-------Later Address TXXX of newData is %lX.------\n",newData); printf("-------Later Address TXXX of newData->txxx is %lX.-------\n",newData->txxx); newData->myshmPtr->txxx = *(newData->txxx); /* Print the Value of each member of TXXX */ printf("SRCID is %X\n",(newData->myshmPtr->txxx).srcID); //Pthread_mutex_unlock(&newData->myshmPtr->TXXX_mutex); free( newData->txxx ); break; case TYPE_OF_ICXX: //Pthread_mutex_lock(&newData->myshmPtr->ICXX_mutex); newData->myshmPtr->icxx = *(newData->icxx); /* Print the Value of each member of ICXX */ //Pthread_mutex_unlock(&newData->myshmPtr->ICXX_mutex); free( newData->icxx ); break; case TYPE_OF_ZJXX: //Pthread_mutex_lock(&newData->myshmPtr->ZJXX_mutex); newData->myshmPtr->zjxx = *(newData->zjxx); /* Print the Value of each member of ZJXX */ //Pthread_mutex_unlock(&newData->myshmPtr->ZJXX_mutex); free( newData->zjxx ); break; case TYPE_OF_SJXX: //Pthread_mutex_lock(&newData->myshmPtr->SJXX_mutex); newData->myshmPtr->sjxx = *(newData->sjxx); /* Print the Value of each member of SJXX */ //Pthread_mutex_unlock(&newData->myshmPtr->SJXX_mutex); free( newData->sjxx ); break; case TYPE_OF_BBXX: //Pthread_mutex_lock(&newData->myshmPtr->BBXX_mutex); newData->myshmPtr->bbxx = *(newData->bbxx); /* Print the Value of each member of BBXX */ //Pthread_mutex_unlock(&newData->myshmPtr->BBXX_mutex); free( newData->bbxx ); break; case TYPE_OF_FKXX: //Pthread_mutex_lock(&newData->myshmPtr->FKXX_mutex); newData->myshmPtr->fkxx = *(newData->fkxx); /* Print the Value of each member of FKXX */ //Pthread_mutex_unlock(&newData->myshmPtr->FKXX_mutex); free( newData->fkxx ); break; } Pthread_mutex_unlock(&(newData->myshmPtr->curMesg_mutex)); Pthread_mutex_lock(&newData->myshmPtr->nready_mutex); dosignal = (newData->myshmPtr->nready == 0); newData->myshmPtr->nready++; Pthread_mutex_unlock(&newData->myshmPtr->nready_mutex); if(dosignal) Pthread_cond_signal(&newData->myshmPtr->cond); sleep(2); free( newData); return(NULL); }
/* We should wait to receive the datagram in a
daemon process or a single bankground thread
all the time!
*/
void ReceiveDataGram( unsigned char recv )
{
int i,j; static int count=0;
static unsigned char *DataGram,CMD[7],checkSum=0;
static long DESID, DataLen; static NEWDATA *newData; static unsigned firstByte =0,secondByte=0,thirdByte=0,mid=256; if( recv == '$')
{
if( count == 0)
{ /*Begin to caculate checksum and recvieve data.*/
checkSum ^= recv;
CMD[count++]= recv;
}
else /*Reset the setting to avoid to receive the incorrect datagram*/
{
checkSum=0;
count=0;
return;
}
}
else
{
if( count == DataLen-1 )
{
printf("Actual checksum value is : %X\n",checkSum);
}
checkSum ^= recv;
if( count<= 6)
{
CMD[count]= recv;
if( count == 6)
{
/* Getting to know the total length of */
/* datagram in the 6th,7th byte,then */
/* we shall allocate the buffer area! */
firstByte= CMD[count-1];
secondByte= CMD[count ];
DataLen= (long)firstByte*mid+secondByte; printf("DataLen is %ld\n",DataLen);
if ((DataGram= (unsigned char *)malloc(sizeof(unsigned char) *DataLen) ) == NULL)
{
printf("Not enough memory to allocate buffer\n");
exit(1); /* terminate program if out of memory */
}
memcpy(DataGram,CMD,7);
}
count++;
}
else if( count>6 && count<DataLen-1)
{
DataGram[count++]= recv;
}
else if( count == DataLen-1 )
{
if( checkSum == 0)
{
/* If checksum equals to zero at this time,it */
/* means the checking for datagram is correct.*/
/* We shall get the available message from it.*/
printf("Code is %c \n",DataGram[1]);
switch( DataGram[1] )
{
case 'D': newData = (NEWDATA *)malloc(sizeof( NEWDATA )); newData->dwxx = (DWXX *)malloc(sizeof( DWXX ));
/* Handle the information of location*/
/* Obtain the id of the source user. */
firstByte= DataGram[7];
secondByte= DataGram[8];
thirdByte= DataGram[9];
newData->dwxx->srcID= (long) firstByte*mid*mid + secondByte*mid + thirdByte;
printf("SRCID is =%lX\n",newData->dwxx->srcID);
/**Obtain the details of the message's type from the 10th byte**/
/* This bit state for the message type is location or query. */
if( ( newData->dwxx->locatType = DataGram[10] & 0x20) == 0)
printf("Location.\n ");
else
printf("Inquery. \n ");
/* This bit state for secret key encryption*/
if( ( newData->dwxx->isSecretKey = DataGram[10] & 0x10) == 0)
printf("Never been encrypted.\n ");
else
printf("Have been encrypted. \n ");
/* This bit state for the precision.*/
/* First class states for 20 meters. */
/* Second class states for 100 meters*/
if( ( newData->dwxx->precision = DataGram[10] & 0x08) == 0)
printf("First class.\n ");
else
printf("Second class.\n ");
/* This bit state for emergency location*/
if( ( newData->dwxx->isEmergenLoc = DataGram[10] & 0x04) == 0)
{
printf("Not choosed.\n ");
}
else
{
printf("Have been choosed.\n ");
}
/* This bit state for mutiple_answer.*/
if( ( newData->dwxx->mutipleKey = DataGram[10] & 0x02) == 0)
{
printf("Incorrect.\n ");
}
else
{
printf("Correct.\n ");
}
/* This bit state for type of height*/
if( ( newData->dwxx->heightType = DataGram[10] & 0x01) == 0)
{
printf("Normal.\n ");
}
else
{
printf("Height.\n ");
}
/**Obtain the Query ID from the message**/
if( ( newData->dwxx->locatType == 0x00) && (DataGram[11] == DataGram[12] == DataGram[13] == 0x00) )
{
newData->dwxx->queryAddr= 0;
}
else if( newData->dwxx->locatType == 0x01)
{
firstByte= DataGram[11];
secondByte= DataGram[12];
thirdByte= DataGram[13];
newData->dwxx->queryAddr= (long) firstByte*mid*mid + secondByte*mid + thirdByte;
printf("QueryAddr is =%lX\n",newData->dwxx->queryAddr);
}
else
{
printf("Datagram has breached the BD protocol!");
}
/**Obtain the detail data of location from the message**/
/* Obtain the time information */
newData->dwxx->hour= DataGram[14];
if( newData->dwxx->hour<0 || newData->dwxx->hour>24)
{
printf(" Incorrect Hour!");
exit( 0 );
}
else
printf(" Received Hour is:%d \n",newData->dwxx->hour);
newData->dwxx->minute= DataGram[15];
if( newData->dwxx->minute<0 || newData->dwxx->minute>60)
{
printf(" Incorrect Minute!");
exit( 0 );
}
else
printf(" Received Minute is:%d \n",newData->dwxx->minute);
newData->dwxx->second= DataGram[16];
if( newData->dwxx->second<0 || newData->dwxx->second>60)
{
printf(" Incorrect Minute!");
exit( 0 );
}
else
printf(" Received second is:%d \n",newData->dwxx->second);
newData->dwxx->perSecond= DataGram[17] * 0.01;
if( newData->dwxx->perSecond<0 )
{
printf(" Incorrect Persecond!");
exit( 0 );
}
else
printf(" Received Persecond is:%f \n",newData->dwxx->perSecond);
/* Obtain the longitude data */
newData->dwxx->longitude.du = DataGram[18];
if( newData->dwxx->longitude.du <0 || newData->dwxx->longitude.du >360)
{
printf(" Incorrect L_Degree!");
exit( 0 );
}
else
printf(" Received L_Degree is:%d \n",newData->dwxx->longitude.du);
newData->dwxx->longitude.fen = DataGram[19];
if( newData->dwxx->longitude.fen <0 || newData->dwxx->longitude.fen >60)
{
printf(" Incorrect L_AngleMin!");
exit( 0 );
}
else
printf(" Received L_AngleMin is:%d \n",newData->dwxx->longitude.fen);
newData->dwxx->longitude.miao = DataGram[20];
if( newData->dwxx->longitude.miao <0 || newData->dwxx->longitude.miao >60)
{
printf(" Incorrect L_AngleSec!");
exit( 0 );
}
else
printf(" Received L_AngleSec is:%d \n",newData->dwxx->longitude.miao);
newData->dwxx->longitude.pMiao = DataGram[21] * 0.1;
if( newData->dwxx->longitude.pMiao<0 )
{
printf(" Incorrect L_TenthAnglesec!");
exit( 0 );
}
else
printf(" Received L_TenthAnglesec is:%f \n",newData->dwxx->longitude.pMiao);
/* Obtain the latitude data */
newData->dwxx->latitude.du = DataGram[22];
if( newData->dwxx->latitude.du<0 || newData->dwxx->latitude.du>360)
{
printf(" Incorrect B_Degree!");
exit( 0 );
}
else
printf(" Received B_Degree is:%d \n",newData->dwxx->latitude.du);
newData->dwxx->latitude.fen = DataGram[23];
if( newData->dwxx->latitude.fen<0 || newData->dwxx->latitude.fen>60)
{
printf(" Incorrect B_AngleMin!");
exit( 0 );
}
else
printf(" Received B_AngleMin is:%d \n",newData->dwxx->latitude.fen);
newData->dwxx->latitude.miao = DataGram[24];
if( newData->dwxx->latitude.miao<0 || newData->dwxx->latitude.miao>60)
{
printf(" Incorrect B_AngleSec!");
exit( 0 );
}
else
printf(" Received B_AngleSec is:%d \n",newData->dwxx->latitude.miao);
newData->dwxx->latitude.pMiao = DataGram[25] * 0.1;
if( newData->dwxx->latitude.pMiao < 0 )
{
printf(" Incorrect B_TenthAnglesec!");
exit( 0 );
}
else
printf(" Received B_TenthAnglesec is:%f \n",newData->dwxx->latitude.pMiao);
/**Obtain the height of target place**/
if( newData->dwxx->heightType == 0 )
{
newData->dwxx->normalSign = DataGram[26] & 0xC0;
newData->dwxx->height = (long)(DataGram[26] & 0x3F)*mid + DataGram[27];
newData->dwxx->abnormalSign = DataGram[28];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -