CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

How to implement simple rs232 string receive

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
cap110874
Guest







How to implement simple rs232 string receive
PostPosted: Mon Jun 08, 2009 10:44 am     Reply with quote

Hi folks,
I am using rs232 (hardware on pic 18f2525) to receive single character commands from a pc.
This works very well but I would like this to work with simple strings if possible so I can change my single character commands to 3 characters.

Any suggestions on how to implement simple strings would be very welcome.

The code I have for single commands is
Code:
void pc_recv_check(void)
{
   char c;
   if (kbhit(STREAM_UART))
   {
      c = fgetc(STREAM_UART);
     
      if (c=='u')                                                                                     
         {
         Drive_Up();
         fprintf(STREAM_UART, "Up_Received");
         }
      if (c=='d')   
         {
         Drive_Down();
         fprintf(STREAM_UART, "Down_Received");
         }
      if (c=='s')           
         {
         Drive_Stop();
         fprintf(STREAM_UART, "Stop_Received");
         }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 08, 2009 10:51 am     Reply with quote

Look at the get_string() function. It's in this file:
Quote:

c:\program files\picc\drivers\input.c

It also has other input functions such as gethex(), get_int(), etc.
cap110874
Guest







Tring hard but not getting anywhere
PostPosted: Tue Jun 09, 2009 5:57 am     Reply with quote

I took a look at the input .c and came up with the following code, but this also doesnt seem to work. Any suggestions would be very welcome. My small brain is aching as I don't understand too much of the c language.
Code:

void pc_recv_check(void)
{
   #include <string.h>
   char string1[4], string2[4];     // define 2 strings 4 characters long
   if (kbhit(STREAM_UART))          // Check to see if there is anything on the UART     
      {
      strcpy(string2,"fred ");     //If there is data set string2 to hold value fred
      fgets(string1, STREAM_UART); // get the data from rs232 and place in string1
      if(strcmp(string1, string2)) //Compare 2 strings
        { 
        fprintf(STREAM_UART, "Password_OK\r\n ");  //Print ok if they match
        }
       else
           { 
           fprintf(STREAM_UART, "Password_BAD\r\n ");  // Print BAD if they dont match
           }
      }

}
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Tue Jun 09, 2009 6:08 am     Reply with quote

First of all you have an extra space after fred "fred "
Secondly the STRING "fred" is actually 5 characters long.
Code:
'f', 'r', 'e', 'd', '\0'

The '\0' is a null terminating character used to indicate the end of a string. You must allocate space for its storage
Code:

char string2[5];
strcpy(string2,"fred");

The same for getting a string from your input
Code:
char string1[5];
fgets(string1, STREAM_UART);

Your next problem is, is it possible for more chars to be entered ?
Is yes then you need to protect against it.
The easiest way is to have a larger buffer
Code:
char string1[80];
cap110874
Guest







still learning - long way to go i think
PostPosted: Tue Jun 09, 2009 6:48 am     Reply with quote

Thank you for your advice. This makes perfect sense when you word it like that. Just having problems getting it to compile.
Code:

#include <string.h>
char string1[80], string2[5];     // define 2 strings 4 characters long

   if (kbhit(STREAM_UART))          // Check to see if there is anything on the UART     
      {
      char string2[5];
      strcpy(string2,('f', 'r', 'e', 'd', '\0'));    //If there is data set string2 to hold value fred
      char string1[80];
      fgets(string1, STREAM_UART); // COMPILE ERROR asking to numeric expression
      if(strcmp(string1, string2)) //Compare 2 strings
        { 
        fprintf(STREAM_UART, "Password_OK\r\n ");  //Print ok if they match
        }
       else
           { 
           fprintf(STREAM_UART, "Password_BAD\r\n ");  // Print BAD if they dont match
           }
      }         
}
Ttelmah
Guest







PostPosted: Tue Jun 09, 2009 8:17 am     Reply with quote

Includes, in general, _must_ be outside of any function. So:
Code:

#include <string.h>
void function(void) {
   //your code
}


Remember an include works as if you type all it's code 'in' at the point you include it. If you put it inside a function, and it contains function definitions, then you will have definitions inside definitions - not legitimate....

Best Wishes
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

Re: still learning - long way to go i think
PostPosted: Tue Jun 09, 2009 8:43 am     Reply with quote

cap110874 wrote:
Thank you for your advice. This makes perfect sense when you word it like that. Just having problems getting it to compile.
Code:

#include <string.h>
char string1[80], string2[5];     // define 2 strings 4 characters long

   if (kbhit(STREAM_UART))          // Check to see if there is anything on the UART     
      {
      char string2[5];
      strcpy(string2,('f', 'r', 'e', 'd', '\0'));    //If there is data set string2 to hold value fred
      char string1[80];
      fgets(string1, STREAM_UART); // COMPILE ERROR asking to numeric expression
      if(strcmp(string1, string2)) //Compare 2 strings
        { 
        fprintf(STREAM_UART, "Password_OK\r\n ");  //Print ok if they match
        }
       else
           { 
           fprintf(STREAM_UART, "Password_BAD\r\n ");  // Print BAD if they dont match
           }
      }         
}

