[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

RS-232 Receive Timing



	This serial routine is borrowed from a Debian HowTo on
RS-232 communications and it basically works until the data
received are 50 or 60 bytes or more. The buffer size is set to
1024 bytes so it is not an overrun. If I send short bursts of
data, I can immediately prepare to receive and all is well. In
longer strings of 50 or more characters, when I receive, I see
the tail end of what I previously received after the first call.
If I make another receive call, I get what I should have received the first
time.

	If I put a no-less-than 1-second sleep between
transmitting and receiving, I get perfect communications but
that sure slows down the program.

	I have set the receive routine to time out if nothing is
received in 2 seconds.

	Here is the serial port configuration.

#include "headers.h"
#include "defs.h"
#include "externs.h"
  
  
void portinit()
      {                                                                       
        struct termios oldtio,newtio;                                         
  
        fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY );                           
  
        if (fd <0) {perror(MODEMDEVICE); exit(-1); }                          
  
        tcgetattr(fd,&oldtio); /* save current port settings */               

/*Clear everything about that port.*/
        bzero(&newtio, sizeof(newtio));                                       
  
        newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD;           
  
        newtio.c_iflag = IGNPAR | ICRNL;
  
        newtio.c_oflag &= ~OPOST;
  
  
        /* set input mode (non-canonical, no echo,...) */                     
  
        newtio.c_lflag = 0;                                                   
  
        newtio.c_cc[VTIME]    = 20;   /* inter-character timer .1 seconds/tick */       
        newtio.c_cc[VMIN] = 0; /*Blocks until VMIN chars or timeout.*/ 
                                                                              
  
        tcflush(fd, TCIFLUSH);                                                
  
        tcsetattr(fd,TCSANOW,&newtio);                                        
  
return;
      }                                                                       

        newtio.c_cc[VTIME]    = 20;   /* inter-character timer .1 seconds/tick */       

That appears to work as one would expect. It sets the hang time
correctly and times out if nothing was received.

        newtio.c_cc[VMIN] = 0; /*Blocks until VMIN chars or timeout.*/ 
If this is set > 0, it hangs for ever because one has not
received anything yet.

	If one wants to receive data, the following routine is
called.

#include "headers.h"
#include "defs.h"
#include "externs.h"
void receive()

{ /*Receive incoming serial data.*/
 STOP=FALSE;                                         
res = 0;
sleep(1);
/*That's the cluge to keep it from re rerunning data from the
last call even if the receive buffer is totally empty. It
dredges it up from somewhere and fills the buffer again.*/
        while (STOP==FALSE) {       /* loop for input */                      
  
          res = read(fd,buf,sizeof(buf));   /* returns after sizeof(buf) chars have been inputed or timeoutt */ 
  
  
STOP=TRUE;                                         
        }                                                                     
          buf[res]=NULL;               /* so we can printf... */                 
/*printf("%s\n", buf);                                       */
return;
} /*Receive incoming serial data.*/

	I would like it to be able to time out if serial comm is
lost, but completely reset everything so it is waiting for new
data. Currently, the port is open for reception and transmission
Must one completely close the receiver down between calls?

	Thanks.

Martin McCormick
the whole time the program is running.


Reply to: