Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Getting string from UART

Status
Not open for further replies.

Qutbuddin

Newbie level 4
Joined
Nov 8, 2012
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,323
Hi Everyone,
I am doing a project using gsm. I want to receive string from UART and compare it with the predefined string.But somehow I am not able to get string from UART. Below is my code

void recieve_string()
{ unsigned char p;
while(RI!=0)
{
p=SBUF;
z=p;
i++;
}while(p!='\0');

RI=0;
compare_string();
}


void compare_string()
{
if(strcmp(z,a)==0){
relay=1;
}
else if(strcmp(z,b)==0){
relay=0;
}
 

Code:
// Program to test serial communication of controller with PC using hyper terminal
#include<reg51.h>

void ini()     // Initialize Timer 1 for serial communication
{
TMOD=0x20;  //Timer1, mode 2, baud rate 9600 bps
TH1=0XFD; 
SCON=0x50;
TR1=1;
}

void recieve()  //Function to receive serial data
{
unsigned char value;
while(RI==0);
value=SBUF;
P1=value;
RI=0;
}

void transmit()  // Funtion to transmit serial data
{
P2=P1-32;
SBUF=P2;
while(TI==0);
TI=0;
SBUF=P1;
while(TI==0);
TI=0;
}

void main()
{
while(1)
{
  ini();
  recieve();
  transmit();
}
}
 

This will read only a single byte from uart but i want read an array of string.
 

Edit receive() following:
Code:
char recieve()  //Function to receive serial data
{
unsigned char value;
while(RI==0);
value=SBUF;
P1=value;
RI=0;
return value
}

Add this function:
Code:
void UReadBuffer(void *buff,int len)
{
	int i;
	for(i=0;i<len;i++)
	{
		((char*)buff)[i]=recieve();
	}
}
 

Thanks for the help.Now I want to compare array of the received string with that of predefined string.Can you please help me with the same ?
 

Thanks for the help.Now I want to compare array of the received string with that of predefined string.Can you please help me with the same ?

First know the string length, then it is easy.
Try this one, it will help you

Code:
int b=0;                   //initialize b
for(int i=0;i<5;i++)  //string length is 5
{
   if(a[i]==x[i])    //here a[] is the string which is received through uart and x[] is predefined string
   {
      b++;        //increment b value by one
   }
}
if(b==5)
{
 //write your output
}
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top