|
|
View previous topic :: View next topic |
Author |
Message |
refa
Joined: 12 Dec 2003 Posts: 15
|
Having problems receiveing strings over RS232 |
Posted: Sun Jan 11, 2004 6:03 am |
|
|
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 |
Posted: Sun Jan 11, 2004 7:08 am |
|
|
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
|
|
Posted: Sun Jan 11, 2004 7:18 am |
|
|
Ttelemah, thank you. I got it but wasnt the point of my problem :(!
Thank you anyways |
|
|
refa
Joined: 12 Dec 2003 Posts: 15
|
|
Posted: Sun Jan 11, 2004 11:21 am |
|
|
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 |
|
|
refa
Joined: 12 Dec 2003 Posts: 15
|
|
Posted: Sun Jan 11, 2004 12:13 pm |
|
|
I get an S insteed of R all the time(for the first byte)!! Hmm, any idea friends?
Thank you |
|
|
Ttelmah Guest
|
|
Posted: Sun Jan 11, 2004 3:46 pm |
|
|
refa wrote: | I get an S insteed of R all the time(for the first byte)!! Hmm, any idea friends?
Thank you |
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
|
|
Posted: Mon Jan 12, 2004 12:38 pm |
|
|
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 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jan 12, 2004 1:05 pm |
|
|
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
|
|
Posted: Mon Jan 12, 2004 1:25 pm |
|
|
Sorry but I am a beginner |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jan 12, 2004 1:34 pm |
|
|
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
|
|
Posted: Mon Jan 12, 2004 1:39 pm |
|
|
Don't worry I don't take anything personal |
|
|
refa
Joined: 12 Dec 2003 Posts: 15
|
|
Posted: Mon Jan 12, 2004 3:05 pm |
|
|
Hey Ttelmah, PCM programmer and other who tried to help I still didn't solve my problem with the S insteed of R 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....
Thank you friends for your help |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jan 12, 2004 3:28 pm |
|
|
Give the manufacturer and model of the wireless module.
If possible, provide a link to their website. |
|
|
refa
Joined: 12 Dec 2003 Posts: 15
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Jan 12, 2004 3:55 pm |
|
|
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. |
|
|
|
|
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
|