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

gets()problem.

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 12, 2006 10:08 am     Reply with quote

How long is the string that you are receiving ? You have a very small
buffer. There's little margin for error.

You need a buffer that has room for:

1. Your message.
2. The CR at the end of the message.
3. Finally, you need room for the 0x00 byte, which marks the
end of the string.

Also, if your incoming message happens to be larger than the intended
length (for any reason), you will blow past the end of the buffer and
trash some of your other variables. I suggest using a larger buffer,
just for safety reasons.

-------

I'd like to add that using gets() is very unsafe. If you miss the <CR>
character, you likely blow past the end of the buffer and clobber all
your variables. At at minimum, your program will behave erratically.
It will likely crash. CCS has a get_string() function in INPUT.C,
which is in this folder: c:\Program Files\Picc\Drivers
It has a parameter which sets the maximum number of characters
to accept. It won't let you blow past the end of the buffer. It also
has a backspace feature in the code. You don't really want that
feature for a program that's taking in messages. You should edit that
part out of it.
Markdem



Joined: 24 Jun 2005
Posts: 206

View user's profile Send private message Send e-mail

PostPosted: Mon Feb 13, 2006 2:44 am     Reply with quote

Thanks for that PCM programmer. I am not very good at C programing, so the next question i ask my sound a bit dumb :(. All i need to transfer is 1 int8 and it will always be positive. I dont realy want to transfer a string because i need to convert it to a int later anyway. Is there maybe a easer way of what i am doing here??. If not, i will just make a buffer to buff the incoming string.

Thanks, Mark
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 13, 2006 2:56 am     Reply with quote

Quote:
Problem is that the when i use the following code, the PIC will
never detect a char 13.
All i need to transfer is 1 int8 and it will always be positive


If all you need to do is to send one 8-bit integer value, then using
getc() to receive it is fine. Example: If one PIC sends 0x55 to
the other PIC, that's only one byte, and getc() will get it.
Markdem



Joined: 24 Jun 2005
Posts: 206

View user's profile Send private message Send e-mail

PostPosted: Wed Feb 15, 2006 3:05 am     Reply with quote

Hi All, i feel so stupid asking this. I cant belive how much i am written in this project without knowing this. All i need to go is send a int8 from pic to pic using rs232 ( i would like to use i2c, but i dont know how). I know that if i do printf("%U",int8number) it will sent the ascii code to the other pic. What should i be doing. My cod now looks like this,

PIC1


Code:

#use rs232(baud=2400,parity=N,bits=8,xmit=PIN_C0,rcv=PIN_C1)
      
      printf("1\r");
      
      while(!kbhit()&&(++timeout<50000))
         {
            delay_us(5);
         }
      if(kbhit())
         {   
            
            currentPH = getch();
         }
      else
         {
            output_high(reset_adc);
         }

      while(!kbhit()&&(++timeout<50000))
         {
            delay_us(5);
         }
      if(kbhit())
         {   
            currentKH = getch();
         }
      else
         {
            output_high(reset_adc);
         }
   
      while(!kbhit()&&(++timeout<50000))
         {
            delay_us(5);
         }
      if(kbhit())
         {   
            currentCO2 = getch();
         }
      else
         {
            output_high(reset_adc);
         }

      while(!kbhit()&&(++timeout<50000))
         {
            delay_us(5);
         }
      if(kbhit())
         {   
            currenttemp = getch();
         }
      else
         {
            output_high(reset_adc);
         }

      while(!kbhit()&&(++timeout<50000))
         {
            delay_us(5);
         }
      if(kbhit())
         {   
            currenthoodtemp = getch();
         }
      else
         {
            output_high(reset_adc);
         }


PIC2
Code:

   data = 69;

   while (TRUE)
      {
         gets(serialreq);
         printf("%u",data);
         printf("%u",data);
         printf("%u",data);
         printf("%u",data);
         printf("%u",data);
      }


in the above code, data will be 5 diffrent vars later. This is only for testing. How should i be sending the data without useing atio()??

Thanks so much for your help. I am soooo close to finnishing this thanks to you guys

Mark Demczuk
Mattr0



Joined: 27 Mar 2005
Posts: 30

View user's profile Send private message Visit poster's website

PostPosted: Wed Feb 15, 2006 6:11 am     Reply with quote

Code:
      
      {                  
               gets(data);
               printf("recived = %u\r\n",data);
            }
   }


markdem I notice that the printf comment is not properly used in this case
it should be
Code:

printf("recived = %s\r\n",data);


This will make it print out the whole string. Not sure what it will do above if it will compile at all
Ttelmah
Guest







PostPosted: Wed Feb 15, 2006 7:42 am     Reply with quote

I think it'll print the LSB of the address of the string!. The name of a string, is a shortcut for it's address. The address is a short integer value on a 16x chip, or a long integer is '*=16' is used in the configuration, or with the 18x chips. %u, expects a 8 bit integer, so it'll probably print the LSB of the long address form.

Best Wishes
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