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

Strange behavior, array of char not cleaning ...

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



Joined: 01 Mar 2008
Posts: 48

View user's profile Send private message

Strange behavior, array of char not cleaning ...
PostPosted: Thu May 15, 2008 8:47 am     Reply with quote

Hi guys I was trying to empty an array that was filled in my aplication but it doesnt fills, if i put another data over it, it works but if I just try to empty not happens ...

i've tryed that way:
Code:

   if (bReadMCard)
      {
       for (i=0;i<79;i++)
        Track1[i] = 0x00;

       for (i=0;i<40;i++)
        Track2[i] = 0x00;

       rMCardError = mcr_read(Track1,Track2);
       bReadMCard = False;     
      }


and that way to

Code:

   if (bReadMCard)
      {
       for (i=0;i<79;i++)
        *Track1[i] = 0x00;

       for (i=0;i<40;i++)
        *Track2[i] = 0x00;
       //Read the card again!
       rMCardError = mcr_read(Track1,Track2);
       bReadMCard = False;     
      }


Any tips wy this is happen ?

Thanks a lot guys,
Regards,
Diego Garcia.
se07860



Joined: 13 May 2008
Posts: 5

View user's profile Send private message

PostPosted: Thu May 15, 2008 8:52 am     Reply with quote

I don't have a great experience with this compiler but i've had some problems if i tried to write the program without {} also for 1 line.

I can't do much more for you.
DiegoGarcia



Joined: 01 Mar 2008
Posts: 48

View user's profile Send private message

PostPosted: Thu May 15, 2008 9:16 am     Reply with quote

This doesn't works to :/

I've tried with the memset function to but it don't work too !

The strange thing is that if I pass another card that has both the tracks it works it fills the array with the new data, but if after that I pass another card that has just one of the tracks, the array stills with the old value on the track that doesn't exist in the card ...

Any more tips ?

Thanks a lot guys,
Regards,
Diego Garcia
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu May 15, 2008 9:21 am     Reply with quote

The second method where you use '*' is wrong. The first method should work, but are you 100% sure the variable bReadMCard is set to TRUE?

What is the error code returned by mcr_read?
DiegoGarcia



Joined: 01 Mar 2008
Posts: 48

View user's profile Send private message

PostPosted: Thu May 15, 2008 9:26 am     Reply with quote

Yap, its true because I send the command using the usb so if I pass the other card that has both the tracks it reads ok !

I've tryed with many ways, none of then works !

I will try to shift the bits ! this should work , I hope!

Regards,
Diego Garcia
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu May 15, 2008 9:50 am     Reply with quote

Quote:
Yap, its true because I send the command using the usb so if I pass the other card that has both the tracks it reads ok !
This doesn't convince me that the variable bReadMCard is set to TRUE. From your code fragment this can not be concluded. Have you tried setting a breakpoint after the if-statement? Or, if you don't have a debugger can you toggle a LED?

I don't understand what you mean by 'to shift the bits'.
DiegoGarcia



Joined: 01 Mar 2008
Posts: 48

View user's profile Send private message

PostPosted: Thu May 15, 2008 10:03 am     Reply with quote

ok sorry for the bad explanation ckielstra,

I know that its entering because my to my program read an card I have to send the command 0x03 from usb so when the pic receives the command it sets the var to true or else It wouldnt be reading the other data from the new card, so its reading the main problem is just that it reads the track contained in the card ok, but if the first card passed was filled with both tracks 1 and 2 and the new card just has the track 2 for example, when I send all the data to PC, the track1 is comming filled with the data from the first card ...

And shifting the bits doesnt works ...

I've just called shift the bits doing that

Code:
int clear_tracks(char* track1, char* track2)
{
 int i1, i2;

 if(track1 != 0)
 { 
  for (i1=0;i1<79;i1++)
   {
    while (i2<=7)
     {
       shift_right(Track1+i1, 1, 0);
       i2++;
     }
   }
 }

 if(track2 != 0)
 {
  for (i1=0;i1<40;i1++)
   {
    while (i2<=7)
     {
       shift_right(Track2+i1, 1, 0);
       i2++;
     }
   }
 }
}


