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

Having problems receiveing strings over RS232
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
refa



Joined: 12 Dec 2003
Posts: 15

View user's profile Send private message MSN Messenger ICQ Number

Having problems receiveing strings over RS232
PostPosted: Sun Jan 11, 2004 6:03 am     Reply with quote

HI group,

what I want to do is to receive a string of known characters over the RS232, 9600 bauds.

Here is my code, I made it so when I receive it I send it back to my PC to confirm the received.

Code:

#include <16f84a.h>
#include <stdlib.h>

#fuses XT, NOWDT, NOPROTECT
#use delay(clock=4000000)

#use rs232(baud=9600, xmit=PIN_B1, rcv=PIN_B0, PARITY=N, BITS=8, invert)

#include <input.c>

void main()
{
char stringonja[5];

 while(PIN_B0 == 0){
 }
 while(PIN_B0 == 1){
 }
 printf("test");
 
 while(TRUE){
 get_string(stringonja, 5);
 printf(stringonja);

 }
}



The while(PIN_B0 == 0) and while(PIN_B0 == 1){ are made for synchronization and the sender does it too(send the 1 and 0) and I get the string "test" on my PC.

So the problem is in the get_string command because I send the string with another pic printf ("there") and on the PC I don't get anything(just "test")??

When I send the string "there" manualy like printf("t"), printf("h"), printf("e"), printf("r"), printf("e") I receive the string and see it on PC("testhere"). This way it works.

Where is my problem? May it be because I use a PIC on 4 MHz? I hope you can help me, please.

Thank you very much
Ttelmah
Guest







Re: Having problems receiveing strings over RS232
PostPosted: Sun Jan 11, 2004 7:08 am     Reply with quote

refa wrote:
HI group,

what I want to do is to receive a string of known characters over the RS232, 9600 bauds.

Here is my code, I made it so when I receive it I send it back to my PC to confirm the received.

Code:

#include <16f84a.h>
#include <stdlib.h>

#fuses XT, NOWDT, NOPROTECT
#use delay(clock=4000000)

#use rs232(baud=9600, xmit=PIN_B1, rcv=PIN_B0, PARITY=N, BITS=8, invert)

#include <input.c>

void main()
{
char stringonja[5];

 while(PIN_B0 == 0){
 }
 while(PIN_B0 == 1){
 }
 printf("test");
 
 while(TRUE){
 get_string(stringonja, 5);
 printf(stringonja);

 }
}



The while(PIN_B0 == 0) and while(PIN_B0 == 1){ are made for synchronization and the sender does it too(send the 1 and 0) and I get the string "test" on my PC.

So the problem is in the get_string command because I send the string with another pic printf ("there") and on the PC I don't get anything(just "test")??

When I send the string "there" manualy like printf("t"), printf("h"), printf("e"), printf("r"), printf("e") I receive the string and see it on PC("testhere"). This way it works.

Where is my problem? May it be because I use a PIC on 4 MHz? I hope you can help me, please.

Thank you very much


Before looking anywhere else, learn how strings are stored in C!...
A string, allways requires _one more character_ of storage, than it's length, to hold the 'null' 'end of string' character.
You are asking the program to receive a five character string, which will require six storage locations, but are only assigning five storage locations for this.

Best Wishes
refa



Joined: 12 Dec 2003
Posts: 15

View user's profile Send private message MSN Messenger ICQ Number

PostPosted: Sun Jan 11, 2004 7:18 am     Reply with quote

Ttelemah, thank you. I got it but wasnt the point of my problem :(!

Thank you anyways Laughing
refa



Joined: 12 Dec 2003
Posts: 15

View user's profile Send private message MSN Messenger ICQ Number

PostPosted: Sun Jan 11, 2004 11:21 am     Reply with quote

Here is the new code for receiver
Code:

#include <16f84a.h>
#include <stdlib.h>

#fuses HS, NOWDT, NOPROTECT
#use delay(clock=20000000)

#use rs232(baud=9600, xmit=PIN_B1, rcv=PIN_B0, PARITY=N, BITS=8, invert)

#include <input.c>
void main()
{
char stringonja[10];
int a;

 while(PIN_B0 == 0){
 }
 while(PIN_B0 == 1){
 }

 get_string(stringonja,10);

 for (a=0; a<10; a++){
 printf(stringonja[a]);
 }

}

here is for the transmitter
Code:


#include <16f84a.h>
#include <stdlib.h>

#fuses XT, NOWDT, NOPROTECT

#use delay(clock=4000000)


#use rs232(baud=9600, xmit=PIN_A1, rcv=PIN_A0, PARITY=N, BITS=8, invert)

void main()
{

 DELAY_MS(3000); //optional :)
 output_high(PIN_A1);
 delay_ms(5);
 output_low(PIN_A1);

 printf("R");
 delay_ms(2);
 printf("r");
 delay_ms(2);
 printf("o");
 delay_ms(2);
 printf("k");
 delay_ms(2);
 printf("s");
 delay_ms(2);
 printf("i");
 delay_ms(2);
 printf("t");
 delay_ms(2);
 printf("z");
 delay_ms(2);
 printf("l");
 delay_ms(2);

}

Now I get the 9 bytes but the first one is always wrong, everytime different? Can somebody tell my why, please? Thank you Smile
refa



Joined: 12 Dec 2003
Posts: 15

View user's profile Send private message MSN Messenger ICQ Number

PostPosted: Sun Jan 11, 2004 12:13 pm     Reply with quote

I get an S insteed of R all the time(for the first byte)!! Hmm, any idea friends?

Thank you Laughing
Ttelmah
Guest







PostPosted: Sun Jan 11, 2004 3:46 pm     Reply with quote

refa wrote:
I get an S insteed of R all the time(for the first byte)!! Hmm, any idea friends?

Thank you Laughing

You are still calling the 'get_string' function, with the same number of bytes defined as the size of the string.
The _max_ number in get_string, must be no more than one below the number of characters in the storage space you define. Again this is standard C.
Also the get_string function, will return when the 'max' limit is reached, or with a 'newline' character, but if this is received, this is stored.
You do not need all the 'delay_ms' calls in the transmitter, _but_ you do need the one at the end (or a slightly longer one, or a 'run off the end' trap). The 'main' function, does not loop on it's own, but will run off the end of the code. At this point, the compiler puts a 'sleep' function, hence if you send (say):

main {
printf("Test string");
}

The last two characters of the string will be lost when the chip goes to sleep. This can result in garbage characters.

Coding as:

main {
printf("Test string");
while (true) ;
}

Prevents this happening.

Now you are sending nine characters, and have the receiver waiting for ten.
What is needed, is:

[code]
TRANSMITTER
#include <16f84a.h>
#include <stdlib.h>

#fuses XT, NOWDT, NOPROTECT

#use delay(clock=4000000)


#use rs232(baud=9600, xmit=PIN_A1, rcv=PIN_A0, PARITY=N, BITS=8, invert)

void main()
{
while (true) {
output_high(PIN_A1);
delay_ms(5);
output_low(PIN_A1);

printf("Rroksitzl/n");
delay_ms(3000);
}
}

This will send the string every three seconds.

RECEIVER
#include <16f84a.h>
#include <stdlib.h>

#fuses HS, NOWDT, NOPROTECT
#use delay(clock=20000000)

#use rs232(baud=9600, xmit=PIN_B1, rcv=PIN_B0, PARITY=N, BITS=8, invert)

#include <input.c>
void main()
{
char stringonja[11];
int a;

while (TRUE) {
while(PIN_B0 == 0) ;
while(PIN_B0 == 1);
get_string(stringonja,10);
printf("%s",stringonja);
}
}

Best Wishes
refa



Joined: 12 Dec 2003
Posts: 15

View user's profile Send private message MSN Messenger ICQ Number

PostPosted: Mon Jan 12, 2004 12:38 pm     Reply with quote

Thank you, I know that it won't loop wihtout the while(true); but I didn't know the fact about losing the last two chars when the code ends. Thank you a lot Ttelmah Smile
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 12, 2004 1:05 pm     Reply with quote

Quote:
I didn't know the fact about losing the last two chars when the code ends

It turns out that CCS is not the only compiler company that has
a "secret" instruction at the end of main(), that can cause trouble.
While researching 8051 compilers, I found that Raisonance puts
a RET instruction there, which can cause more problems than the
CCS Sleep instruction. So this would be something to watch out
for, on any embedded compiler.
http://www.raisonance.com/support/faq/General/RC51_8.html
refa



Joined: 12 Dec 2003
Posts: 15

View user's profile Send private message MSN Messenger ICQ Number

PostPosted: Mon Jan 12, 2004 1:25 pm     Reply with quote

Sorry but I am a beginner
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 12, 2004 1:34 pm     Reply with quote

I didn't mean that as a comment about your skill level, but
more as a general comment to everyone, and myself included.
refa



Joined: 12 Dec 2003
Posts: 15

View user's profile Send private message MSN Messenger ICQ Number

PostPosted: Mon Jan 12, 2004 1:39 pm     Reply with quote

Don't worry Smile I don't take anything personal Laughing
refa



Joined: 12 Dec 2003
Posts: 15

View user's profile Send private message MSN Messenger ICQ Number

PostPosted: Mon Jan 12, 2004 3:05 pm     Reply with quote

Hey Ttelmah, PCM programmer and other who tried to help I still didn't solve my problem with the S insteed of R Smile I don't know why but it might be because I transmitt the data wireless and get always another same character for another character, for number 2 I get 3, for R I get S and so on.... I can send anything as the first byte and disclude tha character in my software but I was interested why it is like that. I assume it is because of the wireless module.... Very Happy

Thank you friends for your help
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 12, 2004 3:28 pm     Reply with quote

Give the manufacturer and model of the wireless module.
If possible, provide a link to their website.
refa



Joined: 12 Dec 2003
Posts: 15

View user's profile Send private message MSN Messenger ICQ Number

PostPosted: Mon Jan 12, 2004 3:30 pm     Reply with quote

their website is: http://www.stecom.com/
module is: BK17 A5, transceiver
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jan 12, 2004 3:55 pm     Reply with quote

The data sheet has a clue.
http://www.stecom.com/files/26_BK17_1.pdf
It says there must be a pre-amble of some specified bits,
and there must be no gaps between bytes.
Also, they say you must send an XON, an XOFF, and a checksum.

I don't see that your code is doing any of this. That's probably
the reason for your problems.

The website does not appear to have any application notes.

I suggest you email them and ask for an appnote, with C source
code, which shows how to use the transceiver with a typical
micro-controller.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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