[En-Nut-Discussion] fread() on a serial port problem...

Kyle Rhodes kyle at ghbraille.com
Thu Nov 20 22:19:47 CET 2003


Hello all,
 
In a Nut/OS 3.3.2 program one of the things I'm doing is reading from
serial port 0 using fread().  Occasionally, fread() returns < 0.  This
puzzles me, since shouldn't it block until the specified number of bytes
are available?  I don't see how an error condition could form with this
embedded hardware.  I had big problems with this issue using version
3.3.0 (or maybe .1?), it always failed.  To fix the problem (mostly) I
upgraded to 3.3.2.  It works a lot of the time, maybe 85%, but others it
returns the error.  Also note that errno is set to 0, I assume it's not
implemented fully.  Here is how I'm opening the serial port and reading
data from it:
 
  //Open serial port
  NutRegisterDevice(&devUart0, 0, 0);
  cd.rs232 = fopen("uart0", "r+b");
  
  tmp = DATA_UART_SPEED;                
  _ioctl(_fileno(cd.rs232), UART_SETSPEED, &tmp);
    
   tmp = 1;                     //Odd parity
   _ioctl(_fileno(cd.rs232), UART_SETPARITY, &tmp);
 
  NutSleep(500);
 
///////////////////////////////////////////////////////
 
size_t readFull(void *ptr, size_t size, FILE *fp)
{
  size_t retVal;
  size_t numRead = 0;
  unsigned char *cptr;
 
  cptr = (unsigned char *) ptr;
            
  while(numRead < size)
  {
    retVal = fread(&cptr[numRead], 1, size - numRead, fp);
                        
    if(retVal < 0)
      return(retVal);
                                    
    numRead += retVal;
  }
            
  return(numRead);
}
 
char readDih(FILE *fp, struct dihType *dihPtr)
{
  size_t retVal;
            
  retVal = readFull(dihPtr, DIH_SIZE, fp);
                                                            
  if(retVal <= 0)
    return(CONNECTION_ERROR);
                        
  if(dihPtr->magic != MAGIC_NUMBER)
    return(XFER_ERROR);
                        
  return(OK);
}
 
The readFull() function fails 'sometimes' when called to read a packet
of data from the serial port which is always 60 bytes.
 
If anyone can help it would be greatly appreciated.  This controller is
for an in house machine that needs to be online ASAP.  This is the
reason it is not online yet!  Please help!   :-)
 
 
Thanks,
 
Kyle Rhodes
Hardware / Software Engineer
gh, LLC
3000 Kent Ave
Lafayette, IN 47906
(765) 775-3776  ext 205
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.egnite.de/pipermail/en-nut-discussion/attachments/20031120/e978ffdb/attachment.html>


More information about the En-Nut-Discussion mailing list