I am trying to read and write data from an FPGA board. The board itself is equipped with a driver that creates the ttyUSB0 terminal device whenever the board is connected. The FPGA has an asynchronous receiver and transmitter, and they seem to work.
However, the problem seems to be on C side. I used some test vectors to check if the FPGA outputs the correct information. I noticed a few things:
- The device sometimes does not open correctly
- Terminal attributes can sometimes not be restored or set.
- Reading is sometimes not blocked and does not return the correct value.
The following describes how to configure the terminal descriptor and file parameters. Most of this was taken from here: http://slackware.osuosl.org/slackware-3.3/docs/mini/Serial-Port-Programming
Any advice or comments on why the program may be unsuccessful will be very helpful.
#include <stdio.h> // Standard input/output definitions
#include <string.h> // String function definitions
#include <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
#include <errno.h> // Error number definitions
#include <termios.h> // POSIX terminal control definitions
int open_port(void){
int fd;
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
if (fd == -1){
fprintf(stderr, "open_port: Unable to open /dev/ttyUSB0 %s\n",strerror(errno));
exit(EXIT_FAILURE);
}
return (fd);
}
int main(void){
int fd = 0;
struct termios options;
fd = open_port();
fcntl(fd, F_SETFL);
tcgetattr(fd, &options);
cfsetispeed(&options, B230400);
cfsetospeed(&options, B230400);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
options.c_cflag &= ~CRTSCTS;
options.c_lflag &= ~(ICANON | ECHO | ISIG);
tcsetattr(fd, TCSANOW, &options);
close(fd)
return EXIT_SUCCESS
}
UPDATE
I changed my code based on the first answer. This is what I have now:
#include <errno.h> // Error number definitions
#include <stdint.h> // C99 fixed data types
#include <stdio.h> // Standard input/output definitions
#include <stdlib.h> // C standard library
#include <string.h> // String function definitions
#include <unistd.h> // UNIX standard function definitions
#include <fcntl.h> // File control definitions
#include <termios.h> // POSIX terminal control definitions
int open_port(void){
int fd;
fd = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY);
if (fd == -1){
fprintf(stderr, "open_port: Unable to open /dev/ttyUSB0 %s\n",strerror(errno));
exit(EXIT_FAILURE);
}
return fd;
}
int main(void){
int fd = 0;
struct termios options;
int rc;
fd = open_port();
if((rc = tcgetattr(fd, &options)) < 0){
fprintf(stderr, "failed to get attr: %d, %s\n", fd, strerror(errno));
exit(EXIT_FAILURE);
}
cfsetispeed(&options, B230400);
cfsetospeed(&options, B230400);
cfmakeraw(&options);
options.c_cflag |= (CLOCAL | CREAD);
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CRTSCTS;
options.c_cc[VMIN] = 1;
options.c_cc[VTIME] = 2;
if((rc = tcsetattr(fd, TCSANOW, &options)) < 0){
fprintf(stderr, "failed to set attr: %d, %s\n", fd, strerror(errno));
exit(EXIT_FAILURE);
}
close(fd);
return EXIT_SUCCESS;
}
To clarify, the receiver and transmitter use 8 data bits, 1 stop bit and a parity bit.