int OpenComPort (int ComPort, int baudrate, int databit, const char *stopbit, char parity) { char *pComPort; int fd; //File descriptor for the port int retval; switch (ComPort) { case 0: pComPort = "/dev/ttyUSB0"; break; case 1: pComPort = "/dev/ttyS1"; break; case 2: pComPort = "/dev/ttyS2"; break; case 3: pComPort = "/dev/ttyS3"; break; case 4: pComPort = "/dev/ttyS4"; break; case 5: pComPort = "/dev/ttyS5"; break; case 6: pComPort = "/dev/ttyS6"; break; case 7: pComPort = "/dev/ttyS7"; break; default: pComPort = "/dev/ttyS0"; break; } fd = open (pComPort, O_RDWR | O_NOCTTY | O_NONBLOCK); if (-1 == fd) return (-1); tcgetattr (fd, &termios_old); /* save old termios value */ /* 0 on success, -1 on failure */ retval = SetPortAttr (fd, baudrate, databit, stopbit, parity); if (-1 == retval) return -1; return (fd); }
/* Open serial port ComPort at baudrate baud rate. */ int OpenComPort (int ComPort, int baudrate, int databit,const char *stopbit, char parity) { const char *pComPort; int retval; int fd; switch (ComPort) { case 0: pComPort = "/dev/tcc-uart0"; break; case 1: pComPort = "/dev/tcc-uart1"; break; case 2: pComPort = "/dev/tcc-uart2"; break; case 3: pComPort = "/dev/tcc-uart3"; break; case 4: pComPort = "/dev/tcc-data"; break; case 5: pComPort = "/dev/ttySAC4"; break; default: pComPort = "/dev/tcc-uart0"; break; } fd = open (pComPort, O_RDWR/*|O_NONBLOCK*/); if (fd == -1) { fprintf (stderr, "cannot open port %s\n", pComPort); return (-1); } tcgetattr (fd, &termios_old); /* save old termios value */ retval = SetPortAttr (fd,baudrate,databit, stopbit, parity); if (retval == -1) { fprintf (stderr, "\nport %s cannot set baudrate at %d\n", pComPort, baudrate); return -1; } return fd; }
static int OpenComPort (const char *dev, int baudrate, int databit, const char *stopbit, char parity) { int retval; fd = open (dev, O_RDWR|O_NONBLOCK); if (fd == -1) { fprintf (stderr, "Failed to open %s.\r\n", dev); return -1; } tcgetattr (fd, &termios_old); /* save old termios value */ retval = SetPortAttr (baudrate,databit, stopbit, parity); if (retval == -1) { fprintf (stderr, "Failed to set port attr.\r\n"); tcsetattr (fd, TCSANOW, &termios_old); close(fd); return -1; } return 0; }