The following code configures two UART ports on a BeagleBone Black.
// Open the given file as read/write, don't become the controlling terminal, don't block
int fileDescriptor = open(filename, O_RDWR | O_NOCTTY | O_SYNC);
if(fileDescriptor == -1) {
cerr << "open_port: Unable to open " << filename << " " << strerror(errno) << endl;
return false;
}
struct termios tty;
memset(&tty, 0, sizeof tty);
if(tcgetattr(fileDescriptor, &tty) != 0) {
cerr << strerror(errno) << endl;
return false;
}
// Baud
cfsetispeed(&tty, B115200);
cfsetospeed(&tty, B115200);
// 8-bit chars
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
// Disable IGNBRK for mismatched speed tests; otherwise receive break
// as \000 chars
tty.c_iflag &= ~IGNBRK; // disable break processing
tty.c_lflag = 0; // no signaling chars, no echo
// No canonical processing
tty.c_oflag = 0; // no remapping, no delays
tty.c_cc[VMIN] = 0; // read doesn't block
tty.c_cc[VTIME] = 5; // 0.5 seconds read timeout
// Shut off xon/xoff ctrl
tty.c_iflag &= ~(IXON | IXOFF | IXANY);
// Ignore modem controls
tty.c_cflag |= (CLOCAL | CREAD);
// Enable reading
tty.c_cflag &= ~(PARENB | PARODD); // shut off parity
tty.c_cflag |= 0;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if(tcsetattr(fileDescriptor, TCSANOW, &tty) != 0) {
cerr << strerror(errno) << endl;
return false;
}
I'm able to send data, but for some reason 0x0D's sent are received as 0x0A's. I'm fairly sure something in this port configuration is doing this.
I notice these three flags on the man page for tcsetattr
:
INLCR
Translate NL to CR on input.
IGNCR
Ignore carriage return on input.
ICRNL
Translate carriage return to newline on input (unless IGNCR is set).
You might want to try explicitly setting INLCR
and ICRNL
to 0 ?