Thank you very mutch for your great help guys ;)
Regards,
Diego Garcia.
DiegoGarcia



Joined: 01 Mar 2008
Posts: 48

View user's profile Send private message

PostPosted: Thu May 15, 2008 10:26 am     Reply with quote

Ok ckielstra now just to be sure I've put it an break point on it and debuged with my ICD2, and its entering ...

I dont know why is this happening ...

Any more tips ?

Thanks a lote,
Regards,
Diego Garcia
Ttelmah
Guest







PostPosted: Thu May 15, 2008 10:30 am     Reply with quote

Well, that won't work, for so many reasons, that it rather increases my belief that Ckielstra is dead right, and your code never calls the original routine.
You test 'Track1' for being zero - remember it is the _address_ of the data now, not the data itself. It can never be zero, unless you set it to NULL. Then you never reset the i2 counter, so the shift will only ever execute on the first byte. It'll never execute at all for Track2.

Best Wishes
DiegoGarcia



Joined: 01 Mar 2008
Posts: 48

View user's profile Send private message

PostPosted: Thu May 15, 2008 10:39 am     Reply with quote

Nice Ttelmah ;) doesnt noted that i2 wasnt being reseted.

Now it works ;)

But it just works that way !

Code:

int clear_tracks(char* track1, char* track2)
{
 int i1, i2;

 if(track1 != 0)
 { 
  for (i1=0;i1<79;i1++)
   {
    i2=0;
    while (i2<=7)
     {
       bit_clear(*(Track1+i1), i2);
       i2++;
     }
   }
 }

 if(track2 != 0)
 {
  for (i1=0;i1<40;i1++)
   {
    i2=0;
    while (i2<=7)
     {
       bit_clear(*(Track2+i1), i2);
       i2++;
     }
   }
 }
}


Thanks for the great observation ;)
Regards,
Diego Garcia
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu May 15, 2008 10:51 am     Reply with quote

Code:
    i2=0;
    while (i2<=7)
     {
       bit_clear(*(Track1+i1), i2);
       i2++;
     }
equals:
Code:
Track1[i1] = 0;


The whole function can be reduced to:
Code:
int clear_tracks(char* track1, char* track2)
{
  if(track1 != 0)
    memset(track1, 0, 79);

  if(track2 != 0)
    memset(track2, 0, 40);
}


Code:
  if(track1 != 0)
Dynamic memory allocation in an embedded PIC is unlikely, so I guess track1 always points to a valid address. More likely is that you wanted to check the first byte of the array being zero? Than it should have been:
Code:
  if(*track1 != 0)
DiegoGarcia



Joined: 01 Mar 2008
Posts: 48

View user's profile Send private message

PostPosted: Thu May 15, 2008 10:57 am     Reply with quote

Ok let me try that way to see the results !

Thanks ckielstra ;)

Regards,
Diego Garcia
DiegoGarcia



Joined: 01 Mar 2008
Posts: 48

View user's profile Send private message

PostPosted: Thu May 15, 2008 11:00 am     Reply with quote

Works like a charm ;)

Thanks I had done that before but i didnt noted that the memset uses the size of the array .. :/

Thank you very mutch ckielstra and Ttelmah !

Regards,
Diego Garcia
DiegoGarcia



Joined: 01 Mar 2008
Posts: 48

View user's profile Send private message

PostPosted: Thu May 15, 2008 11:06 am     Reply with quote

Now my mcr.c driver is reading in both directions track1 and 2 and working like a charm Smile

Thanks for your help guys ;)

Regards,
Diego Garcia
mpardinho



Joined: 16 Jul 2008
Posts: 8
Location: Mexico

View user's profile Send private message

mcr
PostPosted: Sun Nov 02, 2008 5:08 am     Reply with quote

your code is free(for share) ?
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