bool SerialPort::open(int mode) { // Always open as raw so no input/output buffering if (!QFile::open(mode | IO_Raw)) return false; // Save current state and set raw if (tcgetattr(handle(),&_p->state) < 0) { qWarning("Saving termios state failed: %d", errno); close(); return false; } else { struct termios state = _p->state; cfmakeraw(&state); state.c_cflag |= CLOCAL; state.c_cflag &= ~CRTSCTS; if (tcsetattr(handle(), TCSANOW, &state) < 0) { qWarning("Setting raw state failed: %d", errno); close(); return false; } } // Set serial port to current characteristics setSerial(_baud, _dataBits, _parity, _stopBits); setHandshake(_handshake); setTimeout(_timeout); return true; }
void initSerialPort() { com = open(COM, O_RDWR | O_NOCTTY | O_NDELAY); if (com <0) comErr("Failed to open serial port\n"); fcntl(com, F_SETFL, 0); struct termios opts; tcgetattr(com, &opts); opts.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); opts.c_cflag |= (CLOCAL | CREAD); opts.c_cflag &= ~PARENB; opts.c_cflag |= CSTOPB; // two stop bits opts.c_cflag &= ~CSIZE; opts.c_cflag |= CS8; opts.c_oflag &= ~OPOST; opts.c_iflag &= ~INPCK; opts.c_iflag &= ~(IXON | IXOFF | IXANY); opts.c_cc[ VMIN ] = 0; opts.c_cc[ VTIME ] = 10;//0.1 sec cfsetispeed(&opts, baudRate); cfsetospeed(&opts, baudRate); setHandshake(); if (tcsetattr(com, TCSANOW, &opts) != 0) { perror(COM); abort(); } tcflush(com,TCIOFLUSH); // just in case some crap is the buffers if (!terminalMode) { char buf = -2; while (read(com, &buf, 1)>0) { if (verbose) flsprintf(stderr,"Unexpected data from serial port: %02X\n",buf & 0xFF); } } }