View previous topic :: View next topic |
Author |
Message |
PA_papa_wolf
Joined: 11 Nov 2019 Posts: 9
|
strcmp() and UART no work [Solved] |
Posted: Mon Nov 11, 2019 8:16 pm |
|
|
I perform the uptake of the character.
However, it does not move.
What is wrong with the program that I made?
Code: |
#include <12F615.h>
#device ADC = 10
#include <string.h> //strcmp()
#Fuses NOMCLR,PROTECT
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_A5, rcv=PIN_A3, PARITY=N, BITS=8, PARITY=N)
//#use rs232(baud=9600, xmit=PIN_A5, rcv=PIN_A3, PARITY=N, BITS=8, PARITY=N, STREAM=upper)
char string; //UART
char s1 = "A";
char s2 = "H";
void main(){
while(True){
if(kbhit()){
GETS(string);
if(strcmp(string, s1) == 0){
printf("1\n");
}
else if(strcmp(string, s2) == 0){
printf("2\n");
}
else{
printf("3\n");
}
}
else{
printf("4\n");
}
}
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Mon Nov 11, 2019 11:45 pm |
|
|
The first big problem is you have nowhere to put 'string'...
In C a 'string' is an array of characters. You need an array to hold this.
You are allocating _one_ character of storage. No good.
Then gets, expects to receive the address where it is to put the
'string'. Currently you are giving it the contents of the variable.
So the actual 'string' is being written 'somewhere' into memory, and
potentially overwriting other things in memory.
Then 'strcmp', only returns '0', if the two strings are the same. So
you would have to be typing A<CR> or B<CR> to work.
Then as written you will receive continuous 4<LF> characters sent
'flat out'. 480 pairs per second, so long as nothing is being received.
You are not going to have much chance of seeing the real reply in
this huge stream. You might want to slow this.
Declaration of 'string' needs to be something like:
char string[16];
Make the count large enough to hold anything you may send.
Remember to allow for the terminator character.
Then in C, the name of the array can be used as it's address. So will
work for the functions needing an address. |
|
|
PA_papa_wolf
Joined: 11 Nov 2019 Posts: 9
|
|
Posted: Tue Nov 12, 2019 2:49 am |
|
|
I tried your instructions, but there was not a reaction by the terminal software.
I think that 3 or 4 appears, but terminal software does not have a reaction.
Where will I get a wrong? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Nov 12, 2019 2:56 am |
|
|
These are not strings.
Quote: | char s1 = "A";
char s2 = "H"; |
To make them be strings, you have to add square brackets:
Code: | char s1[] = "A";
char s2[] = "H"; |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Tue Nov 12, 2019 3:17 am |
|
|
Lets just explain on a little more.
Again in C a 'string' is an array of characters. In the case of your s1, it'll
actually have to be a character 'A', followed by another character '\0'.
This is the 'NULL terminator', that all 'strings' in C have to have.
String functions are dependant on this existing.
Then if you use a variable 'fred', and put this into a function, it is the
_value_ of this that is passed to the function. However if you declare an
array, and then use the name of this in a function, C instead passes the
_address_ of the array.
So with your current declaration the number '65' is being passed to strcmp,
which then tries to use this as the address in memory where it expects to
find a string. What it will actually 'find' is completely indeterminate..... :(
So just as for 'string', _all_ 'strings' need to be declared as arrays.
Now in the case of 'string', you have to use a 'size' to tell it how many
characters it actually needs to allocate. In the case of the test values
though, you can just use the empty brackets, and the compiler will
automatically allocate the two characters needed to hold these values. |
|
|
PA_papa_wolf
Joined: 11 Nov 2019 Posts: 9
|
|
Posted: Tue Nov 12, 2019 5:17 am |
|
|
I'm sorry, I cannot understand your explanation.
Because I understand your explanation, what should I check?
I look at the site of the native language, and it wants to be understood.
But there is little available information about the compiler of CCSC. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Nov 13, 2019 1:42 pm |
|
|
He means do this:
Code: |
char string[16];
char s1[] = "A";
char s2[] = "H";
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Wed Nov 13, 2019 1:49 pm |
|
|
I was trying to explain 'why' we need a size on the first line and not
on the others. |
|
|
PA_papa_wolf
Joined: 11 Nov 2019 Posts: 9
|
|
Posted: Sun Nov 17, 2019 7:45 pm |
|
|
Hi all
I found damage to the test PIC.
Maybe I need buying a new PIC.
Thank you |
|
|
|