May 3, 2011

Mannion Mannion
Lab Rat
21 posts

QExtSerialPort port->write Problem

 

I seem to be having difficulty using the PortListener class. If I write to a connected port directly everything is fine. If I write via a public or private function using identical code I get an error:

“Application.exe has encountered a problem and needs to close.”

It seems a bizarre problem so I must be missing something really simple. My apologies in advance if this is the case. I have cut the code down to a minimal case that still generates the problem.

  1. MainWindow::MainWindow(QWidget *parent): QMainWindow(parent){
  2.     QWidget *w = new QWidget(this);
  3.     scPtr = new Session ();
  4.     QString portName = scPtr->enumerate();
  5.     if (portName == "NULL"){
  6.         qDebug() << "Serial Device NOT FOUND portName = " << portName;
  7.         int i = QMessageBox::warning(this,tr("Connection"),tr("No SerialDevices detected."),QMessageBox::Cancel);
  8.     }else{ qDebug() << "SerialDevice Found. portName = " << portName; }
  9.     PortListener *listener = new PortListener(portName);
  10.    
  11. //============ below does not work?===============
  12.     clickTEST0();
  13.    
  14. //============ below works======================
  15.     QByteArray ba;
  16.     ba.resize(3);
  17.     ba[0] = 254 ;              
  18.     ba[1] = 1;      
  19.     ba[2] = 255;
  20.     int i = listener->port->write(ba,ba.length());
  21. }
  22.  
  23. void MainWindow::clickTEST0() {
  24.     QByteArray ba;
  25.     ba.resize(3);
  26.     ba[0] = 254 ;              
  27.     ba[1] = 1;
  28.     ba[2] = 255;
  29.     int i = listener->port->write(ba,ba.length());
  30. }
  31.  
  32. Session::Session(){}
  33.  
  34. QString Session::enumerate(){
  35.     QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
  36.     int tempport = 9999;
  37.     QString tempportname = "NULL";
  38.     QString tempfriendname = "NULL";
  39.     QString qstr;              
  40.     int portsize = 0;
  41.     for (int i = 0; i < ports.size(); i++) {
  42.         portsize++;
  43.         qstr = ports.at(i).enumName;
  44.         if (qstr ==  SerialDeviceString) {
  45.             tempport = i;
  46.             tempportname = ports.at(i).portName;
  47.             tempfriendname = ports.at(i).friendName;
  48.         } else {}
  49.     }
  50.     QString portName = tempportname;              // update this to use your port of choice
  51.     if (portName == "NULL"){ qDebug() << "SerialDevice NOT FOUND portName = " << portName;
  52.     }else{ qDebug() << " SerialDevice FOUND portName = " << portName; }
  53.     return portName;
  54. }
  55.  
  56. PortListener::PortListener(const QString & portName) {  //, int iVal){     //portName = this->enumerate();
  57.     if (portName=="NULL"){ //NOT CONNECTED
  58.     }else{ SetPort(portName);  }
  59. }
  60.  
  61. void PortListener::SetPort(const QString & portName){  
  62.     this->port = new QextSerialPort(portName, QextSerialPort::EventDriven);
  63.     port->setBaudRate(BAUD115200);
  64.     port->setFlowControl(FLOW_OFF);
  65.     port->setParity(PAR_NONE);
  66.     port->setDataBits(DATA_8);
  67.     port->setStopBits(STOP_1);          
  68.     if (port->open(QIODevice::ReadWrite) == true) {
  69.         connect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
  70.         connect(port, SIGNAL(dsrChanged(bool)), this, SLOT(onDsrChanged(bool)));
  71.         if (!(port->lineStatus() & LS_DSR))
  72.             qDebug() << "warning: device is not turned on";
  73.         qDebug() << "listening for data on" << port->portName();
  74.     }
  75.     else {qDebug() << "device failed to open:" << port->errorString(); }
  76.     emit onReadyWrite("Listener Created!");
  77. }

4 replies

May 3, 2011

stuk stuk
Ant Farmer
540 posts

I have more problem with this class in the past, now i use this project https://gitorious.org/qserialdevice [gitorious.org] Maybe can help you.

May 4, 2011

Mannion Mannion
Lab Rat
21 posts

Thanks for the reply. I am aware of QSerialDevice but I have found QextSerialPort to be extremely reliable and very effective for my requirements. It has enabled me to quickly produce a lot of successful code and I am not particularly keen to start from scratch.

The line

PortListener *listener = new PortListener(portName);

seems to be the problem as it makes listener behave with local scope even though it is a member of the MainWindow class.

May 4, 2011

stuk stuk
Ant Farmer
540 posts

Ehehe for some user is reliable QextSerialPort, for other is QSerialDevice… why the team not join the force?
…this is only my consideration…sorry for the spam.

May 4, 2011

Andre Andre
Area 51 Engineer
6031 posts

Normally, you should not have to start from scratch at all, since they both simply subclass QIODevice. That means most of your code can stay stable.

 Signature 

Looking for Qt developers to join our team @ i-Optics: https://qt-project.org/forums/viewthread/25393/

 
  ‹‹ QExtSerialPort && waitForReadyRead      drawing cross-platform pie chart on a design form ››

You must log in to post a reply. Not a member yet? Register here!