📄 pdu_chris.c
字号:
#include "PDU_chris.h"
//行地址指针数组
u08 *RowAddress[6];
//行数寄存器
u08 RowCount=0;
//RowAddress_p 指向"行地址指针数组"的指针
u08 **RowAddress_p=&RowAddress;
//暂时定义两个结构数组
struct TP_UDstruct TP_UD[2];
//pSrc point to the orignal register
//pDst point to the destination register, which the coverted Hex stored to.
//nSrcLength indicate how many orignal byte to covert to Hex
//pSrc 源数据地址
//pDst 目标寄存器
//nSrcLength 要处理的源数据字节数
void charToHex(u08 *pSrc, u08 *pDst, u08 nSrcLength)
{
u08 j=0;
for(j=0;j<nSrcLength;j++)
{
//if 0<=x<=9, then x minus char "0".
if ((*pSrc>='0')&&(*pSrc<='9'))
{
//store the x to high half-byte
*pDst=(*pSrc-'0')<<4;
}
else
{
//if not 0<=x<=9, then x minus char "A".
//NOTE!\: Not considered the lowercase!
*pDst=(*pSrc-'A'+10)<<4;
}
pSrc++;
//if 0<=x<=9, then x minus char "0".
if(*pSrc>='0'&&*pSrc<='9')
{
//store the x to low half-byte
*pDst|=*pSrc-'0';
}
else
{
//if not 0<=x<=9, then x minus char "A".
//NOTE!\: Not considered the lowercase!
*pDst|=*pSrc-'A'+10;
}
pSrc++;
pDst++;
}
}
//get all the PDUdata which *p point to ,the *p is available in u08 *RowAddress[6];
//and store each member to corresponding register in the PDU struct
//*p PDU地址
//charToHex() 把字符转16进制
//此函数从源数据串中提取PDU数据并存于相应单元
void GetAllPDUData(u08 *p)
{
u08 i=0;
u08 * t;
u08 *pp;//unused!!
//convert 2 byte SCAddressLength to Hex
charToHex( p, (struct TP_UDstruct*)&TP_UD[0].SCAddressLength, 1);
//point to next member
p+=2;
//convert 2 byte SCAddressLength to Hex
charToHex( p, (struct TP_UDstruct*)&TP_UD[0].SCAddressType, 1);
//point to next member
p+=2;
//store the SCA number ,not convert char to Hex
i=0;
while(1)
{
TP_UD[0].SCAddressValue[i] = *(p+1);
TP_UD[0].SCAddressValue[i+1] = *(p);
if( *(p)=='F')
{
break;
}
p+=2;
i+=2;
}
/* //this is the old idea ,not suit for variational number.
for(i=0;i<13;i+=2,p+=2)
{
TP_UD[0].SCAddressValue[i] = *(p+1);
TP_UD[0].SCAddressValue[i+1] = *(p);
}
*/
//point to next member
p+=2;
//convert 2 byte FirstOctet to Hex
charToHex( p, (struct TP_UDstruct*)&TP_UD[0].FirstOctet, 1);
//point to next member
p+=2;
//convert 2 byte SrcAddressLength to Hex
charToHex( p, (struct TP_UDstruct*)&TP_UD[0].SrcAddressLength, 1);
//point to next member
p+=2;
//convert 2 byte SrcAddressType to Hex
charToHex( p, (struct TP_UDstruct*)&TP_UD[0].SrcAddressType, 1);
//point to next member
p+=2;
//store the Src number ,not convert char to Hex
i=0;
while(1)
{
TP_UD[0].SrcAddressValue[i] = *(p+1);
TP_UD[0].SrcAddressValue[i+1] = *(p);
if( *(p)=='F')
{
break;
}
p+=2;
i+=2;
}
/* //this is the old idea ,not suit for variational number.
for(i=0;i<13;i+=2,p+=2)
{
TP_UD[0].SrcAddressValue[i] = *(p+1);
TP_UD[0].SrcAddressValue[i+1] = *(p);
}
*/
//point to next member
p+=2;
//convert 2 byte TP_PID to Hex
charToHex( p, (struct TP_UDstruct*)&TP_UD[0].TP_PID, 1);
//point to next member
p+=2;
//convert 2 byte TP_DCS to Hex
charToHex( p, (struct TP_UDstruct*)&TP_UD[0].TP_DCS, 1);
//point to next member
p+=2;
//get the Date,store each member to year month day hour minute second timezone
t=(struct TP_UDstruct*)TP_UD[0].year;
for(i=0;i<14;i+=2,p+=2)
{
*(t+i)=*(p+1);
*(t+i+1)=*p;
}
//convert 2 byte TP_UDL to Hex
charToHex( p, (struct TP_UDstruct*)&TP_UD[0].TP_UDL, 1);
//point to next member
p+=2;
//convert TP_UD to Hex, TP_UDL is the length of TP_UD
charToHex( p, (struct TP_UDstruct*)TP_UD[0].TP_UD, TP_UD[0].TP_UDL);
}
//*********************************
//SrcAddress is the source string,DstAddress is the destication string
//SrcAddress 要比较的字符串首地址
//DstAddress 参考字符串
u08 PDUstrcmp(u08 *SrcAddress,u08 *DstAddress)
{
//compare complete when meet 0x0D or 0x0A in the address01
while( (*SrcAddress)!=0x0D&&(*SrcAddress)!=0x0A )
{
//compare each char of SrcAddress and DstAddress
if( (*SrcAddress++)!=(*DstAddress++) )
//if not match ,return 0
return 0;
}
//if match ,return 1
return 1;
}
//*********************************
//compare the destination string with "AT"string
//检测目的字符串是否为“AT”
u08 checkAT(u08 *address)
{
return PDUstrcmp(address,"AT") ;
}
//*********************************
//compare the destination string with "OK"string
//检测目的字符串是否为“OK”
u08 checkOK(u08 *address)
{
return PDUstrcmp(address,"OK") ;
}
//*********************************
//compare the destination string with "ERROR"string
//检测目的字符串是否为“ERROR”
u08 checkERROR(u08 *address)
{
return PDUstrcmp(address,"ERROR") ;
}
//*********************************
//get the row count and the address of each row in the received buffer
//buffer: point to uartRxData(the uart received PDUbuffer)
//RowAddress: is the pointer pointed to pointer group
//RowCount: is the pointer pointed to RowCount
//buffer: GSM送回的没处理过的数据首地址
//RowAddress_p: 指向"行地址指针数组"的指针
//RowCount_p: 数向"行数寄存器"的指针
//此函数获取每行参数
void getRowReference(u08 *buffer,u08 **RowAddress_p,u08 *RowCount_p )
{
u08 notComplete=1;
//RowAddress_p[0] is point to the first row
*RowAddress_p=buffer;
//initialize the RowCount with value 1
(*RowCount_p)=1;
while( notComplete==1 )
{
if( (*buffer)==0x0A||(*buffer)==0x0D )
{
//when meet 0x0A or 0x0D, buffer pointer increase
while( (*buffer)==0x0A||(*buffer)==0x0D )
{
buffer++;
}
//WHY?\:why can not use *(RowAddress+RowCount)=buffer;
//here buffer is point to the next row
//store the next row address
RowAddress_p[*RowCount_p]=buffer;
//increase the row count
(*RowCount_p)++;
//ORIGNAL\:if( checkOK(buffer)||checkERROR(buffer) )
//if this row is the string of "OK" or "ERROR"
//then get out of getRowReference()
if( checkOK(buffer) )
{
//when complete, break the while()
notComplete=0;
}
}
else
{
//if not the 0x0A or 0x0D, buffer pointer increase
buffer++;
}
}
}
//RowNum: 0-x (x=1,2,3...)
//buffer: point to uartRxData(the uart received PDUbuffer)
//RowNum: the row you want to get its address
//Bug!\: this function will run forever when RowNum exceed the actual row number
//buffer: GSM送回的没处理过的数据首地址
//RowNum: 要传回的行的首地址
u08 *getRowAddress(u08 *buffer,u08 RowNum )
{
//the current row indicator
u08 i=0;
while(1)
{
if( (*buffer)==0x0A||(*buffer)==0x0D )
{
//when meet 0x0A or 0x0D, buffer pointer increase
while( (*buffer)==0x0A||(*buffer)==0x0D )
{
buffer++;
}
//increase the current row, which *buffer point to
i++;
if(i>=RowNum)
{
//if match the row you want to get,
//break and return the row address
return buffer;
}
}
else
{
//if not the 0x0A or 0x0D, buffer pointer increase
buffer++;
}
}
}
//this function is attached to exterior interrupt(GSM /RING0 pin),
//triger in falling edge
void newSMSIndicator(void)
{
newSMS=1;
}
//struct TP_UDstruct * TP_UD_p;//use the pointer may course problem WHY?
//new message process,store all the orignal dataS to
//corresponding register in the PDUstruct.
void newSMSProcess(void)
{
//此函数从源数据串中提取PDU数据并存于相应单元
GetAllPDUData( getRowAddress(tt,2) );
//GetAllPDUData( getRowAddress(uartRxData,2) );
}
/*
//*********************************
//! create and initialize the pdu buffers
void PDUInitBuffers(void)
{
// initialize the pdu receive buffer
bufferInit(&PDURxBuffer, PDURxData, PDU_RX_BUFFER_SIZE);
}
//*********************************
//change the order and store the u16 data to the "Address"
void GetSwapData(u16 Address)
{
Address = bufferGetFromFront(&PDURxBuffer);
Address <<= 8;
Address |= bufferGetFromFront(&PDURxBuffer);
}
void SendCommand(u08 Command[10])
{
u08 i;
for(i=0;i<10;i++)
{
uartAddToTxBuffer(Command[i]);
}
uartSendTxBuffer();
}
void uartSendReceivedByte(void)
{
u08 c;
while(uartReceiveByte(&c))
rprintfChar(c);//can not use the rprintf();WHY???
}
void uartswSendHwReceivedByte(void)
{
u08 c;
while(uartReceiveByte(&c))
uartswSendByte(c);
}
void uartswSendHwReceivedByte02(void)
{
u08 c,start=0;
while(uartReceiveByte(&c))
{
if(c=='\n')//when meet the '\n' then start read data
{
start++;
}
if(start==2)
{
uartswSendByte(c);
}
}
}
void GetAllData(void)
{
u08 i;
GetSwapData(SCAddressLength);
GetSwapData(SCAddressType);
for(i=0;i<8;i++)
{
GetSwapData(SCAddressValue[i]);
}
//******************
GetSwapData(FirstOctet);
//******************
GetSwapData(SrcAddressLength);
GetSwapData(SrcAddressType);
for(i=0;i<8;i++)
{
GetSwapData(SrcAddressValue[i]);
}
//********************
GetSwapData(TP_PID);
GetSwapData(TP_DCS);
GetSwapData(year);
GetSwapData(month);
GetSwapData(day);
GetSwapData(hour);
GetSwapData(minute);
GetSwapData(second);
GetSwapData(timezone);
GetSwapData(TP_UDL);
}
*/
//u08 charToHex(u08 H,u08 L);
/*
void getDate(void)
{
u08 *i;
i=&TP_UD_p->year;
for(i=0;i<14;i+=2,p+=2)
{
*i=*(p+1);
*(i+1)=*p;
}
}
*/
/*
void getRowReference(u08 *buffer,u08 *RowAddress,u08 *RowCount )
{
sbi(PORTB,PB0);//LED LED LED
u08 notComplete=1;
*RowAddress=buffer;
*RowCount=1;
while( notComplete )
{
sbi(PORTB,PB1);//LED LED LED
if( (*buffer)==0x0A||(*buffer)==0x0D )
{
while( (*buffer)==0x0A||(*buffer)==0x0D )
{
sbi(PORTB,PB2);//LED LED LED
buffer++;
}
//why can not use *(RowAddress+RowCount)=buffer;
(*RowAddress)[RowCount]=buffer;
*RowCount++;
if( checkOK(buffer)||checkERROR(buffer) )
{
sbi(PORTB,PB3);//LED LED LED
notComplete=0;
sbi(PORTB,PB3);//LED LED LED
}
}
else
{
buffer++;
}
}
}
*/
/*
void GetAllData02(u08 *p)
{
u08 i;
SCAddressLength = charToHex(*p++,*p++);
SCAddressType = charToHex(*p++,*p++);
for(i=0;i<13;i+=2,p+=2)
{
SCAddressValue[i] = *(p+1);
SCAddressValue[i+1] = *(p);
}
FirstOctet = charToHex(*p++,*p++);
SrcAddressLength = charToHex(*p++,*p++);
SrcAddressType = charToHex(*p++,*p++);
for(i=0;i<13;i+=2,p+=2)
{
SrcAddressValue[i] = *(p+1);
SrcAddressValue[i+1] = *(p);
}
TP_PID = charToHex(*p++,*p++);
TP_DCS = charToHex(*p++,*p++);
getDate();
TP_UDL = charToHex(*p++,*p++);
for(i=0;i<30;i++,p+=4)
{
TP_UD[i]=04charToU32Hex(p);
}
}
void newSMSProcess02(void)
{
//此函数获取每行参数
//getRowReference( uartRxData,RowAddress_p,&RowCount );
getRowReference( tt,RowAddress_p,&RowCount );
sbi(PORTB,PB1);//LED LED
//此函数从源数据串中提取PDU数据并存于相应单元
GetAllPDUData(RowAddress[2]);
}
void newSMSProcess02(void)
{
//此函数获取每行参数
//getRowReference( uartRxData,RowAddress_p,&RowCount );
getRowReference( tt,RowAddress_p,&RowCount );
sbi(PORTB,PB1);//LED LED
//此函数从源数据串中提取PDU数据并存于相应单元
GetAllPDUData(RowAddress[RowCount-2]);
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -