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

Problem with string copy function (strcpy)

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



Joined: 25 Aug 2009
Posts: 175

View user's profile Send private message Yahoo Messenger

Problem with string copy function (strcpy)
PostPosted: Wed Mar 27, 2013 7:05 pm     Reply with quote

Hi all.
I have a problem when using strcpy to copy one string to another string.
In my project used :
-- CCS C PCH ver 4.140
-- Pic18LF4680

And here is my code:
0. setup fuses, init variables
Code:

#include <18LF4680.h>
#include <def_18f4680.h>
#fuses HS,NOPROTECT,NOLVP,NOWDT
#device *=16 ADC=10
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)
#priority rda,timer0,timer1
#use fast_io (B)
#use fast_io (D)

#include <LCD_TM.c>
#include <font.c>
#include <matrix_control.c>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

unsigned int8 str_rs232[16]="";
unsigned int8 str_mod[16]="";
unsigned int8 str_pla[16]="";

unsigned int1 flag_rs232=0;


1. Interrupt rda code:
Code:

#int_rda
void ngat_rs232()
{
   char c;
   c=getc();
      switch(c)
      {
     
         case '$':
            {
               index=0;               
            }
         break;
         //------------------------
         
         case '*':
            {
               if((str_rs232[0]=='M')&&(str_rs232[1]=='O')&&(str_rs232[2]=='D'))
               {
                  index=0;
                  flag_mod=1;
               }
            }
         break;
         //------------------------- 
         
         case '#':
            {
               if((flag_mod==1)&&(index==16))
               {
                  flag_mod=0;
                  index=0;
                  flag_rs232_ok=1;
               }         
            }
          break;
         //-------------------------
         
         default:
            {
               str_rs232[index]=c;
               index++;
            }
         break;
     
      }  // end of switch     
}        // end of int_rda



2. And main program:
Code:

void main(void)
{

   trisb=0;
   trisd=0;
   trisc=0x80;
   trise=0;
   trisa=0;
   

   enable_interrupts(int_rda);
   //enable_interrupts(int_timer0);
   //enable_interrupts(int_timer1); 
   enable_interrupts(global);
   //setup_timer_0 (RTCC_DIV_256| RTCC_INTERNAL);
   //set_timer0(200);

   //setup_timer_1 (T1_DIV_BY_8| T1_INTERNAL);
   //set_timer1(3035);   
   

   //buffer_full=0;
   
   printf("*******************************************************\n\r");
   printf("* Nhap chuoi ki tu\ so can hien thi                   *\n\r");
   printf("* Bat dau chuoi bang ki tu : *                        *\n\r");
   printf("* Ket thuc chuoi bang ki tu : Enter                   *\n\r");
   printf("*******************************************************\n\r");

   while(1)
   {

        if(flag_rs232)
       {
              strcpy(str_mod,str_rs232);
              printf("%s",str_mod);
              flag_rs232=0;
       }
     

   } // end of while   
}     // end of main


My problem is in copy "str_rs232" string to "str_mod" string is errors.
If i do follow after , that is right:
Code:

        if(flag_rs232)
       {
              //strcpy(str_mod,str_rs232);
              printf("%s",str_rs232);
              flag_rs232=0;
       }

Pls show me errors in my code and show me a way to fix it.
thanks all.
Regards.

p/s: my code was shorted, only extract some necessary for my problem Smile
_________________
Begin Begin Begin !!!
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Thu Mar 28, 2013 12:26 am     Reply with quote

The interrupt function doesn't build correct strings because it misses to write a delimiting null character. It also might write beyond the string limits.

The usual solution is to write received data to a circular buffer and do all further processing on the main level.
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