You are on page 1of 6

Hi.I designed my own functions for PDU ot ASCII conversion and vis versa.

I work with PIC18FXXXbut I


thing that the functions will work properly aver all MCU.
Here is the functions:

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
//// PDU converter
//// Designed by eng. Valentin Nikolov
//// E-mail: val_niko@yahoo.com
//// Mobile: +359 889 49 74 26
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

unsigned char send[160];

////////////////////////////////////////////////////////////////////////////////
void toPDU(char *asci, char *temp)
{
unsigned short pos=0, get=1, count=0,h=0,len=0;

len=strlen(asci);

if(15>=len>= 8 ) len--;
else len=len-(len/8 );

while (pos!=len)

{
switch (count)
{
case 0: get=1; break;
case 1: get=3; break;
case 2: get=7; break;
case 3: get=15; break;
case 4: get=31; break;
case 5: get=63; break;
case 6: get=127; break;
}
temp[pos]=( asci[pos+h]>>count) | (( asci[pos+(h+1)]& get)<<(7-count));
pos++;
count++;
if (count>6) count=0, h++;
}
}
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void toASCII(char *asci, char temp[])


{
unsigned short pos=0, get1=127, get2=128, count=0,h=0,len=0, alpha=0,mem0=0,mem1=0;
unsigned short convert[160];

len=strlen(asci);
while(*asci)
{
alpha=*asci;
switch (alpha )
{
case 48: alpha=0; break;
case 49: alpha=1; break;
case 50: alpha=2; break;
case 51: alpha=3; break;
case 52: alpha=4; break;
case 53: alpha=5; break;
case 54: alpha=6; break;
case 55: alpha=7; break;
case 56: alpha=8; break;
case 57: alpha=9; break;
case 65: alpha=10; break;
case 66: alpha=11; break;
case 67: alpha=12; break;
case 68: alpha=13; break;
case 69: alpha=14; break;
case 70: alpha=15; break;
}
convert[pos]=alpha;
pos++;

*asci++;
}

pos=0;
while (pos!=len)
{
convert[pos]=(convert[pos+pos]*16)+ convert[pos+pos+1];
pos++;
}

pos=0;
count=0;
while (pos!=(len/2+h))
{

switch (count)
{
case 0: get1=127; get2=128; break;
case 1: get1=63; get2=128; break;
case 2: get1=31; get2=192; break;
case 3: get1=15; get2=224; break;
case 4: get1=7; get2=240; break;
case 5: get1=3; get2=248; break;
case 6: get1=1; get2=252; break;
}

temp[pos]=((convert[pos-h]& get1)<<(count)) | ((convert[pos-h-1]& get2) >>(7-(count-1)));


pos++;
count++;
if(count==7)
{
temp[pos]=convert[pos-h-1]>>1;
convert[pos-h-1]=0;
pos++;
count=0;
h++;

}
}

}
////////////////////////////////////////////////////////////////////////////////

void main()
{

//toPDU("This is probe", send);

toASCII("54747AE4ACF4170F95B5C2E", send);

while(1) {}

As you can see the converted data are stored in send[]. Because of memory deficit, the maximum length
of strings are 160 chars (I thing it was and the maximum string in the SMS). In function toPDU the
translated data stored in send[] are decimal so you should convert in hex before use it. The function
strlen() get the lenght of the string. If you dont have this functin in your compiler (mine is MIKROC) you
should build your own one.

buff = buffer[0];
a=rev[0];
resp = strstr(buffer,a);
if(resp != NULL)
{
recibido=1;
}
buff = buffer[0];
a="RIN";
resp = strstr(buffer,a);
if(resp != NULL)
{
P_PORTB = 0xFF;
}

You might also like