Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. 3rd Party Software
  4. QExtSerialPort port->write Problem
Forum Updated to NodeBB v4.3 + New Features

QExtSerialPort port->write Problem

Scheduled Pinned Locked Moved 3rd Party Software
5 Posts 3 Posters 6.3k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mannion
    wrote on last edited by
    #1

    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.

    @MainWindow::MainWindow(QWidget *parent): QMainWindow(parent){
    QWidget *w = new QWidget(this);
    scPtr = new Session ();
    QString portName = scPtr->enumerate();
    if (portName == "NULL"){
    qDebug() << "Serial Device NOT FOUND portName = " << portName;
    int i = QMessageBox::warning(this,tr("Connection"),tr("No SerialDevices detected."),QMessageBox::Cancel);
    }else{ qDebug() << "SerialDevice Found. portName = " << portName; }
    PortListener *listener = new PortListener(portName);

    //============ below does not work?===============
    clickTEST0();

    //============ below works======================
    QByteArray ba;
    ba.resize(3);
    ba[0] = 254 ;
    ba[1] = 1;
    ba[2] = 255;
    int i = listener->port->write(ba,ba.length());
    }

    void MainWindow::clickTEST0() {
    QByteArray ba;
    ba.resize(3);
    ba[0] = 254 ;
    ba[1] = 1;
    ba[2] = 255;
    int i = listener->port->write(ba,ba.length());
    }

    Session::Session(){}

    QString Session::enumerate(){
    QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
    int tempport = 9999;
    QString tempportname = "NULL";
    QString tempfriendname = "NULL";
    QString qstr;
    int portsize = 0;
    for (int i = 0; i < ports.size(); i++) {
    portsize++;
    qstr = ports.at(i).enumName;
    if (qstr == SerialDeviceString) {
    tempport = i;
    tempportname = ports.at(i).portName;
    tempfriendname = ports.at(i).friendName;
    } else {}
    }
    QString portName = tempportname; // update this to use your port of choice
    if (portName == "NULL"){ qDebug() << "SerialDevice NOT FOUND portName = " << portName;
    }else{ qDebug() << " SerialDevice FOUND portName = " << portName; }
    return portName;
    }

    PortListener::PortListener(const QString & portName) { //, int iVal){ //portName = this->enumerate();
    if (portName=="NULL"){ //NOT CONNECTED
    }else{ SetPort(portName); }
    }

    void PortListener::SetPort(const QString & portName){
    this->port = new QextSerialPort(portName, QextSerialPort::EventDriven);
    port->setBaudRate(BAUD115200);
    port->setFlowControl(FLOW_OFF);
    port->setParity(PAR_NONE);
    port->setDataBits(DATA_8);
    port->setStopBits(STOP_1);
    if (port->open(QIODevice::ReadWrite) == true) {
    connect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
    connect(port, SIGNAL(dsrChanged(bool)), this, SLOT(onDsrChanged(bool)));
    if (!(port->lineStatus() & LS_DSR))
    qDebug() << "warning: device is not turned on";
    qDebug() << "listening for data on" << port->portName();
    }
    else {qDebug() << "device failed to open:" << port->errorString(); }
    emit onReadyWrite("Listener Created!");
    }
    @

    1 Reply Last reply
    0
    • S Offline
      S Offline
      stukdev
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        Mannion
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • S Offline
          S Offline
          stukdev
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • A Offline
            A Offline
            andre
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved