View previous topic :: View next topic |
Author |
Message |
mojsa2000
Joined: 18 May 2010 Posts: 78
|
help about receive on RS232 |
Posted: Mon Aug 22, 2011 6:04 pm |
|
|
Hi
I have a question about RS232 communication. I use the #RDA_int and when
I receive a character (for example 'd') on this port I send an answer to
sender(PC) and wait to receive next byte. But PC needs to send 2 bytes because
first byte be ignored. Now I want to ask why this takes place?
I want to send a INT16 and a INT8 from PC to PIC and make this protocol.
This is my program. Also I use a PIC16f877a.
Code: |
#RDA_int
void RDA_isr(void)
{
if(dg_flag)
{
buffer[0]=getc();
nxt_byte=1;
dg_flag=0;
if(cal_flag)
int_flag=1;
}
if(nxt_byte)
{
buffer[1]=getc();
nxt_byte=0;
putc('s');
}
if(delay_flag)
{
delay_in=getc();
delay_flag=0;
cal_flag=1;
putc('w');
}
if(getc()=='d')
{
//get degrees value
dg_flag=1;
putc('r');
}
if(getc()=='s')
{
delay_flag=1;
putc('r');
}
}
|
In this way this is the communication details:
http://www.4shared.com/photo/tcHApd8O/2_online.html |
|
 |
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Aug 22, 2011 8:03 pm |
|
|
When you use a line like
buffer[0]=getc();
the getc() consumes the character in the buffer and another getc() won't get the same character but must wait for the next one to be received.
So if you have
if(getc()=='d')
followed by
if(getc()=='s')
you are comparing the FIRST character received to 'd' and the SECOND character received to 's'. If there is no second character yet the getc() function will wait all day for one to arrive.
I assume you really want something like:
buffer[0]=getc();
if(buffer[0] == 'd')
if(buffer[0] == 's') _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
 |
mojsa2000
Joined: 18 May 2010 Posts: 78
|
|
Posted: Mon Aug 22, 2011 8:38 pm |
|
|
Dear SherpaDoug
You're right. Thanks alot. I used your idea and my fault fixed.
best wishes |
|
 |
|