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 CCS Technical Support

capturing response from external device with uart

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



Joined: 24 Feb 2004
Posts: 11

View user's profile Send private message

capturing response from external device with uart
PostPosted: Mon Dec 10, 2007 4:00 pm     Reply with quote

I have an external device that I send a command via the uart and need to capture its response. I initially used hyperterminal to communicate with the device and view the response.

Now I need to do this with a PIC18F452. Here is a segment of the code that does not capture the response.

char response[100];
int i;


// send my command and wait on the response with the following code:

while(kbhit())
delay_us(5);
while(i < 256)
response[ i++] = getc();


CLEAR_DISP;
LCD_position (FIRST_LINE);
for(i = 0; i <200; i++)
printf(LCD_char,"%c", response[i]);


Could someone explain what is wrong with this code or if there is a better way to accomplish the task of capturing a long response of characters.

Thank you,
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Dec 10, 2007 5:16 pm     Reply with quote

There are several things wrong with it.

One major problem is that your array is only 100 bytes, but then
your loops expect the array to be 200 or 256 bytes long. That's
not going to work. If you write past the end of your array, you
will destroy the contents of other variables that are allocated in
those memory locations.

I suggest that you use the get_string() function to get the input.
It allows you to set the maximum number of characters that
you are willing to accept. This means you prevent your code
from writing past the end of your input array. The get_string()
function is in the Input.c file, which is in this directory:
Quote:
c:\Program Files\picc\Drivers\Input.c
Guest








PostPosted: Tue Dec 11, 2007 2:49 pm     Reply with quote

Thank you for the feedback. I will look at input.c.
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