Results 1 to 2 of 2

Thread: QextSerialPort port->write Problem

  1. #1
    Join Date
    Sep 2010
    Posts
    25
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Windows

    Default 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.


    Qt Code:
    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. ba.resize(3);
    16. ba[0] = 254 ;
    17. ba[1] = 1;
    18. ba[2] = 255;
    19. int i = listener->port->write(ba,ba.length());
    20. }
    21.  
    22. void MainWindow::clickTEST0() {
    23. ba.resize(3);
    24. ba[0] = 254 ;
    25. ba[1] = 1;
    26. ba[2] = 255;
    27. int i = listener->port->write(ba,ba.length());
    28. }
    29.  
    30. Session::Session(){}
    31.  
    32. QString Session::enumerate(){
    33. QList<QextPortInfo> ports = QextSerialEnumerator::getPorts();
    34. int tempport = 9999;
    35. QString tempportname = "NULL";
    36. QString tempfriendname = "NULL";
    37. QString qstr;
    38. int portsize = 0;
    39. for (int i = 0; i < ports.size(); i++) {
    40. portsize++;
    41. qstr = ports.at(i).enumName;
    42. if (qstr == SerialDeviceString) {
    43. tempport = i;
    44. tempportname = ports.at(i).portName;
    45. tempfriendname = ports.at(i).friendName;
    46. } else {}
    47. }
    48. QString portName = tempportname; // update this to use your port of choice
    49. if (portName == "NULL"){ qDebug() << "SerialDevice NOT FOUND portName = " << portName;
    50. }else{ qDebug() << " SerialDevice FOUND portName = " << portName; }
    51. return portName;
    52. }
    53.  
    54. PortListener::PortListener(const QString & portName) { //, int iVal){ //portName = this->enumerate();
    55. if (portName=="NULL"){ //NOT CONNECTED
    56. }else{ SetPort(portName); }
    57. }
    58.  
    59. void PortListener::SetPort(const QString & portName){
    60. this->port = new QextSerialPort(portName, QextSerialPort::EventDriven);
    61. port->setBaudRate(BAUD115200);
    62. port->setFlowControl(FLOW_OFF);
    63. port->setParity(PAR_NONE);
    64. port->setDataBits(DATA_8);
    65. port->setStopBits(STOP_1);
    66. if (port->open(QIODevice::ReadWrite) == true) {
    67. connect(port, SIGNAL(readyRead()), this, SLOT(onReadyRead()));
    68. connect(port, SIGNAL(dsrChanged(bool)), this, SLOT(onDsrChanged(bool)));
    69. if (!(port->lineStatus() & LS_DSR))
    70. qDebug() << "warning: device is not turned on";
    71. qDebug() << "listening for data on" << port->portName();
    72. }
    73. else {qDebug() << "device failed to open:" << port->errorString(); }
    74. emit onReadyWrite("Listener Created!");
    75. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Mannion; 3rd May 2011 at 14:42. Reason: Added detail

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QextSerialPort port->write Problem

    this should not compile, unless you have defined
    Qt Code:
    1. PortListener *listener;
    To copy to clipboard, switch view to plain text mode 
    in your header as well.
    You are allocating a local 'listener' in your constructor, and the one in the method is not initialized.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. qext-serial-port write problem.
    By rex in forum Qt Programming
    Replies: 11
    Last Post: 9th December 2013, 07:18
  2. Replies: 3
    Last Post: 27th April 2011, 09:27
  3. Replies: 5
    Last Post: 27th November 2009, 14:37
  4. Problem in reading port using QextSerialPort
    By cutie.monkey in forum Qt Programming
    Replies: 6
    Last Post: 2nd July 2009, 02:07
  5. Replies: 1
    Last Post: 1st July 2009, 00:36

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.