I am having problems reading and writing to serial ports using gcc under Mandrake Linux v 8.1. I have a cable connecting com1 and com2 and I write 16 characters to com1. If I read com2 immediately I get no characters returned but if I delay
4 or more seconds before reading I get 4096 characters returned - my 16 characters repeated followed by 1, 2, 4, 8, 16, etc linefeeds. Any help would be greatly appreciated.
Code:
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/stat.h>
int main(void)
{
int fd0, fd1, knt, n;
char coma[2];
struct termios option0, option1;
// Open and set up ttyS0
fd0 = open("/dev/ttyS0", O_RDWR | O_NOCTTY);
if (fd0 == -1) return(-1);
tcgetattr(fd0, &option0);
cfsetispeed(&option0, B19200);
cfsetospeed(&option0, B19200);
option0.c_cflag |= (CLOCAL | CREAD);
option0.c_cflag &= ~PARENB;
option0.c_cflag &= ~CSTOPB;
option0.c_cflag &= ~CSIZE;
option0.c_cflag |= CS8;
option0.c_oflag = 0;
option0.c_lflag |= ~(ICANON | ECHO | ISIG);
tcflush(fd0, TCIFLUSH);
tcsetattr(fd0, TCSANOW, &option0);
// Open and set up ttyS1
fd1 = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY);
if (fd1 == -1) return(-1);
tcgetattr(fd1, &option1);
cfsetispeed(&option1, B19200);
cfsetospeed(&option1, B19200);
option1.c_cflag |= (CLOCAL | CREAD);
option1.c_cflag &= ~PARENB;
option1.c_cflag &= ~CSTOPB;
option1.c_cflag &= ~CSIZE;
option1.c_cflag |= CS8;
option1.c_lflag |= ~(ICANON | ECHO | ISIG);
option1.c_cc[VTIME] = 10;
option1.c_cc[VMIN] = 0;
tcflush(fd1, TCIFLUSH);
tcsetattr(fd1, TCSANOW, &option1);
// Write to ttyS0
n = write(fd0,"ABCABCABCABCABC\n",16);
printf ("Wrote %d characters, ABCABCABCABCABC\\n\n",n);
// Without this delay, no characters are read, with it 4096 are read.
sleep(4);
coma[1]=0;
n = read (fd1, coma, 1);
knt = 1;
while (n >= 0)
{
if (coma[0]=='\n') printf("\\n");
else printf ("%s",coma);
knt++;
if (knt%40 == 0) printf("\n");
n = read (fd1, coma, 1);
}
printf ("\nknt = %d, n = %d\n",knt,n);
close(fd0);
close(fd1);
return (0);
}