View previous topic :: View next topic |
Author |
Message |
kodomo32
Joined: 07 Apr 2015 Posts: 3
|
Split string from serial data(RS232)? |
Posted: Tue Apr 07, 2015 12:23 pm |
|
|
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
|
|
Posted: Tue Apr 07, 2015 12:33 pm |
|
|
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
|
|
Posted: Tue Apr 07, 2015 4:04 pm |
|
|
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 -- |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Apr 08, 2015 7:46 am |
|
|
CCS provides sample code (eg EX_STR.C) for handling string functions and RS232.
Mike |
|
|
kodomo32
Joined: 07 Apr 2015 Posts: 3
|
|
Posted: Wed Apr 08, 2015 11:40 am |
|
|
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: 19559
|
|
Posted: Wed Apr 08, 2015 12:26 pm |
|
|
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.. |
|
|
|