Bytes_Available: 3 Bytes_Available: 3
Bytes_Available: 2 Bytes_Available: 4
MyMainWindow::MyMainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MyMainWindow){ ui->setupUi(this);...serialPort->open(AbstractSerial::ReadOnly| AbstractSerial::Unbuffered);...serialPort->setCharIntervalTimeout(10); connect(serialPort, SIGNAL(readyRead()), this, SLOT(serialDataReady())); }void MyMainWindow::serialDataReady(){ uint8_t num = serialPort->bytesAvailable(); // сколько байт в буфере qDebug() << "Bytes_Available:" << num; QByteArray ba; if ((serialPort->bytesAvailable() > 0)) { ba.clear(); ba = serialPort->read(num); qDebug() << "Readed is : " << ba.size() << " bytes"; } serialPort->reset();}
C++ (Qt)#if defined (Q_OS_UNIX) // Method setTotalReadConstantTimeout() not supported in *.nix. if (port->openMode() & AbstractSerial::Unbuffered) port->setCharIntervalTimeout(5000);//5 msec#elif defined (Q_OS_WIN) if (port->openMode() & AbstractSerial::Unbuffered) port->setTotalReadConstantTimeout(100); //100 msec#endif
C++ (Qt)port->setTotalReadConstantTimeout(100); //100 msec
MyMainWindow::MyMainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MyMainWindow){ ui->setupUi(this); QIODevice::OpenMode mode = QIODevice::ReadOnly; mode |= QIODevice::Unbuffered; if (!myPortCom->open(mode)) // Открываем порт { QMessageBox::critical(this, QString::fromLocal8Bit("Com port"), QString::fromLocal8Bit("Не могу открыть порт ")+ comPort, QMessageBox::Ok); ui->statusBar->showMessage(QString::fromLocal8Bit("Error opening COM port!")); } else { myPortCom->setRate(9600); myPortCom->setDataBits(SerialPort::Data8); myPortCom->setParity(SerialPort::NoParity); myPortCom->setStopBits(SerialPort::OneStop); myPortCom->setFlowControl(SerialPort::NoFlowControl); myPortCom->waitForReadyRead(5000); connect(myPortCom, SIGNAL(readyRead()), this, SLOT(serialDataReady())); } uint8_t num = myPortCom->bytesAvailable(); // байт в буфере qDebug() << "Bytes_Available:" << num;void MyMainWindow::serialDataReady(){ qDebug() << "Recieved";}
C++ (Qt)mode |= QIODevice::Unbuffered;