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 CCS Technical Support

Split string from serial data(RS232)?

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



Joined: 07 Apr 2015
Posts: 3

View user's profile Send private message

Split string from serial data(RS232)?
PostPosted: Tue Apr 07, 2015 12:23 pm     Reply with quote

Hello

I'm new for programing , I want check data from serial port PC to PIC
my condition
1. PC set data to PIC => load cell value 125 kg. in "WV000125" format
2.PIC check data if "WV" print data "000125"

can you help me, please

my code.

Code:
#include <16f628A.h>
#fuses NOWDT,HS, NOPUT, NOPROTECT,NOLVP,BROWNOUT
//#use delay (clock=12000000)
#include <string.h>
#use delay (clock=18432000)
#use rs232(baud=19200,parity=N,xmit=PIN_B2,rcv=PIN_B1,bits=8)
//================================
void main()
{

   char string[30];
   char name[30];
   strcpy(name, "WV");
   while(1)
   
      gets(string);   
      if(strcmp(string, name))
      {
       ' get string to array
               }
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Apr 07, 2015 12:33 pm     Reply with quote

Quote:
if(strcmp(string, name))

Strcmp() doesn't return TRUE if there is a match. It returns 0.
Always read the function description.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Apr 07, 2015 4:04 pm     Reply with quote

add the ERRORS argument to your #use rs232 declaration.

also GETS() is a high risk call since there is no way in your program to break out of the while loop if no CARRIAGE return is received or if transmission stops for any other reason.

You are assuming that everything will happen "just so"as you imagine in your program -- when in fact the real world may intrude and hang it -- Very Happy Very Happy Very Happy
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Wed Apr 08, 2015 7:46 am     Reply with quote

CCS provides sample code (eg EX_STR.C) for handling string functions and RS232.

Mike
kodomo32



Joined: 07 Apr 2015
Posts: 3

View user's profile Send private message

PostPosted: Wed Apr 08, 2015 11:40 am     Reply with quote

Thanks for many advise, Now i can check data from serial port
but code is long, is there anyway to write to short form.
Thanks
Code:
char a[7],char C;
int i2,i3;
C=getc();
if(C=='W') {   // Check data 'W"
      a[0]=C;
      C=getc();
      if (C=='V'){// Check data 'V'
      a[1]=C;
      C=getc();
      a[2]=C;
      C=getc();
       a[3]=C;
      C=getc();
      a[4]=C;
      C=getc();
      a[5]=C;
       C=getc();
      a[6]=C;
      C=getc();
      a[7]=C; }
    else {   // clear data
          for (i2=0;i2<=7;++i2)
                  a[i2]="";
                        }
     }
   else { //clear data
            for (i2=0;i2<=7;++i2)
               a[i2]="";
            }
     for (i3=2;i3<=7;++i3)
           printf ("%c",a[i3]);// Print "000125"
      }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19498

View user's profile Send private message

PostPosted: Wed Apr 08, 2015 12:26 pm     Reply with quote

This look a lot more complex, but is doing a lot more...
Code:

const char lookfor[]="WV";
#define ISNUM(x) (x>='0' && x<='9')

int1 look(void)
{
   char numbers[7], tempchr;
   int state=0;   
   do
   {
      tempchr=getc();
      switch (state)
      {
      case 0:
      case 1:
        if (tempchr==lookfor[state])
           state++
        else
           return FALSE; //failed not WV
        break;
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
         if (ISNUM(tempchr))
         {
            numbers[state-2]=tempchr;
            state++;
         }
         else
            return FALSE; //not a number
         break;
      case 7:
         if (ISNUM(tempchr))
         {
            numbers[state-2]=tempchr;
            numbers[state-1]='\0'; //null terminate the number
            printf("%s",numbers); //print the numbers
            return TRUE;
         }
         return FALSE; //not a number
      }
   }
}


This looks for the "WV", and returns 'FALSE' if this is not found, or if the 'digits' arriving afterwards are not digits. Returns TRUE if it all works.
However several things it does could be used to simplify your version if you didn't want to go so far. For instance, printing the numbers as a string by just adding the terminator, just searching for the text from a string, rather than character by character etc..
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