if (port->waitForReadyRead(1000)) { receiveMsg();}
connect(port, SIGNAL(readyRead()), this, SLOT(setReady()));.............................waitReadyRead(1000);if (ready) { receiveMsg();}..............................void Thread::setReady(){ ready = true; emit sendMessage("eee");}void Thread::waitReadyRead(int msec){ QTime timer; timer.start(); while ( (timer.elapsed() < msec)) { if (ready) break; msleep(1); }}
void Thread::waitReadyRead(int msec){ QTime timer; timer.start(); while ( (timer.elapsed() < msec)) { if (port->bytesAvailable() > 0) { ready = true; break; } msleep(1); }}
void qPThRead::run(){ int i;priem = true;recivString="";int len=0; if ( ComPort != NULL ){ while (priem) { if ( !stopReciv ) { len=0; len = ComPort->bytesWaiting() ; if ( len == ( unsigned int ) -1 ) continue; if (len >0) { stopReciv = true; mutex.lock(); for (i=0; i<len; i++) { recivString +=ComPort->getch(); } qPThReadEvent * event = new qPThReadEvent(1001); event->String = recivString; event->PortName = PortName; QApplication::postEvent(TargetWidget, event); recivString = ""; mutex.unlock(); } } } } }
C++ (Qt)void Mainwin::sendData(){ char data[1024]; QByteArray baSend; int numBlocks = ceil((float)dataToSend().length() / 64); QProgressDialog progress(tr("Sending Data..."), tr("Abort Sending"), 0, numBlocks, this); progress.setWindowModality(Qt::WindowModal); for (int i = 0; i < numBlocks; i++) { progress.setValue(i); baSend.clear(); baSend.append(0xAA); //startByte baSend.append(i>>8); //pageNum baSend.append(i & 0x0F); //pageNum baSend.append(dataToSend().mid(i*64, 64)); //64 bytes of data baSend.append(0xDD); //crc baSend.append(0xCC); //crc if (progress.wasCanceled()) break; port->write(baSend, baSend.length()); // wait for an answer // or timeout } progress.setValue(numBlocks);}