Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Serial comm between uC and PC...

Status
Not open for further replies.
I am trying to compare two string one stored in memory other is scanned..
the problem is that while reading string from terminal i am using array the length will depend on it.
so, comparing of string will be wrong can't we read string in such a way without using a fixed size and compare it???

Code:
unsigned char b[10];
unsigned char *c="Hello";

for(char x=0;x<6;x++){
b[x]=HSerin(); 

}

int result = strncmp(c,b, compareLimit);

if(result > 0){
ch='a';
HSerout(ch);
}

else if(result < 0)
		{
ch='b';
HSerout(ch);
		}
else{
ch='c';
HSerout(ch);
}

You aren't using this code on PC to PC transfers are you??? That code was for Hitech on a pic16... PC to PC needs different coding...
 
You aren't using this code on PC to PC transfers are you??? That code was for Hitech on a pic16... PC to PC needs different coding...

OK, this code was for comparing two string not for PC to PC....the problem in this code is that while scanning/reading the string it depend on size i mean on loop/array indexing i want to make it as per user size like pressing enter will terminate string like this..
 
OK, this code was for comparing two string not for PC to PC....the problem in this code is that while scanning/reading the string it depend on size i mean on loop/array indexing i want to make it as per user size like pressing enter will terminate string like this..

Our biggest problem here is... You are struggling to program one device!!! Programming two.... To talk together... You really need to get one half working as you need it first....
Going off you "Window" thread, you can't get windows to send a string to the outside world... With VB6 you just chuck the MScomm control on a form and write

Code:
MSComm1.output " Hello outside world " & CRLF

It extremely simple...

You can chuck a button on the form and put the line in the button handler and keep repeating...
 
Our biggest problem here is... You are struggling to program one device!!! Programming two.... To talk together... You really need to get one half working as you need it first....
Going off you "Window" thread, you can't get windows to send a string to the outside world... With VB6 you just chuck the MScomm control on a form and write

I would like to stop you for talking about PC to PC transfer...
I am thinking that i should first clear few doubt then move to data transfer.
I am trying to compare two string one is stored in uC memory and 2nd string is readied from PC to UART ten uC here i am not talking about PC to PC only one PC to uC...
for example as we do for making password, the problem i am repeating again is how to store string of variable size if enter is press then it should end after that comparing of that two string is to be done...
i hope now you got my point.
 
Comparing strings and all the other stuff should be learned, from a C language book, instantly. Do you have a semester in programming?
 
Please have a look on previous post i have done that asking for something else..
i want to make read char independent of array size i.e. real time or on pressing enter it should end reading/storing string from terminal window.
Code:
unsigned char b[10];
unsigned char *c="Hello";
 
for(char x=0;x<6;x++){
b[x]=HSerin(); 
 
}
 
int result = strncmp(c,b, compareLimit);
 
if(result > 0){
ch='a';
HSerout(ch);
}
 
else if(result < 0)
		{
ch='b';
HSerout(ch);
		}
else{
ch='c';
HSerout(ch);
}
 
Hi,

i have corrected the code working cool, this code will match the password in terminal window....after pressing ESC the string will terminate!

Code:
#define ESC 27
unsigned char *a=" Hello! Ask me a question!\n ";
unsigned char *w=" WRONG PASSWORD ";
unsigned char *m=" MATCHED PASSWORD ";
unsigned char b[10];
unsigned char *c="Hello";
unsigned char  HSerin(void);
 void HSerout(unsigned char ch),
 HSerinit(void); 

 void main(void)                        // program entry  
   {  
int compareLimit = 100;   
   TRISB=0x00;
   unsigned char ch ;         // <- LOOK HERE.
   ADCON1 = 0x6;                    // Analogue off    
   HSerinit();      
  __delay_ms(1); 

while(*a!=0)
{ 
    ch=*a++;   
    HSerout(ch);  
     __delay_ms(150);       
  }
while(1)                        // endless Loop       
  {  

for(char x=0;x<9;x++){
b[x]=HSerin(); 
if(b[x]== ESC){
b[x]=0;
break;
}

}

int result = strncmp(c,b, compareLimit);

if(result > 0 || result < 0){
while(*w!=0)
{ 
    ch=*w++;   
    HSerout(ch);  
     __delay_ms(150);       
  }
}

else if(result==0)
		{
while(*m!=0)
{ 
    ch=*m++;   
    HSerout(ch);  
     __delay_ms(150);       
  }
		}



}
	}
 
Strings should be null-terminated.. simple as that. The array should be always long enough to hold any string ever imaginable in the particular application. You just compare up to the null character when you do string-to-string comparison.
https://en.wikipedia.org/wiki/Null-terminated_string
 
Ritesh!!! A string compare is as simple as a number compare

Take "Password"

P = 80
a = 97
s = 115
s = 115
w = 119
o = 111
r = 114
d = 100

You can either check each number one by one
Or for a simple ( but not always correct ), solution. Add them all up.

The problem with the last solution is they can be out of order and still work..

The best way is to use strcmp(); in the string library

Parameters:
(const string1[], const string2[], bool:ignorecase, length)

string1 The first string to compare.
string2 The second string to compare.

ignorecase (optional) When set to true, the case doesn't matter - HeLLo is the same as Hello. When false, they're not the same.
length (optional) When this length is set, the first x chars will be compared - doing "Hello" and "Hell No" with a length of 4 will say it's the same string.

Returns -1 if string1 comes before string2
1 if string1 comes after string2
0 if the strings are the same (for the matched length).
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top