|
|
View previous topic :: View next topic |
Author |
Message |
Mahdi
Joined: 21 Aug 2012 Posts: 47
|
transmit and receive several int8 variable with ex_sisr.c |
Posted: Tue Feb 18, 2014 10:28 am |
|
|
hi buddies.
i want to send several variable with rs232 protocol and use the sisr.c example code but in dont know how to do it.
how to send int8 and receive.bgetc() function how to work?
thanks alot. |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Feb 18, 2014 10:49 am |
|
|
Simplest for beginner, send as ASCII characters.
You can then see with a PC what's going on.
So much easier in human readable form.
printf() will do the sending part.
Mike |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Tue Feb 18, 2014 10:59 am |
|
|
I'd add slightly more. Use CR as a separator.
So to send, printf, with "%u\r".
Then store the incoming characters from bgetc, into a char array, till you see the CR (code 13). When you see this, write '0' to the array (string terminator), and call atoi, with the address of the array. This will return the decoded int8 value.
A search here will find lots of examples. |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue Feb 18, 2014 11:13 am |
|
|
Hi,
As is, the 'ex_sisr.c' program is going to receive, via interrupt, characters, one at a time, and place them inside a circular buffer. You will need to extract these characters, and utilize them as you wish.
Can you tell us the format of the data that you need to receive? If you have control over the format, then you assemble a 'packet' of data that the PIC can easily receive and parse to extract the variables you want to send/receive. Here is an example of a data transmission format that is very easy to receive and extract:
Note the 'Start' character, '#', the data 'Type' character, 'V', the actual data, '123', and the terminator <CR>. This format is very easy to buffer and decode with a circular or linear buffer.
John |
|
|
Mahdi
Joined: 21 Aug 2012 Posts: 47
|
|
Posted: Tue Feb 18, 2014 3:05 pm |
|
|
thanks for reply Mike,Ttelmah,ezflyr.
transmitter:
Code: | #include <33FJ12GP202.h>
#use delay(crystal=20m)
#pin_select U1TX=PIN_B14
#pin_select U1RX=PIN_B15
#use rs232(UART1, baud=9600,errors)
void main()
{
int8 h=10;
while(TRUE)
{
printf("%u",h);
}
} |
receiver :
Code: | #include <rec.h>
#include <Lcd420.C>
#INCLUDE <stdlib.h>
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
#int_rda
void serial_isr() {
int t;
buffer[next_in]=getc();
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t; // Buffer full !!
}
#define bkbhit (next_in!=next_out)
BYTE bgetc() {
BYTE c;
while(!bkbhit) ;
c=buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
}
void main()
{
enable_interrupts(INT_RDA);
enable_interrupts(INTR_GLOBAL);
lcd_init();
char h[];
int8 j;
while(TRUE)
{
while(bkbhit){
h=bgetc();
}
j=atoi(h);
lcd_gotoxy(1,1);
printf(lcd_putc,"%u",j);
}
} |
whats wrong?
thanks |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue Feb 18, 2014 3:15 pm |
|
|
Hi,
Well, for starters you pretty much ignored what Ttelmah and I recommended you do!
You seem to be going about this project totally "in the blind", and that is a recipe for extreme frustration for you and the people who will try to help you!
My recommendation would be to first use your transmitter code to send to your PC serial port to see *exactly* what is being transmitted. Once you do that (hint: it's multiple ASCII characters), you should be able to see where your receiving program is falling off the rails (hint: when multiple characters are received, following characters overwrite preceding ones....).
Note that you also have no way out of your character reading loop! As Ttelmah said, add a <CR> to the data transmission and use it as an 'end of data' marker.......
Additional hint: when Ttelmah makes a recommendation, you can pretty much take it as gospel! Me? Not so much
John
Last edited by ezflyr on Tue Feb 18, 2014 3:17 pm; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19551
|
|
Posted: Tue Feb 18, 2014 3:16 pm |
|
|
Start with you are leaving out the delimiter. If you print numbers one after the other, how is the receive code ever going to know where one ends?.
Without this, and the code to look for it in the slave, how is it ever going to work...
Then in the receive you declare an array, but no size. So it is a zero length array.
Then as soon as one character is fetched, it is written into invalid space (no space allocated for it), and the string is never terminated. Strings need a terminating null. |
|
|
Mahdi
Joined: 21 Aug 2012 Posts: 47
|
|
Posted: Tue Feb 18, 2014 4:27 pm |
|
|
sorry.i'm beginner in c language.
in previous post i want to see i can send data and receive it and show on lcd.
i know that i should add separator to parse data.
Code: | For (i =0; i<=5; i++)
{
h[i]=bgetc();
} |
Partly my problem has solved but my problem is that when i send 5 character, for example 10,12 how to Understand how to character received?
may be the 12 Will 9,then my character has 4,and this is a problem.
excuse me.my English language is very poor.Some sentences of your I do not understand,So I'm confused
trans:
Code: | #include <33FJ12GP202.h>
#use delay(crystal=20m)
#pin_select U1TX=PIN_B14
#pin_select U1RX=PIN_B15
#use rs232(UART1, baud=9600,errors)
#INCLUDE <string.h>
void main()
{
char f[10];
strcpy(f,"123456\r");
int8 h=10,k=25;
while(TRUE)
{
printf("%u,",h);
printf("%u,",k);// use , for seprator
} |
}
rec:
Code: | #include <rec.h>
#include <Lcd420.C>
#INCLUDE <stdlib.h>
#define BUFFER_SIZE 32
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
#int_rda
void serial_isr() {
int t;
buffer[next_in]=getc();
t=next_in;
next_in=(next_in+1) % BUFFER_SIZE;
if(next_in==next_out)
next_in=t; // Buffer full !!
}
#define bkbhit (next_in!=next_out)
BYTE bgetc() {
BYTE c;
while(!bkbhit) ;
c=buffer[next_out];
next_out=(next_out+1) % BUFFER_SIZE;
return(c);
}
void main()
{
enable_interrupts(INT_RDA);
enable_interrupts(INTR_GLOBAL);
lcd_init();
char h[10];
while(TRUE)
{
For (i = 0; i<=5; i++)// increments i
{
h[i] = bgetc(); // saves received bytes to the array
}
lcd_gotoxy(1,1);
printf(lcd_putc,"%s",h);
}
} |
|
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Tue Feb 18, 2014 7:07 pm |
|
|
Mahdi,,
Early on I asked you to describe the data being sent and received. It's a critical question relevant to the design of your code!
As a hint, rather than your 'For' loop in Main(), how about a Do-While loop that executes until the received character is a <CR>? At that point you know you've got the whole data transmission, right? I'd concentrate on that first!
John |
|
|
|
|
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
|