You declared string2[5] twice, once right after #include <string.h> and then once again after the if(kbhit) statement. You cannot declare variables in the middle of executable code.
cap110874
Guest







thank you for another bit of very useful knowledge
PostPosted: Tue Jun 09, 2009 8:50 am     Reply with quote

Thanks for your help guys. The code seems to be near but I am having problems with it locking up.
It seems to be stuck receiving rs232 because it doesn't pass to the last if statement. I thought this would just timeout after a few ms.
Code:

   #include <string.h>
   char string1[80];//, string2[5];     // define 2 strings 4 characters long
   if (kbhit(STREAM_UART))          // Check to see if there is anything on the UART     
      {
      char string2[5];
      strcpy(string2,('f', 'r', 'e', 'd', '\0'));    //If there is data set string2 to hold value fred
      fgets(string1, STREAM_UART);
      if(strcmp(string1, string2)) //Compare 2 strings
        { 
        fprintf(STREAM_UART, "Password_OK\r\n ");  //Print ok if they match
        }
       else
           { 
           fprintf(STREAM_UART, "Password_BAD\r\n ");  // Print BAD if they dont match
           }
      }
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Tue Jun 09, 2009 9:43 am     Reply with quote

See comments

Code:

#include <string.h>  // This at the top of your file somewhere !

void pc_recv_check(void)
{
  char string1[80], string2[5];

  if (kbhit(STREAM_UART))
  {
    strcpy(string2, "fred"); //This will automatically add '\0' to the end of your string and is neater
    fgets(string1, STREAM_UART);
    if(strcmp(string1, string2) == 0) // strcmp returns 0 if they are equal!
    {
      fprintf(STREAM_UART, "Password_OK\r\n "); //Print ok if they match
    }
    else
    {
      fprintf(STREAM_UART, "Password_BAD\r\n "); // Print BAD if they dont match
    }
  }
}


Please note fgets reads input until a return is encountered.
If your teminal program appends <cr><lf> (Linefeed) then this may be messing up your input.
cap110874
Guest







very very close
PostPosted: Tue Jun 09, 2009 10:15 am     Reply with quote

Yes your right, the code is now working well with your mods to simplify it.

I changed terminal programs and works well with a CReturn.

If i send CReturn everything works perfectly.

is there any way of stopping the code from locking up until it gets a cr.
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Wed Jun 10, 2009 2:08 am     Reply with quote

There is a way of stopping it from locking up. You can use the serial interrupt if it is hardware serial or polling if you are using software serial on a non interrupt port.

There are many examples and lots of info on how to do this in this forum so I will not be posting the code for it.

When you have got some code and you are having trouble with it then I am sure plenty of people on here will help you out.

Have fun.
cap1108742003
Guest







great help guys thank you
PostPosted: Wed Jun 10, 2009 10:06 am     Reply with quote

Thanks wayne and everyone else.

Im working on the locking up bit now but its getting very close. Ill post some code once i get it going but may take a few hours yet.

Thankyou all.........
sri.....
Guest







problem with line feed
PostPosted: Thu Jul 02, 2009 10:18 pm     Reply with quote

hi friends,

I tried using both get_string and gets. I am getting stuck with the carriage return and line feed combined, the code remains in INT_RDA interrupt all the time.

Please help me....
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Jul 02, 2009 11:28 pm     Reply with quote

I can't imagine, that gets() or get_string() can be a solution for the terminal input interface of typical embedded applications. Basically, because you can't leave the main loop waiting for a carriage return, that may come either next second or next day...

This input method is appropriate for a DOS style terminal application, that can afford to wait for user input, doing nothing else. In the embedded world, you have to read the terminal input to a buffer and start action, when a complete line has been received, indicated by the terminating CR.

There are different ways to achieve this behaviour, personally I prefer to have a interrupt driven circular buffer for UART receive, check this buffer for the terminating CR from the main loop and execute the input function, that extracts and interprets the next line from the buffer, conditionally. I don't want to discuss the details, just mention the general method.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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