|
|
View previous topic :: View next topic |
Author |
Message |
Andre-Pretorius
Joined: 09 Mar 2010 Posts: 9 Location: South Africa, Klerksdorp
|
RS232 receive problem |
Posted: Mon Apr 05, 2010 3:46 am |
|
|
Hi there
I am trying to create a function that will receive a string of char from the serial port, and check if the phrase "OK" is present. If nothing is received or the phrase is not present it should return false. My demo just send out "hallo" and then waits for a feedback toggling two LED's accordingly.
Can someone maybe tell me what I am doing wrong as nothing seems to happen after the hallo has been transmitted.
Code: |
#include <config.h>
char string[60];
int i=0;
int counter_search=0;
int timeout;
char Get_OK()
{
counter_search=0;
timeout=0;
while(!(kbhit())&&(++timeout<200000))
delay_us(10);
if(kbhit())
{
while(kbhit()) //read data from serial port to char array
{
string[i]=getc();
i=i+1;
}
while((string[counter_search]!="O")&&(counter_search<69)) // wait till 'O' indicator is found 'O'
{
counter_search++;
}
if (string[counter_search+1] = "K") //Check if next char is 'K'
{
return(TRUE); //return true if 'OK' is received
}
else
{
return(FALSE); //return false if 'OK' is not in received area
}
}
else
{
return(FALSE); //return false if nothing is received in 2 sek.
}
}
void main()
{
//setup has been removed to save space
while(true)
{
i=0;
DELAY_MS (1000);
printf("hallo");
if(Get_OK())
{
output_high(PIN_D4);
output_low(PIN_D5);
}
else
{
output_low(PIN_D4);
output_high(PIN_D5);
}
}
}
|
_________________ Andre Pretorius |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon Apr 05, 2010 4:28 am |
|
|
You arrive in 'get_OK'.
You loop waiting for kbhit to go true.
It does, saying that just _one_ character has arrived.
You then do a 'while' loop, reading characters, till kbhit goes false.
Problem is that as soon as you have read the one character that has arrived, kbhit _will_ go false, so you drop out of this loop....
At this point, you have got just _one_ charcacter. Not what you want.
You also potentially overshoot the end of your character array. Counter search is allowed to go to 69, when the array only reached 59...
Then, '''timeout', needs to be an int32, not an int, to hold a value of 200000.
Then it is easier to look for 'K', and check if the previous character was 'O', since you have nothing otherwise marking the end of the incoming data (it is more 'normal' to look for the 'line feed', and then search.
Something like:
Code: |
char string[60];
int1 GetOK(void) {
int1 found=false;
int i=0;
int32 timeout=0;
for (i=0;i<59;i++) { //Loop waiting
while(!(kbhit())&&(++timeout<200000)) delay_us(10);
if (!kbhit()) break; //exit for timeout
//Here one character has arrived
string[i] = getc()
if (string[i]=='K') {
//Here we _may_ have 'OK'
if (i<1) continue; //Have only received one character - carry on
if (string[i-1]=='O') { //If _previous_ character was 'O'
found=true;
break;
}
}
}
return found;
}
|
Depending on your processor speed, you have perhaps 2.5 seconds after the 'hello' is sent, to type _both_ 'O', and 'K'. Remember also that as written, it will require these to be upper case.
Best Wishes |
|
|
Andre-Pretorius
Joined: 09 Mar 2010 Posts: 9 Location: South Africa, Klerksdorp
|
|
Posted: Mon Apr 05, 2010 4:52 am |
|
|
Thank you, it works perfect _________________ Andre Pretorius |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|