以下代码在BeagleBone Black上配置两个UART端口。
// 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;
}
我能够发送数据,但由于某种原因,发送的0x0D被接收为0x0A&#39; s。我非常确定此端口配置中的某些内容正在执行此操作。
答案 0 :(得分:1)
我注意到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).
您可能想尝试将INLCR
和ICRNL
明确设置为0?