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

breaking up a char array

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







breaking up a char array
PostPosted: Sun Feb 27, 2005 7:05 pm     Reply with quote

Hello, I have been having alot of trouble breaking up a char array into component parts, for example my input to a pic is of the form "X:10;Y:10;" (w/o the quotations), however whenever i use the tokenizer i get garbage values, my code is below

------------------------------------------------------------------------------------

#include <18F458.h> //device
#device ICD=TRUE //allow for debugging with icd-u40
#device adc=10
#use delay(clock=40000000) //clock speed
#fuses NOWDT,HS
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=pc)
#use rs232(DEBUGGER)

#include <math.h>
#include <string.h>
#include <input.c>
#include <stdlib.h>

char Xval, Yval, ptr,str1,str2,str3,str4,str5,str6;
char delim[];
char commandstring[1], xNum[1], yNum[1];
Int index,indexx, check, rightcontent,error;
int16 xcoord=0, ycoord=0, sumcheck=0,sum=0,su=0;
int sum_check[];
char Checksum[3];
char *rxbuff;

void getCoords()
{
rxbuff=commandstring;
strcpy(delim,":;");
str1 = strtok(ptr,delim);
str2 = strtok(0,delim);
str3 = strtok(0,delim);
str4 = strtok(0,delim);

}

#int_RDA
void RDA_isr()
{
fgets(commandstring, pc);
//printf("%s\n", commandstring); //prints to interface
puts(commandstring); //prints to debugger monitor
getCoords();
}

void main()
{
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);

while(1)
{

}
}

------------------------------------------------------------------------------------

what am i doing wrong?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 27, 2005 9:41 pm     Reply with quote

There are several things about your program that could be improved.
For example, you're not using #INT_RDA in the proper way. There is
also no reason to use interrupts in your current program.

To answer your question about strtok():
The CCS manual has a good example of how use strtok().
I made it into a test program, as shown below, and it
displays the following output:

Code:

X
10
Y
10


Whenever you have a coding problem you should always make a
short test program that only tests that problem.

When you make a short program, it's much easier to study the
problem, and to solve it.
Code:
#include <16F877.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#include <string.h>
//--------------------------------------------

void main()
{
char *ptr;
char string[20];
char term[10];
   
strcpy(string, "X:10;Y:10;");
strcpy(term, ":;");
 
ptr = strtok(string, term);

while(ptr!=0)
   {
    puts(ptr);
    ptr = strtok(0, term);
   }

while(1);  // Prevent the PIC from going to sleep
}
Christian
Guest







PostPosted: Sun Feb 27, 2005 10:01 pm     Reply with quote

thank you programmer for ur post, however i have used the tokenizer sucessful in a program similar to urs above, but i cant get it work whenever i'm trying to tokenize an input from a pc, i get garbage.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 27, 2005 11:41 pm     Reply with quote

OK, I took a closer look at your program in that area, and there are
several things wrong with it. The major thing is that your input
array is defined with a length = 1 byte. There's no room in it
to store your input string. You need to define a larger array.

There are a few other things wrong with your program.
You're using HS mode in your #fuses, but you have 40 MHz
specified in #use delay(). That's not right. You should
have NOLVP specified in your #fuses, unless you're using
low voltage programming (which is rare).

I think you have the demo. Maybe you're just using the MPLAB
simulator. Then these fuse settings don't matter. So I won't
say any more about them.

Here is a very simple test program which just gets a string
from your terminal program, and then displays it when
you press the Enter key on your PC.

Code:
#include <18F458.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)       // 4 MHz crystal
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS, stream=PC)

//===================================
void main()
{
char command_string[80];   // Allow enough space for a long string.

fgets(command_string, PC);   

printf("Command string = %s \n\r", command_string);

while(1);
}
Christian
Guest







PostPosted: Sun Feb 27, 2005 11:53 pm     Reply with quote

hey programmer, not intending to be bashful but i have tried setting commandstring[30], but the code just wont work, and ur right i am using the hyperterminal, but i cant tokenize my inputs, does your code do this?
Thanks
Christian
Guest







PostPosted: Mon Feb 28, 2005 12:26 am     Reply with quote

Very Happy
problem solved

"You need to define a larger array"

thanks for that tip
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