Printing the data from a serial port

Hi all,

I am trying to write a simple C app that simply continues to write the data it captures off the serial port.  Below is a peice of code I found here, but I am getting into the "Have a problem while receiving data".  I want to just print to the screen anything it finds on the port.

#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

int main ( void )
{
     int     iHandler;
     struct termios trmNewtio;
     long     lByteWrite;
     long     lByteRead;
     char     szData [12];

     //     Trying to open as file desc
     iHandler = open ( "/dev/ttyS0", O_RDWR | O_NONBLOCK );
     if ( iHandler == -1 )
     {
          printf ( "Error while opening the port.\n" );
          return -1;
     }

     //     Setup Terminal Poperties
     //     CS8 - 8 bit data
     //     CLOCAL - ignore modem control lines
     //     CREAD - enable receiver
     trmNewtio.c_cflag = CS8 | CLOCAL | CREAD;
     trmNewtio.c_iflag = IGNPAR;          //ignore framing errors and parity errors.
     trmNewtio.c_oflag = 0;               //Not used
     trmNewtio.c_lflag = 0;               //Not used
     cfsetispeed ( &trmNewtio, B9600 );     //Setup speed
     tcflush ( iHandler, TCIOFLUSH );
     tcsetattr ( iHandler, TCSANOW, &trmNewtio );     //Actual set

     //     Actual Receive
     while (1) {
     lByteRead = read ( iHandler, szData, 10 );
     if ( lByteRead != 10 )
          printf ( "Have a problem while receiving data\n" );
     printf("Read: %s\n", szData);
}
     //     close the port
     if ( close ( iHandler ) < 0 )
          printf ( "Error: Failed to close.\n" );

     return 0;
}
DanRaposoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jinesh KamdarCommented:
I dont know if this will work, but have u tried reading in each byte instead of a block of 10 bytes ???

lByteRead = read ( iHandler, szData, 1 );
if ( lByteRead != 1 )
      printf ( "Have a problem while receiving data\n" );
else
      printf("Read: %s\n", szData);

Also, make sure u have specified the right settings for the port in ur program.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DanRaposoAuthor Commented:
Thanks....

It was actually something completely non-code related.  
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.