Link to home
Start Free TrialLog in
Avatar of DanRaposo
DanRaposoFlag for United States of America

asked on

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;
}
ASKER CERTIFIED SOLUTION
Avatar of Jinesh Kamdar
Jinesh Kamdar
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of DanRaposo

ASKER

Thanks....

It was actually something completely non-code related.