[LCP]Serial programming

Mohamed Raouf mohamed.raouf at link.net
Thu Jul 4 20:39:05 UTC 2002


Here is the code I used for reading from the serial but it's not working
I also tried it without the "do -- while " (only using the read sentence)
but it also didn't work .


# include <stdio.h>
# include <unistd.h>
# include <fcntl.h>
# include <sys/ioctl.h>
# include <sys/wait.h>
# include <string.h>
# include <termios.h>
# include <stdlib.h>
# include <errno.h>
int
main(void)
 {
 int fd;
 int n;
 char recv_char;
     int m;

 fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
 if (fd == -1)
  {
  perror("open_port: unable to open /dev/ttyS0 - ");
  }
 else
  fcntl (fd, F_SETFL, FNDELAY);


   do
    {
        /* wait for a char to arrive */
        m=read(fd, &recv_char, 1);
    }
    while (m != 1);
    printf ( "%d",recv_char);
    return (recv_char);
    return (fd);
    }







More information about the linuxCprogramming mailing list