/*! 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 Win_QextSerialPort::Win_QextSerialPort(const QString & name, const PortSettings& settings) Constructs a port with specified name and settings. */ Win_QextSerialPort::Win_QextSerialPort(const QString & name, const PortSettings& settings) { Win_Handle=INVALID_HANDLE_VALUE; setPortName(name); 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; }
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); }
/*! * \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; }
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(); }
/*! \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*/ #ifdef NOQFILE if ((m_fdFile=::open((const char *)QFile::encodeName(portName),O_NOCTTY | O_RDWR | O_SYNC))!=-1) { portOpen=true; #else Posix_File->setName(portName); if (Posix_File->open(/*IO_Async| */ IO_Raw| IO_ReadWrite)) { portOpen=true; #endif /*configure port settings*/ #ifdef NOQFILE tcgetattr(m_fdFile, &Posix_CommConfig); #else tcgetattr(Posix_File->handle(), &Posix_CommConfig); #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; 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; #ifdef NOQFILE tcsetattr(m_fdFile, TCSAFLUSH, &Posix_CommConfig); #else tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig); #endif 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); } } UNLOCK_MUTEX(); return portOpen; } /*! \fn void Posix_QextSerialPort::close() Closes a serial port. This function has no effect if the serial port associated with the class is not currently open. */ void Posix_QextSerialPort::close() { LOCK_MUTEX(); #ifdef NOQFILE ::close(m_fdFile); m_fdFile=-1; #else Posix_File->close(); #endif portOpen=false; UNLOCK_MUTEX(); }
bool mdtSerialPortPosix::open(mdtSerialPortConfig &cfg) { QString strNum; // Close previous opened device this->close(); // Try to open port // O_RDWR : Read/write access // O_NOCTTY: not a terminal // O_NDELAY: ignore DCD signal lockMutex(); // In read only mode, we handle no lock if(cfg.readOnly()){ pvFd = ::open(pvName.toStdString().c_str(), O_RDONLY | O_NOCTTY | O_NONBLOCK); }else{ pvFd = pvPortLock->openLocked(pvName, O_RDWR | O_NOCTTY | O_NONBLOCK); } if(pvFd < 0){ pvPortLock->unlock(); mdtError e(MDT_UNDEFINED_ERROR, "Unable to open port: " + pvName, mdtError::Error); e.setSystemError(errno, strerror(errno)); MDT_ERROR_SET_SRC(e, "mdtSerialPortPosix"); e.commit(); unlockMutex(); return false; } // Get current config and save it to pvOriginalTermios if(tcgetattr(pvFd, &pvOriginalTermios) < 0){ ::close(pvFd); pvFd = -1; pvPortLock->unlock(); mdtError e(MDT_UNDEFINED_ERROR, "tcgetattr() failed, " + pvName + " is not a serial port, or is not available", mdtError::Error); e.setSystemError(errno, strerror(errno)); MDT_ERROR_SET_SRC(e, "mdtSerialPortPosix"); e.commit(); unlockMutex(); return false; } // Get current system config on "work copy" if(tcgetattr(pvFd, &pvTermios) < 0){ ::close(pvFd); pvFd = -1; pvPortLock->unlock(); mdtError e(MDT_UNDEFINED_ERROR, "tcgetattr() failed, " + pvName + " is not a serial port, or is not available", mdtError::Error); e.setSystemError(errno, strerror(errno)); MDT_ERROR_SET_SRC(e, "mdtSerialPortPosix"); e.commit(); unlockMutex(); return false; } // Set baud rate if(!setBaudRate(cfg.baudRate())){ ::close(pvFd); pvFd = -1; pvPortLock->unlock(); strNum.setNum(cfg.baudRate()); mdtError e(MDT_UNDEFINED_ERROR, "unsupported baud rate '" + strNum + "' for port " + pvName, mdtError::Error); e.setSystemError(errno, strerror(errno)); MDT_ERROR_SET_SRC(e, "mdtSerialPortPosix"); e.commit(); unlockMutex(); return false; } // Set local mode and enable the receiver pvTermios.c_cflag |= (CLOCAL | CREAD); // Set data bits if(!setDataBits(cfg.dataBitsCount())){ ::close(pvFd); pvFd = -1; pvPortLock->unlock(); strNum.setNum(cfg.dataBitsCount()); mdtError e(MDT_UNDEFINED_ERROR, "unsupported data bits count '" + strNum + "' for port " + pvName, mdtError::Error); MDT_ERROR_SET_SRC(e, "mdtSerialPortPosix"); e.commit(); unlockMutex(); return false; } // Set stop bits if(!setStopBits(cfg.stopBitsCount())){ ::close(pvFd); pvFd = -1; pvPortLock->unlock(); strNum.setNum(cfg.stopBitsCount()); mdtError e(MDT_UNDEFINED_ERROR, "unsupported stop bits count '" + strNum + "' for port " + pvName, mdtError::Error); MDT_ERROR_SET_SRC(e, "mdtSerialPortPosix"); e.commit(); unlockMutex(); return false; } // Set parity setParity(cfg.parity()); // Set flow control setFlowCtlRtsCts(cfg.flowCtlRtsCtsEnabled()); setFlowCtlXonXoff(cfg.flowCtlXonXoffEnabled(), cfg.xonChar(), cfg.xoffChar()); // Set raw data mode pvTermios.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); pvTermios.c_oflag &= ~OPOST; // Apply the setup if(tcsetattr(pvFd, TCSANOW, &pvTermios) < 0){ ::close(pvFd); pvFd = -1; pvPortLock->unlock(); mdtError e(MDT_UNDEFINED_ERROR, "unable to apply configuration for port " + pvName, mdtError::Error); e.setSystemError(errno, strerror(errno)); MDT_ERROR_SET_SRC(e, "mdtSerialPortPosix"); e.commit(); unlockMutex(); return false; } // Check if configuration could really be set if(!checkConfig(cfg)){ ::close(pvFd); pvFd = -1; pvPortLock->unlock(); unlockMutex(); return false; } // Set the read/write timeouts setReadTimeout(cfg.readTimeout()); setWriteTimeout(cfg.writeTimeout()); return mdtAbstractPort::open(cfg); }
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; }
/*! \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); m_WinCommConfig.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*/ m_WinHandle=CreateFileA(port.toAscii(), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, dwFlagsAndAttributes, NULL); if (m_WinHandle!=INVALID_HANDLE_VALUE) { /*configure port settings*/ GetCommConfig(m_WinHandle, &m_WinCommConfig, &confSize); GetCommState(m_WinHandle, &(m_WinCommConfig.dcb)); /*set up parameters*/ m_WinCommConfig.dcb.fBinary=TRUE; m_WinCommConfig.dcb.fInX=FALSE; m_WinCommConfig.dcb.fOutX=FALSE; m_WinCommConfig.dcb.fAbortOnError=FALSE; m_WinCommConfig.dcb.fNull=FALSE; setBaudRate(Settings.BaudRate); setDataBits(Settings.DataBits); setStopBits(Settings.StopBits); setParity(Settings.Parity); setFlowControl(Settings.FlowControl); setTimeout(Settings.Timeout_Millisec); SetCommConfig(m_WinHandle, &m_WinCommConfig, sizeof(COMMCONFIG)); //init event driven approach if (queryMode() == QextSerialBase::EventDriven) { m_WinCommTimeouts.ReadIntervalTimeout = MAXDWORD; m_WinCommTimeouts.ReadTotalTimeoutMultiplier = 0; m_WinCommTimeouts.ReadTotalTimeoutConstant = 0; m_WinCommTimeouts.WriteTotalTimeoutMultiplier = 0; m_WinCommTimeouts.WriteTotalTimeoutConstant = 0; SetCommTimeouts(m_WinHandle, &m_WinCommTimeouts); if (!SetCommMask( m_WinHandle, EV_TXEMPTY | EV_RXCHAR | EV_DSR)) { qWarning("Failed to set Comm Mask. Error code: %ld", GetLastError()); UNLOCK_MUTEX(); return false; } m_pOverlapThread->start(); } QIODevice::open(mode); } } else { UNLOCK_MUTEX(); return false; } UNLOCK_MUTEX(); return isOpen(); }
/*! \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(); } // Make sure port is actually a serial device (and not e.g. /dev/null) char namebuf[1024]; strncpy(namebuf, port.toAscii().constData(), sizeof(namebuf)); struct stat buf; do { if (lstat(namebuf, &buf) == -1 ) { ErrorPrintf("Couldn't stat %s: %s", namebuf, strerror(errno)); UNLOCK_MUTEX(); return isOpen(); } if( S_ISLNK(buf.st_mode) ) { char link_target[1024]; ssize_t linklen; linklen = readlink(namebuf, link_target, sizeof(link_target)); if( linklen <= 0 ) { DbgPrintf("readlink on %s failed\n", namebuf); UNLOCK_MUTEX(); return isOpen(); } link_target[linklen] = '\0'; if( link_target[0] == '/' ) strcpy(namebuf, link_target); else { char *last_slash = strrchr(namebuf, '/'); last_slash++; strcpy(last_slash, link_target); } } } while( S_ISLNK(buf.st_mode) ); // This prevents the HDS units from spending all available CPU reading a file that is created in place of the // serial port when the serial console is turned on // if (MAJOR(buf.st_rdev) != TTY_MAJOR && MAJOR(buf.st_rdev) != TTS_MAJOR && MAJOR(buf.st_rdev) != USBSERIAL_MAJOR && MAJOR(buf.st_rdev) != ACMSERIAL_MAJOR) if( strstr(namebuf, "tty") == NULL && strstr(namebuf, "tts") == NULL ) { //ErrorPrintf("%s is not a serial port (real device name: %s)", port.toAscii().constData(), namebuf); UNLOCK_MUTEX(); return isOpen(); } if (!isOpen()) { /*open the port*/ Posix_File->setFileName(port); qDebug("Trying to open File"); if (Posix_File->exists() && Posix_File->open(QIODevice::ReadWrite|QIODevice::Unbuffered|QIODevice::Append)) { qDebug("Opened File succesfully"); /*set open mode*/ QIODevice::open(mode); /*configure port settings*/ tcgetattr(Posix_File->handle(), &Posix_CommConfig); #if 0 Posix_CommConfig.c_cc[VMIN]=1; Posix_CommConfig.c_cc[VTIME]=0; #elif 1 //Raw settings //same as -ignbrk -brkint -ignpar -parmrk -inpck -istrip -inlcr //-igncr -icrnl -ixon -ixoff -iuclc -ixany -imaxbel -opost //-isig -icanon -xcase min 1 time 0 /*set up other port settings*/ Posix_CommConfig.c_cflag|=CREAD|CLOCAL; Posix_CommConfig.c_lflag&=(~(IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|ISTRIP)); Posix_CommConfig.c_iflag&=(~(INLCR|IGNCR|ICRNL|IXON|IXOFF|IUCLC|IXANY)); Posix_CommConfig.c_iflag&=(~(IMAXBEL|ISIG|ICANON|XCASE)); Posix_CommConfig.c_oflag&=(~OPOST); Posix_CommConfig.c_cc[VMIN]=1; Posix_CommConfig.c_cc[VTIME]=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; #else 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; 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; #endif setBaudRate(Settings.BaudRate); setDataBits(Settings.DataBits); setParity(Settings.Parity); setStopBits(Settings.StopBits); //setFlowControl(Settings.FlowControl); setTimeout(Settings.Timeout_Millisec); tcsetattr(Posix_File->handle(), TCSAFLUSH, &Posix_CommConfig); fcntl(Posix_File->handle(), F_SETFL, fcntl(Posix_File->handle(), F_GETFL, 0) | O_NONBLOCK); } else { qDebug("Could not open File! Error code : %d", Posix_File->error()); } } UNLOCK_MUTEX(); return isOpen(); }