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

help about receive on RS232

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



Joined: 18 May 2010
Posts: 78

View user's profile Send private message

help about receive on RS232
PostPosted: Mon Aug 22, 2011 6:04 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Aug 22, 2011 8:03 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Mon Aug 22, 2011 8:38 pm     Reply with quote

Dear SherpaDoug
You're right. Thanks alot. I used your idea and my fault fixed.

best wishes
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