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 CCS Technical Support

RS232 timeout error with fgetc() function

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



Joined: 05 Jun 2008
Posts: 18

View user's profile Send private message

RS232 timeout error with fgetc() function
PostPosted: Fri Sep 23, 2011 8:11 am     Reply with quote

i have 2 Rs232 on my project, one harware and one software.
Code:

#use rs232(baud="baud rate", xmit=PIN_"w", rcv=PIN_"x" , stream=hard)
#use rs232(baud="baud rate", xmit=PIN_"y", rcv=PIN_"z" , timeout=1000, stream=soft)


I want to know on RS232 stream=soft if i can check timeout error with fgetc(soft) function :
Code:

answer=fgetc(soft);
if(answer==0)
{
...
"error timeout treatment"
...
}

thanks
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Sep 23, 2011 8:20 am     Reply with quote

try the
kbhit() built in function - it works with streams as an option too

poll kbbit() UNTIL till true - then ONLY do a single getc() WHILE (true==kbhit())

no time wasted - no hanging of your program on a dead serial channel

if you define an edge interruptible portB RX pin - you can do a very nice job of making your own bkbhit function too - based on interrupt captured chars from YOUR RX buffer - i will post a program that demos a software based
rs232 hack i did some time ago that implements this next
-------
read all about it in the RS232 overview section of the CCS manual


Last edited by asmboy on Fri Sep 23, 2011 9:58 am; edited 2 times in total
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Sep 23, 2011 8:34 am     Reply with quote

Code:

//     using 4.9152mhz clock for zero err baud division
//     max safe baud rate 9600
//     with 9.83 mhz 19200  MY CTL PACKET IS ONLY 12 CHARS LONG
//     SO USING BUFFER OF 16 in this example
//     timer0 used to be sure incoming packets are complete
//     before attempting reply - to avoid char distortion and loss
//     due to interrupt  - T
#include <16f818.h>
#include <stdlib.h>

#Fuses HS,NOWDT,PUT, MCLR,BROWNOUT,NOLVP,NOCPD,WRT,NODEBUG,CCPB3,NOPROTECT
#use delay( clock=4915200)
#use rs232(baud=9600 , xmit=PIN_B1, rcv=PIN_B0, sample_early)
#use fast_io(A)
#use fast_io(B)

#bit  T0IF    = 0x0B.2  // flag in INTCON reg
//
#define BUFFER_SIZE 16
BYTE RISR_out;      // global background newchar from ISR BACKGND buffer
BYTE buffer[BUFFER_SIZE];
BYTE next_in = 0;
BYTE next_out = 0;
#define bkbhit (next_in!=next_out)
void bgetc(void) {
   RISR_out=buffer[next_out];    // NOTE : using GLObal return char variable
  ++next_out;
  if (BUFFER_SIZE==next_out) next_out=0;
}
void SIsr_INIT(void){
    clear_interrupt( int_ext );
    EXT_INT_EDGE(H_TO_L);
    enable_interrupts(int_ext);
    enable_interrupts(global);
}
#int_ext
   void serial_isr() {
   int t;
   buffer[next_in]=getc(); t=next_in;
   ++next_in
   if (BUFFER_SIZE==next_in) next_in=0;
   if(next_in==next_out)  next_in=t;           // Buffer full !!

}

//***********************************
void InitHDW ( void ) {
//***********************************
     setup_oscillator ( 4915200 ); //
     output_a (0); output_b (0);  set_tris_a (0xFF);  //  - all INPUT
     setup_timer_0( RTCC_DIV_64);  // set for rollovers ~12 ms holdoff later
      set_tris_b (0b00000001);  output_b (0);
}
//===================================
void main()
{
    char v;  char t;  unsigned int8 a;   unsigned int8 b;
    InitHDW();
    SIsr_INIT();
    printf ("*Hello*\r");  // just check output
    delay_ms(12000);  // delay to let me send a packet in
 
    while (1) {           // now get AND send at same time
      a=next_in;          // get mark of buffer position
      T0IF = 0;
       b=3;      // clear t0if and do 3x12 MS  +/-
      while(b){           // TIMER0 just rolls over endlessly
             if (  T0IF  )  {   // not using INTS - just watching timer
                --b;  T0IF=0;
                if (!b && a==next_in ){ // unload IF no NU chars R caught
                   //              your (safer) xmit routine here
                   while (bkbhit){ bgetc(); putc(RISR_out);}  //simple test

                }  // Implied ELSE - keep waiting
             } // END : IF t0if
       } //       END : WHILE B
    }  //         END : while  1
}
//******
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