I trying to read serial raw bytes from using serial port "/dev/ttyS0".In my program, What i want to do is to press the letter and immediately see the the letter I introduced repeated without pressing ENTER. For example, if I press the letter 'a' I want to see an other 'a' next to it.
My code is here:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/types.h>
int main()
{
        int n = 0, fd = 0;
        struct termios term,trm;
        printf("%d\n",getpid());
        fd = open("/dev/ttyS0", O_RDWR | O_NOCTTY | O_NDELAY);
        if (fd == -1)
        {
                perror("open");
                return 1;
        }
        else
        {
                fcntl(fd, F_SETFL, 0);
                perror("Port");
        }
        if (n = tcgetattr(fd, &term) == -1)
        {
                perror("tcgetattr");
                return 1;
        }
        if (n = cfsetispeed(&term, B115200) == -1)
        {
                perror("cfsetispeed");
                return 1;
        }
        if (n = cfsetospeed(&term, B115200) == -1)
        {
                perror("cfsetospeed");
                return 1;
        }
        term.c_cflag |= (CLOCAL | CREAD);
        term.c_cflag &= ~PARENB;
        term.c_cflag &= ~CSTOPB;
        term.c_cflag &= ~CSIZE;
        term.c_cflag |= CS8;
        term.c_cflag &= ~CRTSCTS;
        term.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
        term.c_iflag &= ~(IXON | IXOFF | IXANY);
        term.c_iflag |= (INPCK | ISTRIP);
        term.c_oflag &= ~OPOST;
        unsigned char c,d;
        ssize_t s=0;
        tcflush(fd, TCIOFLUSH);
        system("/bin/stty raw");
        while((c=getchar()) != 'q')
        {
                write(fd, &c,1);
                term.c_cc[VMIN] = 1;
                term.c_cc[VTIME] = 0;
                tcsetattr(fd, TCSANOW, &term);
                if((s=read(fd, &d,1)) != -1)
                {
                        perror("read");
                        printf("%c",d);
                }
        }
        system("/bin/stty cooked");
        close(fd);
        return 0;
}
 
     
     
    