Linux Programming Discussions

Martin McCormick martin at x.it.okstate.edu
Tue Aug 31 20:20:12 UTC 2010


Here is the receive routine.

#include "headers.h" /*normal C header files*/
#include "defs.h" /*Values for the TTY port, baud rate, etc*/
#include "externs.h" /*all the global variables*/
void receive()

{ /*Receive incoming serial data.*/
 STOP=FALSE;                                         
res = 0; /*eventually shows where buffer ends*/
sleep(1);
/*Force a wait to cause receive buffer to stabilize.*/
        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... */                 
/*That makes sure there is a null to form a strint.*/
/*printf("%s\n", buf);                                       */
return;
} /*Receive incoming serial data.*/

	The only thing I can adjust in the port setup is a
variable that is either kept at 0 so it will time out if no
characters are received at all or to, say, 1024 or whatever the
buffer is defined as. This really doesn't effect the issue but
if set to > 0, the timeout never happens until at least one
character is received.

	Here is the initialization routine. Hardware flow
control is not an option as the receiver does not recognize
RTS/CTS. 

#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;
      }                                                                       

I have tried tcflush at the end of a receive with absolutely no
effect on anything.
Shane W writes:
> Hmm, reading from the ttySx device should never give
> duplicatedcharacters so it sounds like something is up with
> your buffer. I wouldn't zero it out but just keep note of
> the number of bytes read and when to stop. Also, after your
> first read returns, don't sleep but rather call poll() on
> the device which will return when there is data ready to be
> read.

	Yes. It is like it is already ready to receive data too
early.

	The hardware is a USB-based 8-port adaptor which behaves
quite normally when using Kermit or other serial routines.

	Thanks.




More information about the Blinux-list mailing list