/*! \fn bool Posix_QextSerialPort::open(int=0) Opens a serial port. Note that this function does not specify which device to open. If you need to open a device by name, see Posix_QextSerialPort::open(const char*). This function has no effect if the port associated with the class is already open. The port is also configured to the current settings, as stored in the Settings structure. */ bool Posix_QextSerialPort::open(int) { LOCK_MUTEX(); if (!portOpen) { /*open the port*/ Posix_File->setName(portName); if (Posix_File->open(IO_Async|IO_Raw|IO_ReadWrite)) { portOpen=true; } /*configure port settings*/ tcgetattr(Posix_File->handle(), &Posix_CommConfig); /*set up other port settings*/ Posix_CommConfig.c_cflag|=CREAD|CLOCAL; Posix_CommConfig.c_lflag&=(~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG)); Posix_CommConfig.c_iflag&=(~(INPCK|IGNPAR|PARMRK|ISTRIP|IXANY)); Posix_CommConfig.c_oflag&=(~OPOST); Posix_CommConfig.c_cc[VMIN]=0; Posix_CommConfig.c_cc[VINTR] = _POSIX_VDISABLE; Posix_CommConfig.c_cc[VQUIT] = _POSIX_VDISABLE; Posix_CommConfig.c_cc[VSTART] = _POSIX_VDISABLE; Posix_CommConfig.c_cc[VSTOP] = _POSIX_VDISABLE; Posix_CommConfig.c_cc[VSUSP] = _POSIX_VDISABLE; setBaudRate(Settings.BaudRate); setDataBits(Settings.DataBits); setStopBits(Settings.StopBits); setParity(Settings.Parity); setFlowControl(Settings.FlowControl); setTimeout(Posix_Copy_Timeout.tv_sec, Posix_Copy_Timeout.tv_usec); tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig); } UNLOCK_MUTEX(); return portOpen; }
bool QSerialPort::open(int block) { if( block== 1) portDesc = ::open(portName.toAscii(),O_RDWR); else portDesc = ::open(portName.toAscii(),O_RDWR | O_NONBLOCK ); if(portDesc == -1) { return false; } portOpen = true; tcgetattr(portDesc, &portConfig); portConfig.c_cflag|=(CLOCAL | CREAD); portConfig.c_lflag&=(~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG)); portConfig.c_iflag&=(~(IGNBRK|BRKINT|PARMRK|ISTRIP |INLCR| IGNCR|ICRNL|IXON)); // (~(INPCK|IGNPAR|PARMRK|ISTRIP|IXANY)); portConfig.c_iflag&=~(ICRNL); portConfig.c_oflag&=(~OPOST); tcsetattr(portDesc, TCSANOW, &portConfig); setBaudRate(settings.baudRate); setDataBits(settings.dataBits); setFlowControl(settings.flowControl); setParity(settings.parity); setStopBits(settings.stopBits); setDtr(); setRts(); connect(&portFile, SIGNAL(readyRead()), this, SLOT(slotNotifierActivated())); return true; }
static int openTouchDev( char const *devName ){ int fd = -1 ; if( !isSerial(devName) ){ fd = open(devName, O_RDONLY); } else { printf( "Serial touch screen\n" ); char const *end = strchr( devName, ',' ); if( 0 == end ) end = devName + strlen(devName); unsigned nameLen = end-devName ; printf( "nameLen: %u, end %p\n", nameLen, end ); char deviceName[512]; if( nameLen < sizeof(deviceName) ){ memcpy( deviceName, devName, nameLen ); deviceName[nameLen] = '\0' ; unsigned baud = 9600 ; unsigned databits = 8 ; char parity = 'N' ; unsigned stop = 1 ; if( '\0' != *end ){ end++ ; baud = 0 ; while( isdigit(*end) ){ baud *= 10 ; baud += ( *end-'0' ); end++ ; } if( ',' == *end ){ end++ ; databits = *end-'0' ; end++ ; if( ',' == *end ){ end++ ; parity = *end++ ; if( ',' == *end ){ stop = end[1] - '0'; } } } } fd = open( deviceName, O_RDWR ); if( 0 < fd ){ printf( "settings: %s,%u,%u,%c,%u\n", deviceName, baud, databits, parity, stop ); setBaud( fd, baud ); setRaw( fd ); setDataBits( fd, databits ); setStopBits( fd, stop ); setParity( fd, parity ); } else perror( deviceName ); } else fprintf( stderr, "Invalid touch device name\n" ); } return fd ; }
void SerialPort::setSerial(int baud, int dataBits, int parity, int stopBits) { setBaud(baud); setDataBits(dataBits); setParity(parity); setStopBits(stopBits); }
/*! \fn bool Win_QextSerialPort::open(OpenMode mode) Opens a serial port. Note that this function does not specify which device to open. If you need to open a device by name, see Win_QextSerialPort::open(const char*). This function has no effect if the port associated with the class is already open. The port is also configured to the current settings, as stored in the Settings structure. */ bool Win_QextSerialPort::open(OpenMode mode) { unsigned long confSize = sizeof(COMMCONFIG); Win_CommConfig.dwSize = confSize; DWORD dwFlagsAndAttributes = 0; if (queryMode() == QextSerialBase::EventDriven) dwFlagsAndAttributes += FILE_FLAG_OVERLAPPED; LOCK_MUTEX(); if (mode == QIODevice::NotOpen) return isOpen(); if (!isOpen()) { /*open the port*/ Win_Handle=CreateFileA(port.toLatin1(), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, dwFlagsAndAttributes, NULL); if (Win_Handle!=INVALID_HANDLE_VALUE) { /*configure port settings*/ GetCommConfig(Win_Handle, &Win_CommConfig, &confSize); GetCommState(Win_Handle, &(Win_CommConfig.dcb)); /*set up parameters*/ Win_CommConfig.dcb.fBinary=TRUE; Win_CommConfig.dcb.fInX=FALSE; Win_CommConfig.dcb.fOutX=FALSE; Win_CommConfig.dcb.fAbortOnError=FALSE; Win_CommConfig.dcb.fNull=FALSE; setBaudRate(Settings.BaudRate); setDataBits(Settings.DataBits); setStopBits(Settings.StopBits); setParity(Settings.Parity); setFlowControl(Settings.FlowControl); setTimeout(Settings.Timeout_Millisec); SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG)); //init event driven approach if (queryMode() == QextSerialBase::EventDriven) { Win_CommTimeouts.ReadIntervalTimeout = MAXDWORD; Win_CommTimeouts.ReadTotalTimeoutMultiplier = 0; Win_CommTimeouts.ReadTotalTimeoutConstant = 0; Win_CommTimeouts.WriteTotalTimeoutMultiplier = 0; Win_CommTimeouts.WriteTotalTimeoutConstant = 0; SetCommTimeouts(Win_Handle, &Win_CommTimeouts); if (!SetCommMask( Win_Handle, EV_TXEMPTY | EV_RXCHAR | EV_DSR)) { qWarning("Failed to set Comm Mask. Error code: %ld", GetLastError()); UNLOCK_MUTEX(); return false; } overlapThread->start(); } QIODevice::open(mode); } } else { UNLOCK_MUTEX(); return false; } UNLOCK_MUTEX(); return isOpen(); }
/*! \brief SerialDevice::stopDevice Is the serial interface's implementation of setDefaults. * Sets the serial devices to the default values. (Rate=9600, Parity=None, Flow=None, Data=8, Stop=1) */ void SerialDevice::setDefaults() { statusReady = false; setBaudRate(9600); setParity(0); setFlowControl(0); setDataBits(8); setStopBits(1); }
void Serial::config(uint32_t speed, Serial::Parity parity, Serial::WordLength dataBits, Serial::StopBits stopBits, HardwareFlowControl hardwareFlow) { disable(Device::Part::All); setSpeed(speed); setParity(parity); setWordLength(dataBits); setStopBits(stopBits); setHardwareFlowControl(hardwareFlow); }
/*! \fn Win_QextSerialPort::Win_QextSerialPort(const PortSettings& settings) Constructs a port with default name and specified settings. */ Win_QextSerialPort::Win_QextSerialPort(const PortSettings& settings) { Win_Handle=INVALID_HANDLE_VALUE; setBaudRate(settings.BaudRate); setDataBits(settings.DataBits); setStopBits(settings.StopBits); setParity(settings.Parity); setFlowControl(settings.FlowControl); setTimeout(settings.Timeout_Sec, settings.Timeout_Millisec); }
/*! Sets the port settigns. */ void QextSerialPort::setPortSetting(const PortSettings& settings) { setBaudRate(settings.BaudRate); setDataBits(settings.DataBits); setParity(settings.Parity); setStopBits(settings.StopBits); setFlowControl(settings.FlowControl); setTimeout(settings.Timeout_Millisec); }
/*! Opens the serial port associated to this class. This function has no effect if the port associated with the class is already open. The port is also configured to the current settings, as stored in the Settings structure. */ bool QextSerialPort::open(OpenMode mode) { QMutexLocker lock(mutex); if (mode == QIODevice::NotOpen) return isOpen(); if (!isOpen()) { qDebug() << "trying to open file" << port.toAscii(); //note: linux 2.6.21 seems to ignore O_NDELAY flag if ((fd = ::open(port.toAscii() ,O_RDWR | O_NOCTTY | O_NDELAY)) != -1) { qDebug("file opened succesfully"); setOpenMode(mode); // Flag the port as opened tcgetattr(fd, &old_termios); // Save the old termios Posix_CommConfig = old_termios; // Make a working copy /* the equivelent of cfmakeraw() to enable raw access */ #ifdef HAVE_CFMAKERAW cfmakeraw(&Posix_CommConfig); // Enable raw access #else Posix_CommConfig.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON); Posix_CommConfig.c_oflag &= ~OPOST; Posix_CommConfig.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN); Posix_CommConfig.c_cflag &= ~(CSIZE | PARENB); Posix_CommConfig.c_cflag |= CS8; #endif /*set up other port settings*/ Posix_CommConfig.c_cflag|=CREAD|CLOCAL; Posix_CommConfig.c_lflag&=(~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG)); Posix_CommConfig.c_iflag&=(~(INPCK|IGNPAR|PARMRK|ISTRIP|ICRNL|IXANY)); Posix_CommConfig.c_oflag&=(~OPOST); Posix_CommConfig.c_cc[VMIN]= 0; #ifdef _POSIX_VDISABLE // Is a disable character available on this system? // Some systems allow for per-device disable-characters, so get the // proper value for the configured device const long vdisable = fpathconf(fd, _PC_VDISABLE); Posix_CommConfig.c_cc[VINTR] = vdisable; Posix_CommConfig.c_cc[VQUIT] = vdisable; Posix_CommConfig.c_cc[VSTART] = vdisable; Posix_CommConfig.c_cc[VSTOP] = vdisable; Posix_CommConfig.c_cc[VSUSP] = vdisable; #endif //_POSIX_VDISABLE setBaudRate(Settings.BaudRate); setDataBits(Settings.DataBits); setParity(Settings.Parity); setStopBits(Settings.StopBits); setFlowControl(Settings.FlowControl); setTimeout(Settings.Timeout_Millisec); tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig); if (queryMode() == QextSerialPort::EventDriven) { readNotifier = new QSocketNotifier(fd, QSocketNotifier::Read, this); connect(readNotifier, SIGNAL(activated(int)), this, SIGNAL(readyRead())); }
/*! Opens a serial port. Note that this function does not specify which device to open. If you need to open a device by name, see QextSerialPort::open(const char*). This function has no effect if the port associated with the class is already open. The port is also configured to the current settings, as stored in the Settings structure. */ bool QextSerialPort::open(OpenMode mode) { unsigned long confSize = sizeof(COMMCONFIG); Win_CommConfig.dwSize = confSize; DWORD dwFlagsAndAttributes = 0; if (queryMode() == QextSerialPort::EventDriven) dwFlagsAndAttributes += FILE_FLAG_OVERLAPPED; QMutexLocker lock(mutex); if (mode == QIODevice::NotOpen) return isOpen(); if (!isOpen()) { /*open the port*/ Win_Handle=CreateFileA(port.toAscii(), GENERIC_READ|GENERIC_WRITE, 0, NULL, OPEN_EXISTING, dwFlagsAndAttributes, NULL); if (Win_Handle!=INVALID_HANDLE_VALUE) { QIODevice::open(mode); /*configure port settings*/ GetCommConfig(Win_Handle, &Win_CommConfig, &confSize); GetCommState(Win_Handle, &(Win_CommConfig.dcb)); /*set up parameters*/ Win_CommConfig.dcb.fBinary=TRUE; Win_CommConfig.dcb.fInX=FALSE; Win_CommConfig.dcb.fOutX=FALSE; Win_CommConfig.dcb.fAbortOnError=FALSE; Win_CommConfig.dcb.fNull=FALSE; setBaudRate(Settings.BaudRate); setDataBits(Settings.DataBits); setStopBits(Settings.StopBits); setParity(Settings.Parity); setFlowControl(Settings.FlowControl); setTimeout(Settings.Timeout_Millisec); SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG)); //init event driven approach if (queryMode() == QextSerialPort::EventDriven) { Win_CommTimeouts.ReadIntervalTimeout = MAXDWORD; Win_CommTimeouts.ReadTotalTimeoutMultiplier = 0; Win_CommTimeouts.ReadTotalTimeoutConstant = 0; Win_CommTimeouts.WriteTotalTimeoutMultiplier = 0; Win_CommTimeouts.WriteTotalTimeoutConstant = 0; SetCommTimeouts(Win_Handle, &Win_CommTimeouts); if (!SetCommMask( Win_Handle, EV_TXEMPTY | EV_RXCHAR | EV_DSR)) { qWarning() << "failed to set Comm Mask. Error code:", GetLastError(); return false; } winEventNotifier = new QWinEventNotifier(overlap.hEvent, this); connect(winEventNotifier, SIGNAL(activated(HANDLE)), this, SLOT(onWinEvent(HANDLE))); WaitCommEvent(Win_Handle, &eventMask, &overlap); } } } else { return false; } return isOpen(); }
/*! \fn Posix_QextSerialPort::Posix_QextSerialPort(const char* name, const PortSettings& settings) Constructs a port with specified name and settings. */ Posix_QextSerialPort::Posix_QextSerialPort(const char* name, const PortSettings& settings) :QextSerialBase(name) { construct(); setBaudRate(settings.BaudRate); setDataBits(settings.DataBits); setStopBits(settings.StopBits); setParity(settings.Parity); setFlowControl(settings.FlowControl); setTimeout(settings.Timeout_Sec, settings.Timeout_Millisec); }
/*! \fn bool Posix_QextSerialPort::open(OpenMode mode) Opens the serial port associated to this class. This function has no effect if the port associated with the class is already open. The port is also configured to the current settings, as stored in the Settings structure. */ bool Posix_QextSerialPort::open(OpenMode mode) { LOCK_MUTEX(); if (mode == QIODevice::NotOpen) { UNLOCK_MUTEX(); return isOpen(); } if (!isOpen()) { /*open the port*/ Posix_File->setFileName(port); QueueReceiveSignals = 10; if (Posix_File->open(QIODevice::ReadWrite | QIODevice::Unbuffered)) { /*set open mode*/ QIODevice::open(mode); /*configure port settings*/ tcgetattr(Posix_File->handle(), &Posix_CommConfig); /*set up other port settings*/ Posix_CommConfig.c_cflag|=CREAD|CLOCAL; Posix_CommConfig.c_lflag&=(~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG)); Posix_CommConfig.c_iflag&=(~(INPCK|IGNPAR|IGNBRK|PARMRK|ISTRIP|ICRNL|IXANY)); Posix_CommConfig.c_oflag&=(~OPOST); Posix_CommConfig.c_cc[VMIN]=0; Posix_CommConfig.c_cc[VINTR] = _POSIX_VDISABLE; Posix_CommConfig.c_cc[VQUIT] = _POSIX_VDISABLE; Posix_CommConfig.c_cc[VSTART] = _POSIX_VDISABLE; Posix_CommConfig.c_cc[VSTOP] = _POSIX_VDISABLE; Posix_CommConfig.c_cc[VSUSP] = _POSIX_VDISABLE; setBaudRate(Settings.BaudRate); setDataBits(Settings.DataBits); setParity(Settings.Parity); setStopBits(Settings.StopBits); setFlowControl(Settings.FlowControl); //setTimeout(Settings.Timeout_Sec, Settings.Timeout_Millisec); setTimeout(Settings.Timeout_Millisec); tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig); handle = Posix_File->handle(); readerThread->handle = handle; readerThread->shutdown = false; readerThread->start(); } else { qDebug("Could not open File! Error code : %d", Posix_File->error()); } } UNLOCK_MUTEX(); return isOpen(); }
/*! * \brief SerialDev::configPort - Parametri per configurare la porta seriale * \return true se riesce a configurare correttamente la porta seriale */ bool SerialDev::configPort (const QString &name) { bool debugVal = m_debug; m_debug = true; setPortName(name); if (!open(QIODevice::ReadWrite)) { QString testo = QString("Can't open %1, error code %2") .arg(portName()).arg(error()); debug(testo); return false; } if (!setBaudRate(QSerialPort::Baud115200)) { QString testo = QString("Can't set rate 115200 baud to port %1, error code %2") .arg(portName()).arg(error()); debug(testo); return false; } if (!setDataBits(QSerialPort::Data8)) { QString testo = QString("Can't set 8 data bits to port %1, error code %2") .arg(portName()).arg(error()); debug(testo); return false; } if (!setParity(QSerialPort::NoParity)) { QString testo = QString("Can't set no patity to port %1, error code %2") .arg(portName()).arg(error()); debug(testo); return false; } if (!setStopBits(QSerialPort::OneStop)) { QString testo = QString("Can't set 1 stop bit to port %1, error code %2") .arg(portName()).arg(error()); debug(testo); return false; } if (!setFlowControl(QSerialPort::NoFlowControl)) { QString testo = QString("Can't set no flow control to port %1, error code %2") .arg(portName()).arg(error()); debug(testo); return false; } connect(this, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(errorSlot(QSerialPort::SerialPortError))); connect(this, SIGNAL(readyRead()), this, SLOT(fromDeviceSlot())); m_debug = debugVal; return true; }
/*! \fn Win_QextSerialPort::Win_QextSerialPort(const PortSettings& settings) Constructs a port with default name and specified settings. */ Win_QextSerialPort::Win_QextSerialPort(const PortSettings& settings, QextSerialBase::QueryMode mode) { Win_Handle=INVALID_HANDLE_VALUE; setBaudRate(settings.BaudRate); setDataBits(settings.DataBits); setStopBits(settings.StopBits); setParity(settings.Parity); setFlowControl(settings.FlowControl); setTimeout(settings.Timeout_Millisec); setQueryMode(mode); init(); }
/*! \fn Posix_QextSerialPort::Posix_QextSerialPort(const PortSettings& settings) Constructs a port with default name and specified settings. */ Posix_QextSerialPort::Posix_QextSerialPort(const PortSettings& settings) : QextSerialBase() { setBaudRate(settings.BaudRate); setDataBits(settings.DataBits); setParity(settings.Parity); setStopBits(settings.StopBits); setFlowControl(settings.FlowControl); Posix_File=new QFile(); setTimeout(settings.Timeout_Sec, settings.Timeout_Millisec); }
void QextSerialPortPrivate::setPortSettings(const PortSettings &settings, bool update) { setBaudRate(settings.BaudRate, false); setDataBits(settings.DataBits, false); setStopBits(settings.StopBits, false); setParity(settings.Parity, false); setFlowControl(settings.FlowControl, false); setTimeout(settings.Timeout_Millisec, false); settingsDirtyFlags = DFE_ALL; if (update && q_func()->isOpen()) updatePortSettings(); }
/*! Constructs a port with default name and specified settings. */ QextSerialPort::QextSerialPort(const PortSettings& settings, QextSerialPort::QueryMode mode) : QIODevice() { construct(); setBaudRate(settings.BaudRate); setDataBits(settings.DataBits); setParity(settings.Parity); setStopBits(settings.StopBits); setFlowControl(settings.FlowControl); setTimeout(settings.Timeout_Millisec); setQueryMode(mode); platformSpecificInit(); }
/*! \fn Posix_QextSerialPort::Posix_QextSerialPort(const PortSettings& settings) Constructs a port with default name and specified settings. */ Posix_QextSerialPort::Posix_QextSerialPort(const PortSettings& settings, QextSerialBase::QueryMode mode) : QextSerialBase() { setBaudRate(settings.BaudRate); setDataBits(settings.DataBits); setParity(settings.Parity); setStopBits(settings.StopBits); setFlowControl(settings.FlowControl); setTimeout(settings.Timeout_Millisec); setQueryMode(mode); init(); }
SerialPortSettings::SerialPortSettings(QSerialPortInfo serialPortInfo, QSerialPort::DataBits dataBits, QSerialPort::StopBits stopBits, QSerialPort::Parity parityBits, QSerialPort::BaudRate baudRate, QSerialPort::FlowControl flowControl) { setSerialPortInfo(serialPortInfo); setDataBits(dataBits); setStopBits(stopBits); setParityBits(parityBits); setBaudRate(baudRate); setFlowControl(flowControl); }
/*! \fn Posix_QextSerialPort::construct(void) Common constructor function, called by all versions of Posix_QextSerialPort::Posix_QextSerialPort(). Sets up default port settings (115200 8N1 Hardware flow control where supported, otherwise no flow control, and 500 ms timeout). */ void Posix_QextSerialPort::construct(void) { QextSerialBase::construct(); #ifdef NOQFILE m_fdFile=-1; #else Posix_File=new QFile(); #endif setBaudRate(BAUD115200); setDataBits(DATA_8); setStopBits(STOP_1); setParity(PAR_NONE); setFlowControl(FLOW_HARDWARE); setTimeout(0, 500); }
int main( int argc, char const * const argv[] ) { if( 2 <= argc ) { char const *const deviceName = argv[1]; serialSignal_t ss( deviceName, handler ); if( ss.isOpen() ) { unsigned baud = ( 2 < argc ) ? strtoul( argv[2], 0, 0 ) : 115200 ; int rval = setBaud( ss.getFd(), baud ); if( 0 == rval ) debugPrint( "set baud to %u\n", baud ); else printf( "error setting baud to %u\n", baud ); unsigned dataBits = ( 3 < argc ) ? strtoul( argv[3], 0, 0 ) : 8 ; rval = setDataBits( ss.getFd(), dataBits ); if( 0 == rval ) debugPrint( "set data bits to %u\n", dataBits ); else printf( "error setting data bits to %u\n", dataBits ); char parity = ( 4 < argc ) ? toupper( *argv[4] ) : 'N' ; rval = setParity( ss.getFd(), parity ); if( 0 == rval ) debugPrint( "set parity to %c\n", parity ); else printf( "error setting parity to %c\n", parity ); unsigned stopBits = ( ( 5 < argc ) && ( '2' == *argv[5] ) ) ? 2 : 1 ; rval = setStopBits( ss.getFd(), stopBits ); if( 0 == rval ) debugPrint( "set stop bits to %u\n", stopBits ); else printf( "error setting stop bits to %u\n", stopBits ); debugPrint( "device opened\n" ); while( 1 ){ pause(); } } else perror( deviceName ); } else fprintf( stderr, "Usage: serialSignal deviceName [baud=115200 [databits=8 [parity=N]]]\n" ); return 0 ; }
/*! \fn bool Posix_QextSerialPort::open(OpenMode mode) Opens the serial port associated to this class. This function has no effect if the port associated with the class is already open. The port is also configured to the current settings, as stored in the Settings structure. */ bool Posix_QextSerialPort::open(OpenMode mode) { LOCK_MUTEX(); if (mode == QIODevice::NotOpen) return isOpen(); if (!isOpen()) { /*open the port*/ qDebug("trying to open file"); //note: linux 2.6.21 seems to ignore O_NDELAY flag if ((fd = ::open(port.toAscii() ,O_RDWR | O_NOCTTY | O_NDELAY)) != -1) { qDebug("file opened succesfully"); setOpenMode(mode); // Flag the port as opened tcgetattr(fd, &old_termios); // Save the old termios Posix_CommConfig = old_termios; // Make a working copy cfmakeraw(&Posix_CommConfig); // Enable raw access /*set up other port settings*/ Posix_CommConfig.c_cflag|=CREAD|CLOCAL; Posix_CommConfig.c_lflag&=(~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG)); Posix_CommConfig.c_iflag&=(~(INPCK|IGNPAR|PARMRK|ISTRIP|ICRNL|IXANY)); Posix_CommConfig.c_oflag&=(~OPOST); Posix_CommConfig.c_cc[VMIN]= 0; #ifdef _POSIX_VDISABLE // Is a disable character available on this system? // Some systems allow for per-device disable-characters, so get the // proper value for the configured device const long vdisable = fpathconf(fd, _PC_VDISABLE); Posix_CommConfig.c_cc[VINTR] = vdisable; Posix_CommConfig.c_cc[VQUIT] = vdisable; Posix_CommConfig.c_cc[VSTART] = vdisable; Posix_CommConfig.c_cc[VSTOP] = vdisable; Posix_CommConfig.c_cc[VSUSP] = vdisable; #endif //_POSIX_VDISABLE setBaudRate(Settings.BaudRate); setDataBits(Settings.DataBits); setParity(Settings.Parity); setStopBits(Settings.StopBits); setFlowControl(Settings.FlowControl); setTimeout(Settings.Timeout_Millisec); tcsetattr(fd, TCSAFLUSH, &Posix_CommConfig); } else { qDebug("could not open file: %s", strerror(errno)); } } UNLOCK_MUTEX(); return isOpen(); }
bool SerialPort::initPort(const uchar initialBaudrate, const ulong timeout) { uchar baudrate = initialBaudrate % 7; if (baudrate == 0) // Adapt baud { setBaudRate(BAUD2400); qDebug() << "Baudrate" << "2.4k"; } else if (baudrate == 1) { setBaudRate(BAUD4800); qDebug() << "Baudrate" << "4.8k"; } else if (baudrate == 2) { setBaudRate(BAUD9600); qDebug() << "Baudrate" << "9.6k"; } else if (baudrate == 3) { setBaudRate(BAUD19200); qDebug() << "Baudrate" << "19.2k"; } else if (baudrate == 4) { setBaudRate(BAUD38400); qDebug() << "Baudrate" << "38.4k"; } else if (baudrate == 5) { setBaudRate(BAUD57600); qDebug() << "Baudrate" << "57.6k"; } else if (baudrate == 6) { setBaudRate(BAUD115200); qDebug() << "Baudrate" << "115.2k"; } setFlowControl(FLOW_OFF); setParity(PAR_NONE); setDataBits(DATA_8); setStopBits(STOP_1); setTimeout(timeout); return open(QIODevice::ReadWrite); }
/*! * \brief Rs232DevicePrivate::configPort - Parametri per configurare la porta seriale * \return true se riesce a configurare correttamente la porta seriale */ bool Rs232DevicePrivate::configPort () { if (!open(QIODevice::ReadWrite)) { QString testo = QString("Can't open %1, error code %2") .arg(portName()).arg(error()); debug(testo); return false; } if (!setBaudRate(QSerialPort::Baud115200)) { QString testo = QString("Can't set rate 115200 baud to port %1, error code %2") .arg(portName()).arg(error()); debug(testo); return false; } if (!setDataBits(QSerialPort::Data8)) { QString testo = QString("Can't set 8 data bits to port %1, error code %2") .arg(portName()).arg(error()); debug(testo); return false; } if (!setParity(QSerialPort::NoParity)) { QString testo = QString("Can't set no patity to port %1, error code %2") .arg(portName()).arg(error()); debug(testo); return false; } if (!setStopBits(QSerialPort::OneStop)) { QString testo = QString("Can't set 1 stop bit to port %1, error code %2") .arg(portName()).arg(error()); debug(testo); return false; } if (!setFlowControl(QSerialPort::NoFlowControl)) { QString testo = QString("Can't set no flow control to port %1, error code %2") .arg(portName()).arg(error()); debug(testo); return false; } return true; }
/*! * \brief 功能概述 打开串口 * \param 参数描述 strPortName_是串口名,strBaudNo_是波特率,strDataBits_是数据位,strStopBits_是停止位,strParity_是校验方式 * \return 返回值描述 成功打开返回true,若打开失败则返回false * \author zzy * \date 2015/5/27 */ bool CSerialPort::OpenSerialPort(const QString strPortName_, const QString strBaudNo_, const QString strDataBits_, const QString strStopBits_, const QString strParity_) { InitBaudNo(strBaudNo_); InitDataBits(strDataBits_); InitStopBits(strStopBits_); InitParity(strParity_); ///打开串口 QString tempPortName = strPortName_; setPortName(strPortName_); if (!open(QIODevice::ReadWrite)) { qDebug()<<QString("Can't open %1, error code %2").arg(portName()).arg(error()); int nPortNumber = tempPortName.remove("COM").toInt(); QString strPortName2 = tr("ttyS%1").arg(nPortNumber); setPortName(strPortName2); if (!open(QIODevice::ReadWrite)) { qDebug()<<QString("Can't open %1, error code %2").arg(portName()).arg(error()); QString strPortName3 = tr("ttyO%1").arg(nPortNumber); setPortName(strPortName3); if (!open(QIODevice::ReadWrite)) { qDebug()<<QString("Can't open %1, error code %2").arg(portName()).arg(error()); return false; } } } /// 设置波特率 setBaudRate((QSerialPort::BaudRate)m_nBaudNo); /// 设置数据位 setDataBits((QSerialPort::DataBits)m_nDataBits); /// 设置停止位 setStopBits((QSerialPort::StopBits)m_nStopBits); /// 设置校验方式 setParity((QSerialPort::Parity)m_nParity); if (!setFlowControl(QSerialPort::NoFlowControl)) { qDebug()<<QString("Can't set no flow control to port %1, error code %2").arg(portName()).arg(error()); return false; } return true; }
Connector::Connector() { //readOp = {0x09, 0xAC, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x02}; readOp[0] = 0x09; // 数据总数 readOp[1] = 0xAC; // 读数据 readOp[2] = 0xFF; // 密码 readOp[3] = 0xFF; readOp[4] = 0xFF; readOp[5] = 0xFF; readOp[6] = 0xFF; readOp[7] = 0xFF; readOp[8] = 0x01; // 起始扇区 readOp[9] = 0x02; // 扇区数(最多5) //waitOp = {0x01, 0xAD}; waitOp[0] = 0x01; // 数据总数 waitOp[1] = 0xAD; // 关射频 //writeOp = {0x09, 0xAB, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0x02}; writeOp[0] = 0x09; // 数据总数 writeOp[1] = 0xAB; // 写数据 writeOp[2] = 0xFF; // 密码 writeOp[3] = 0xFF; writeOp[4] = 0xFF; writeOp[5] = 0xFF; writeOp[6] = 0xFF; writeOp[7] = 0xFF; writeOp[8] = 0x01; // 起始扇区 writeOp[9] = 0x02; // 扇区数 //后面跟数据,最长240byte //读取数据的返回头 returnHead[0] = 0x5A; returnHead[1] = 0x59; returnHead[2] = 0x48; returnHead[3] = 0x02; setPortName("/dev/ttyO5"); setBaudRate(QSerialPort::Baud9600); setDataBits(QSerialPort::Data8); setStopBits(QSerialPort::OneStop); setParity(QSerialPort::NoParity); open(QIODevice::ReadWrite); timer.setInterval(2000); connect(&timer, SIGNAL(timeout()), this, SLOT(onTimeOut())); connect(this, SIGNAL(readOK(QByteArray)), this, SLOT(onReadOK(QByteArray))); startRead(1, 4); }
bool rs232_port::port_open(const QString & port_name, QSerialPort::BaudRate baud_rate, PORT_PROTOCOL_T prot) { setPortName(port_name); setBaudRate(baud_rate); // 115200 setFlowControl(NoFlowControl); setParity(NoParity); // паритет отключен setDataBits(Data8); // 8 бит setStopBits(OneStop); // один стоповый if (!open(QIODevice::ReadWrite)) { this -> port_name.clear(); return false; } this -> port_name = port_name; this -> prot = prot; qDebug() << tr("RS232 %1 was opened").arg(port_name); return true; }
/*! \fn bool Win_QextSerialPort::open(OpenMode mode) Opens a serial port. Note that this function does not specify which device to open. If you need to open a device by name, see Win_QextSerialPort::open(const char*). This function has no effect if the port associated with the class is already open. The port is also configured to the current settings, as stored in the Settings structure. */ bool Win_QextSerialPort::open(OpenMode mode) { unsigned long confSize = sizeof(COMMCONFIG); Win_CommConfig.dwSize = confSize; LOCK_MUTEX(); if (mode == QIODevice::NotOpen) return isOpen(); if (!isOpen()) { /*open the port*/ Win_Handle=CreateFileA(port.toLatin1(), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); if (Win_Handle!=INVALID_HANDLE_VALUE) { /*set open mode*/ QIODevice::open(mode); /*configure port settings*/ GetCommConfig(Win_Handle, &Win_CommConfig, &confSize); GetCommState(Win_Handle, &(Win_CommConfig.dcb)); /*set up parameters*/ Win_CommConfig.dcb.fBinary=TRUE; Win_CommConfig.dcb.fInX=FALSE; Win_CommConfig.dcb.fOutX=FALSE; Win_CommConfig.dcb.fAbortOnError=FALSE; Win_CommConfig.dcb.fNull=FALSE; setBaudRate(Settings.BaudRate); setDataBits(Settings.DataBits); setStopBits(Settings.StopBits); setParity(Settings.Parity); setFlowControl(Settings.FlowControl); setTimeout(Settings.Timeout_Sec, Settings.Timeout_Millisec); SetCommConfig(Win_Handle, &Win_CommConfig, sizeof(COMMCONFIG)); } } UNLOCK_MUTEX(); return isOpen(); }
int SerialPort::openPort( const QString& port, int baudrate, SerialDataBits dataBits, SerialStopBits stopBits, SerialParity parity) { if (mIsOpen) { closePort(); } mFd = open(port.toLocal8Bit().data(), O_RDWR | O_NOCTTY | O_NDELAY); if (mFd == -1) { qCritical() << "Opening serial port failed."; return -1; } mIsOpen = true; // No-blocking reads fcntl(mFd, F_SETFL, FNDELAY); struct termios options; if (0 != tcgetattr(mFd, &options)) { qCritical() << "Reading serial port options failed."; return -2; } // Enable the receiver and set local mode... options.c_cflag |= CLOCAL | CREAD; // Raw input options.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG); // Disable flow control options.c_cflag &= ~CRTSCTS; options.c_iflag &= ~(IXON|IXOFF|IXANY); // ??? /*set up other port settings*/ options.c_cflag |= CREAD|CLOCAL; options.c_lflag &= (~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|ISIG)); options.c_iflag &= (~(INPCK|IGNPAR|PARMRK|ISTRIP|ICRNL|IXANY)); options.c_oflag &= (~OPOST); options.c_cc[VMIN] = 0; #ifdef _POSIX_VDISABLE // Is a disable character available on this system? // Some systems allow for per-device disable-characters, so get the // proper value for the configured device const long vdisable = ::fpathconf(mFd, _PC_VDISABLE); options.c_cc[VINTR] = vdisable; options.c_cc[VQUIT] = vdisable; options.c_cc[VSTART] = vdisable; options.c_cc[VSTOP] = vdisable; options.c_cc[VSUSP] = vdisable; #endif //_POSIX_VDISABLE //Set the new options for the port... if (0 != tcsetattr(mFd, TCSANOW, &options)) { qCritical() << "Writing serial port options failed."; closePort(); return -3; } if (false == setDataBits(dataBits)) { qCritical() << "Setting data bits failed."; closePort(); return -4; } if (false == setStopBits(stopBits)) { qCritical() << "Setting stopbits failed."; closePort(); return -5; } if (false == setParity(parity)) { qCritical() << "Setting parity faield."; closePort(); return -6; } if (false == setBaudrate(baudrate)) { qCritical() << "Setting baudrate failed."; closePort(); return -7; } mAbort = false; start(LowPriority); return 0; }