46 int handle = open(port, O_RDWR | O_NOCTTY | O_DSYNC | O_NDELAY);
48 fprintf(stderr,
"Unexpected error while reading from serial port (Device: %s)\n", port);
49 fprintf(stderr,
"open() returned \"%s\"\n", strerror(errno));
53 fcntl(handle, F_SETFL, FNDELAY);
55 struct termios options;
58 tcgetattr(handle, &options);
61 if (rate == (speed_t)-1) {
62 fprintf(stderr,
"Unsupported baud rate requested: %d\n", baudRate);
66 cfsetispeed(&options, rate);
67 cfsetospeed(&options, rate);
68 options.c_oflag &= ~OPOST;
69 options.c_cflag &= ~(PARENB | CSTOPB | CSIZE);
70 options.c_cflag |= (CLOCAL | CREAD);
71 options.c_cflag |= CS8;
72 options.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
73 options.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
74 options.c_cc[VTIME] = 1;
75 options.c_cc[VMIN] = 0;
76 if (tcsetattr(handle, TCSANOW, &options)) { fprintf(stderr,
"tcsetattr() failed!\n"); }
80 tcgetattr(handle, &check);
81 if (cfgetispeed(&check) != rate) {
82 fprintf(stderr,
"Unable to set target baud. Wanted %d, got %d\n", baudRate,
int openSerialConnection(const char *port, const int baudRate)
Open a serial connection at a given baud rate.
int baud_to_flag(const int rate)
Convert a numerical baud rate to termios flag.
int flag_to_baud(const int flag)
Convert a termios baud rate flag to a numerical value.