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 and comparing

Status
Not open for further replies.

sam_kevin

Junior Member level 1
Joined
Jan 21, 2013
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,429
Below is my code for receving data in uart and comparing, if the data matches port bit will get low, but the code is not working kindly help me.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <reg51.H>
 sbit sensor = P1^7;
 unsigned char distance, i;
 unsigned char dist[8];
 
 
void main()
{
      TMOD=0X20;                                  //timer1 mode2
      TH1=0XFD;                                     //loading the value to generate baud rate 9600
      SCON=0X50;                                    //mode 1 and set REN (Receive enable) bit
      TR1=1; 
      ES=1;  /* Serial interrupt Enable*/
      EA=1;  /* global interrupt enable*/  
                                                  
while(1);
} 
void serial (void) interrupt 4 
{
 if(RI)
{
  RI=0;
  dist[i]=SBUF; 
 
  
  for(i=0;i<8;i++)          
 
 
   {
    distance= dist[i] ;
    if (distance == "ABCDEF" )
    {
    sensor=0;
   }
   }
 
}                                                 
if(TI)TI=0;
}

 
Last edited by a moderator:

hello

You receive ABCDEF 6 cars long
is there a terminator like <CR> <LF> at the end of message ?
you need to have a terminator to reconnize the end of message
it can be <CR><LF> ( 13 + 10)
or 0 value .. to be able to compare in your programme
I suppose you have CR LF at the end because you are checking indice i=0 to 8

i don't know MCU 51 ......
so example is in C18

Code:
void serial (void) interrupt 4 
{
int k;
 if(RI)
  {
  RI=0;
  dist[i]=SBUF; 
 if (SBUF==13)     //<- test with the reminator caractere, can be CR or value 0
  {
  dist[i]=0;	// string must end by zero value
  k=strcmppgm2ram(dist,"ABCDEF" ); // use function string compare , i hope you have this kind of library
  if (k==0)  sensor=0;             // k=result of comparaison  maybe k=1 if comparaison Ok , check your librayry
  }
 if(TI)TI=0;  // what is it ??
}


or this function
memcmppgm2ram(dist,"ABCDEF",6)

if you don't havet these functions,
do a compare char by char in a loop

Code:
// init this table
msg[]='ABCDEF";

[I][COLOR="#0000FF"]inside interrupt[/COLOR][/I]

k=0;
for(i=0;i<6;i++)
{
  if (dist[i]==msg[i]) k++; else k=0;
 }
if (k==6) sensor=0;
....
 

i am also getting this problem using pic32 uart configuration and i didn't find any solution how to get string and how to compare it.

but by the way as pualfjujo said u be able to create to compare particular char and get the result like below
Code:
if(dist[0] == 'A' )
{
   if(dist[1] == 'B' ){
      if(dist[2] == 'C' ){
       if(dist[3] == 'D' ){
        if(dist[4] == 'E' ){
         if(dist[5] == 'F' ){
            sensor=0;
             }
           }
         }
      }
    }
   
}

AND also i am waiting for how to compare string received using UART of pic or avr .
 

Hello!

Code:
if(dist[0] == 'A' ) {
	if(dist[1] == 'B' ){
		if(dist[2] == 'C' ){
			if(dist[3] == 'D' ){
				if(dist[4] == 'E' ){
					if(dist[5] == 'F' ){
						sensor=0;
					}
				}
			}
		}
	}
}
Nice code!
Just curious: if you have 2 strings, about 1 kbyte long, you will make a source code with 1000 stages
of if statements?
Comparing 2 strings can be a bit smarter and work for any string.
1. If you can use string.h:
use strcmp or strncmp
2. If you cannot use string.h, here is an alternative. string_compare will return
0 if the strings are the same, 1 if they are different (like strcmp).

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#define MAX_LEN 0x100       // Some security, use your own limits
int string_length(char * string){
    int len = 0, i;
    for(i = 0; (str[i] != 0) && (i < MAX_LEN)); i++) len++;
    return len;
}
int string_compare(char * str1, char * str2){
    int l1, l2, i;          // Length of both strings, loop index
    len1 = string_length(str1);
    len2 = string_length(str2);
    if(l1!=l2) return 1;    // Not same length -> not similar
    for(i = 0; i < l1; i++){
        if(str1[i] != str2[i]) break;
    }
    if(i == l1) return 0;   // If the loop reached l1, then no difference has been found
    return 1;
}



NB: This code has been written but not tested and even compiled. It's just an
illustration of the idea. It's not efficient either, it has been written for clarity, not
efficiency.

Dora.
 
thanks a lot now string comparison method also work bro!!!!

Code:
if(!strncmp(dist,"ABCDEF",sizeof("ABCDEF")))
{
       sensor = 0 ;
}

i found it c32 compiler manual thanks bro and also working for strcmp.
 

Kindly find the below code, I have compared both string in this way and my sensor=0 is getting low

please check is this right way to do.

#include <reg52.H> /* special function register declarations */
/* for the intended 8051 derivative */

unsigned char uc, dist[4], msg[] = {"Part"};
sbit sensor = P1^7;
sbit trigger = P1^6;
sbit test = P1^5;
int i;

void main(void)
{
SCON = 0x50; /* SCON: mode 1, 8-bit UART, enable rcvr */
TMOD |= 0x20; /* TMOD: timer 1, mode 2, 8-bit reload */
TH1 = 0xFD; /* TH1: reload value for 4800 baud @ 11.0592 MHz */
TR1 = 1; /* TR1: timer 1 run */
ES=1; /* Serial interrupt Enable*/
EA=1; /* global interrupt enable*/
while(1);
}
void serial (void) interrupt 4
{
if(RI)
{
RI=0;
uc=SBUF;
if (uc == 'P')
{
test=0; // making p1.5 low
dist = uc;
for(i=0;i!='t';i++)
uc=0;
trigger=0; //making P1.6 low
if (dist==msg)
{
sensor=0; //making p1.7 low
SBUF=dist; // Transmitting data to the uart
dist=0; //clearing the array.
}

}

}
if(TI)TI=0;
}
 

Hello!

First use code or syntax tags and make sure it's correctly indented.

Dora.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top