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

Re: Array causing trouble ?

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







RS232 with 16C84
PostPosted: Tue Nov 06, 2001 2:17 am     Reply with quote

I have this problem with some code I wrote. It sends AT commands to a modem, the code works, it sends the commands and the getc() in the serial function works because I can put it into a temp variable and send it again. I have created an array called Buffer and use getc to place the characters into this array. For some reason it will not do this, the array stays empty ! Could someone please tell me what I am doing wrong !

#include <16c84.H>

#fuses HS,NOWDT,NOPROTECT

#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_B1, rcv=PIN_B0)

byte buffer[14]; //Buffer for number
byte in_byte = 0; //To increment Buffer

void input_byte(void)
{
buffer[in_byte]=getc(); //Read character into Buffer
in_bit++; //Increment array position
}

main()
{
int count = 0; //For reading in characters from modem

output_float(PIN_B0);
output_high(PIN_B1);

while(TRUE)
{

in_byte = 0; //Reset increment for Buffer

printf("AT+CMGD=1\r"); //Delete SMS in position 1
delay_ms(2000); //Delay for delete

printf("AT+CMGF=1\r"); //Setup SMS text mode
delay_ms(1000);
while(input(PIN_B0)); //Wait for SMS rec indication
delay_ms(1000);

output_high(PIN_B2); //Turn LED on to indicate SMS rec

printf("AT+CMGR=1\r"); //Read SMS from modem

while(input(PIN_B0)); //Wait for 1st bit

while(count < 36) //Read in bytes
{
if(count >= 22) //Read in required bytes depending on buffer size
input_byte(); //Call input function
else
getc(); //Discard not wanted bytes
count++;
}

count = 0; //Reset read in byte counter

delay_ms(3000); //Delay before send

printf("AT+CMGS="); //Send SMS
printf(buffer); //Send Buffer
delay_ms(1000);
printf("\r"); //End command
delay_ms(1000);
printf("New number is: "); //Message to send
printf(buffer); //Message to send
delay_ms(1000);
putc(0x1a); //Send control Z

delay_ms(5000); //Wait for messsage to be sent
} //Return to beginning to start process agian
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 965
Ian McCrum
Guest







Re: RS232 with 16C84
PostPosted: Tue Nov 06, 2001 3:18 am     Reply with quote

<font face="Courier New" size=-1>The write to buffer routine compiles and works for me.
YOu have mistyped the array increment instruction,

in_bit++;

should be

in_byte++;

</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 968
Willy
Guest







Re: RS232 with 16C84
PostPosted: Tue Nov 06, 2001 4:33 am     Reply with quote

:=<font face="Courier New" size=-1>The write to buffer routine compiles and works for me.
:=YOu have mistyped the array increment instruction,
:=
:=in_bit++;
:=
:=should be
:=
:=in_byte++;
:=
:=</font>

Hi Ian

That was a mistake which I was aware of, thankyou for pointing that out to me. The problem I am having is that the first array element gets filled with the value of 14 and is incremented throughout the array. I have tried loading the array with 0's first but it still happens. I simulate the modem using hyper terminal and print the array and that is what I got.

Regards
Willy
___________________________
This message was ported from CCS's old forum
Original Post ID: 971
Willy
Guest







Re: Array causing trouble ?
PostPosted: Tue Nov 06, 2001 5:49 am     Reply with quote

:=:=<font face="Courier New" size=-1>The write to buffer routine compiles and works for me.
:=:=YOu have mistyped the array increment instruction,
:=:=
:=:=in_bit++;
:=:=
:=:=should be
:=:=
:=:=in_byte++;
:=:=
:=:=</font>
:=
:=Hi Ian
:=
:=That was a mistake which I was aware of, thankyou for pointing that out to me. The problem I am having is that the first array element gets filled with the value of 14 and is incremented throughout the array. I have tried loading the array with 0's first but it is still replaced by 14 up . I simulate the modem using hyper terminal and print the array and that is what I got.
:=
:=Regards
:=Willy

Hi

I am using Mplab and I noticed that the array is filled with the values 14 up even before I run the program. If I declare another variable before the array its starts at 15 up etc, what could be causing this ?? Can anyone help.

Regards and Thanks
Willy
___________________________
This message was ported from CCS's old forum
Original Post ID: 973
Ian McCrum
Guest







Re: Array causing trouble ?
PostPosted: Tue Nov 06, 2001 7:47 am     Reply with quote

Willy, I only had time for a quick look at the machine code. The code to fill the buffer looks ok, but that area of memory may be getting corrupted by the printf(buffer) statement, which is a strange way to use printf by the way? do you input leading and trailing double quotes?

Does printf("\%s",buffer) work, IF THE LAST LOCATION IN BUFFER HOLDS A ZERO!!!!

I'm a bit new to the CCS compiler by the way...
Cheers Ian
___________________________
This message was ported from CCS's old forum
Original Post ID: 977
Willy
Guest







Re: Array causing trouble ?
PostPosted: Tue Nov 06, 2001 8:05 am     Reply with quote

Hi Ian
\%s does not work but \%c,\%0x,\%d work. I just don't understand way the array "buffer" is filled with the same values as the line numbers, because if I create more variables it moves the buffer array and it takes on the values of the new line numbers. This code was working at a stage but then I added some more and this started happening. Strange !!

Cheers
Willy
___________________________
This message was ported from CCS's old forum
Original Post ID: 978
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Re: RS232 with 16C84
PostPosted: Tue Nov 06, 2001 8:24 am     Reply with quote

Your buffer is not really a string is it? To send and array of bytes;

for(TempInt1= 0; TempInt1<= 14; TempInt1++)
{
putc(Buffer[TempInt1]);
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 979
Willy
Guest







Re: RS232 with 16C84
PostPosted: Tue Nov 06, 2001 8:46 am     Reply with quote

:=Your buffer is not really a string is it? To send and array of bytes;
:=
:=for(TempInt1= 0; TempInt1<= 14; TempInt1++)
:= {
:= putc(Buffer[TempInt1]);
:= }

The problem I am having is that the first array element gets filled with the value of 14 (same as line number)and is incremented throughout the array. I have tried loading the array with 0's first but it is still replaced by 14 up . I simulate the modem using hyper terminal and print the array and that is what I got.
I noticed the array "buffer" is filled with the same values as the line numbers, because if I create more variables it moves the buffer array and it takes on the values of the new line numbers. This code was working at a stage but then I added some more and this started happening. Using printf(in_buffer); worked, but no I have a problem placing the recieved data into the array because it gets filled by other data. Could it not be compiler problem ?

Regards
Willy
___________________________
This message was ported from CCS's old forum
Original Post ID: 980
Willy
Guest







Re: RS232 with 16C84
PostPosted: Tue Nov 06, 2001 8:53 am     Reply with quote

:=Your buffer is not really a string is it? To send and array of bytes;
:=
:=for(TempInt1= 0; TempInt1<= 14; TempInt1++)
:= {
:= putc(Buffer[TempInt1]);
:= }
The problem I am having is that the first array element gets filled with the value of 14 (same as line number)and is incremented throughout the array. I have tried loading the array with 0's first but it is still replaced by 14 up . I simulate the modem using hyper terminal and print the array and that is what I got.
I noticed the array "buffer" is filled with the same values as the line numbers, because if I create more variables it moves the buffer array and it takes on the values of the new line numbers. This code was working at a stage but then I added some more and this started happening. Using printf(in_buffer); worked, but now I have a problem placing the recieved data into the array because it gets filled by other data. Could it not be compiler problem ?

Regards
Willy
___________________________
This message was ported from CCS's old forum
Original Post ID: 981
Ian McCrum
Guest







Re: Array causing trouble ?
PostPosted: Tue Nov 06, 2001 4:06 pm     Reply with quote

<font face="Courier New" size=-1>:=Hi Ian
:=\%s does not work

. This code was working at a stage but then I added some more and this started happening. Strange !!

Oops, yes I checked the manual, \%s not available in version 2
(you do get it in version 3!).

I still think your printf'f look funny. I supppose you need to cut out lines until it resumes working... try removing all the printfs, or at least the printf(buffer) statements

As I said, fairly new to ccs C

Good luck
Ian
p.s if you get time have a go with my problem, earlier in the BBS... a bug in delay_us() I think</font>
___________________________
This message was ported from CCS's old forum
Original Post ID: 989
Tone
Guest







Re: Array causing trouble ?
PostPosted: Thu Nov 08, 2001 3:59 am     Reply with quote

Hello,

I thought that the printf(buffer) statement looked funny also. When ever you call a function that expects a string as a parameter, it MUST be NULL terminated. IE have a byte set to zero somewhere in the array. Not doing so will cause indeterminate results and is a probable cause of corruption elsewhere in memory.

My 2p worth.

Tone.
___________________________
This message was ported from CCS's old forum
Original Post ID: 1010
Tone
Guest







Re: Array causing trouble ?
PostPosted: Thu Nov 08, 2001 4:11 am     Reply with quote

I forgot to add (also for the benefit of Ian) that there is a probably a better way of sending strings to the serial port:

putc(buffer);

assuming you know the string to be NULL terminated of course :)

This is a Peculiarity of CCS C, in that if you pass a string to a function that expects a type char or int as a parameter, then the function gets repeatedly called with every element of the array until the NULL is reached.

Regards,

Tone.
___________________________
This message was ported from CCS's old forum
Original Post ID: 1011
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