The fundamental problem is that your code indicates that you have not (properly) initialized the tty port. Essentially the initialization does not call tcsetattr()
to install the updated configuration that you tried to specify.
The proper way to initialize the tty port is to open the port, obtain the current attributes, save the current attributes for restoration on program exit, modify the attributes, and install the attributes. And always test the return codes from system calls.
tty_fd = open(device, O_RDWR | O_NONBLOCK);
if (tty_fd< 0) {
syslog(LOG_DEBUG, "failed to open: %d, %s", tty_fd, strerror(errno));
exit (-1);
}
rc = tcgetattr(tty_fd, &tio);
if (rc < 0) {
syslog(LOG_DEBUG, "failed to get attr: %d, %s", rc, strerror(errno));
exit (-2);
}
savetio = tio; /* preserve original settings for restoration */
spd = B9600;
cfsetospeed(&tio, (speed_t)spd);
cfsetispeed(&tio, (speed_t)spd);
cfmakeraw(&tio);
tio.c_cc[VMIN] = 1;
tio.c_cc[VTIME] = 5;
tio.c_cflag &= ~CSTOPB;
tio.c_cflag &= ~CRTSCTS; /* no HW flow control? */
tio.c_cflag |= CLOCAL | CREAD;
rc = tcsetattr(tty_fd, TCSANOW, &tio);
if (rc < 0) {
syslog(LOG_DEBUG, "failed to set attr: %d, %s", rc, strerror(errno));
exit (-3);
}
Of course there may also be cabling and hardware issues still to be resolved, but since the tty port has not been initialized to the operational state that you want or need, nothing is likely to function as you expect.
Here's a guide for programming the serial port.