dali1985
dali1985

Reputation: 3313

Open and configure Serial port

I wrote the following code in which I open and configure my serial port Device

int open_port(void)
{
 int fd; // file description for the serial port 
 fd = open("/dev/ttyAMA0", O_RDWR | O_NOCTTY | O_NDELAY);
 if(fd == -1) // if open is unsucessful
  {
   //perror("open_port: Unable to open /dev/ttyAMA0 - ");
   printf("open_port: Unable to open /dev/ttyAMA0. \n");
  }
 else
 {
  fcntl(fd, F_SETFL, 0);
  printf("port is open.\n");
 }

return(fd);
} //open_port

And configure port

int configure_port(int fd)      // configure the port
{
 struct termios port_settings;      // structure to store the port settings in
 cfsetispeed(&port_settings, B9600);    // set baud rates
 cfsetospeed(&port_settings, B9600);
 port_settings.c_cflag &= ~PARENB;    // set no parity, stop bits, data bits
 port_settings.c_cflag &= ~CSTOPB;
 port_settings.c_cflag &= ~CSIZE;
 port_settings.c_cflag |= CS8;
 tcsetattr(fd, TCSANOW, &port_settings);    // apply the settings to the port
 return(fd);
} //configure_port 

My question (maybe is easy) is what I have to change exactly in these 2 functions to have

FILE *fd;
fd=fopen("/dev/ttyUSB0","r");

instead of fd= open(...) etc?

Upvotes: 0

Views: 1344

Answers (1)

Peter DW
Peter DW

Reputation: 456

Can you not just convert the file descriptor (fd) to a file pointer (fp) with the next call after your initialization?

FILE* fp = fdopen(fd,"w")

Upvotes: 1

Related Questions