#include /* File control definitions */ #include #include /* POSIX terminal control definitions */ int main(void) { int ch; /* Character received from keyboard */ int fd; /* Serial port file descriptor */ struct termios options; unsigned char TmpChar[80]; fd = open_port("/dev/com1"); /* Open the serial port in no delay mode */ if (fd == -1) { /* Could not open the port */ exit(1); } else { tcgetattr(fd, &options); /* Get current options to modify them */ options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG | IEXTEN); options.c_cflag |= (CLOCAL | CREAD); /* Enable RAW I/O and receiver & local mode */ cfsetispeed(&options, B115200); /* 115200bps input */ cfsetospeed(&options, B115200); /* 115200bps output */ options.c_cflag &= ~PARENB; /* No parity */ options.c_cflag &= ~CSTOPB; /* 1 Stop bit */ options.c_cflag &= ~CSIZE; /* Mask out data size */ options.c_cflag |= CS8; /* 8 bit data */ options.c_iflag &= ~(INLCR | ICRNL | IGNCR); /* Do not interpret S/W flow control either */ options.c_iflag &= ~(IXON | IXOFF | IXANY); options.c_iflag |= IGNBRK; tcsetattr(fd, TCSANOW, &options); /* Post the updated options */ } for (;;) { printf("Checking for serial input\n"); ch = read(fd, &TmpChar[0], 1); if (ch > 0) { printf("Got a character\n"); if (ch == 'q') { break; } } else { printf("No character waiting\n"); } } close(fd); /* Close the serial port */ return(0); } int open_port(const char *Port) { int fd; /* File descriptor for the port */ fd = open(Port, O_RDWR | O_NOCTTY | O_NDELAY); if (fd != -1) { /* Successfully opened port */ fcntl(fd, F_SETFL, FNDELAY); /* Set to no delay mode */ } return(fd); } /* EOF */