Results 1 to 6 of 6

Thread: Frustrating problem with QextSerialPort write()

  1. #1
    Join Date
    Oct 2009
    Posts
    33
    Thanks
    2

    Default Frustrating problem with QextSerialPort write()

    Hey all,

    I'm trying to write to a serial device using QextSerialPort. Here are some things I should note:

    *I can read from the device, but cannot write to it.
    *I can read and write from the device using a serial communication tool (cutecom)
    *Using a loopback serial cable, I can see that write is working

    Based on this info, I know it seems like the problem is device related, and for what its worth, it seems to have a really flaky serial I/F. However, I've been able to use it with normal linux serial drivers, and I can communicate with the device with cutecom (a linux gui app that r/w to serial ports -- handy for testing) no problem. This leads me to believe I'm doing something wrong with QextSerialPort that I don't understand. I made a simple program similar to the "events" example included with the source.

    I'd really appreciate any advice!

    -KF


    Qt Code:
    1. // System Includes
    2. #include "main.h"
    3.  
    4. // RS232 Helper Thread
    5. RS232Thread::RS232Thread(QextSerialPort* port, QObject* parent)
    6. :QThread(parent)
    7. { serialPort = port; }
    8.  
    9. RS232Thread::~RS232Thread()
    10. {}
    11.  
    12. void RS232Thread::run()
    13. {
    14. // constantly get user input
    15. std::string inpSt;
    16.  
    17. std::cerr << "Send MVP a command string: " << std::endl;
    18.  
    19. while (inpSt.compare("quit") != 0)
    20. {
    21. std::getline(std::cin, inpSt);
    22. std::cerr << serialPort->write(inpSt.c_str());
    23. std::cerr << "Sent command: " << inpSt << std::endl;
    24. }
    25.  
    26. serialPort->close();
    27. }
    28.  
    29.  
    30. // RS232 Signal Handler
    31. RS232SignalHandler::RS232SignalHandler(QextSerialPort* port, RS232Thread* thread, QObject* parent):
    32. QObject(parent)
    33. { serialPort = port;
    34. sTime = thread->startTime; }
    35.  
    36. RS232SignalHandler::~RS232SignalHandler()
    37. {}
    38.  
    39. void RS232SignalHandler::RS232ReceivedData()
    40. {
    41.  
    42. char data[512];
    43. int numBytesRead = serialPort->read(data, 512);
    44.  
    45. std::cerr << "DATA READ IN: " << std::endl;
    46.  
    47. // print out what we read in as chars
    48. for (int i=0; i < numBytesRead; i++)
    49. { std::cerr << data[i]; }
    50. std::cerr << std::endl;
    51.  
    52. // print out what we read in as ints
    53. for (int i=0; i < numBytesRead; i++)
    54. { std::cerr << int(data[i]); }
    55. std::cerr << std::endl;
    56. }
    57.  
    58.  
    59. // Start Main
    60. int main(int argc, char *argv[])
    61. {
    62. QCoreApplication a(argc, argv);
    63.  
    64. // set up serial port
    65. QextSerialPort *port = new QextSerialPort("/dev/ttyUSB0", QextSerialPort::EventDriven);
    66. port->setBaudRate(BAUD9600);
    67. port->setFlowControl(FLOW_OFF);
    68. port->setParity(PAR_NONE);
    69. port->setDataBits(DATA_8);
    70. port->setStopBits(STOP_1);
    71.  
    72. // setup helper thread
    73. RS232Thread* hThread = new RS232Thread(port);
    74. a.connect(hThread, SIGNAL(finished()), &a, SLOT(quit()));
    75. hThread->start();
    76.  
    77. // setup data received signal handler
    78. RS232SignalHandler* handler = new RS232SignalHandler(port, hThread);
    79. handler->connect(port, SIGNAL(readyRead()), handler, SLOT(RS232ReceivedData()));
    80.  
    81. // event loop
    82. return a.exec();
    83. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Frustrating problem with QextSerialPort write()

    You will QSerialDevice:
    http://qt-apps.org/content/show.php/...content=112039

    Look there examples:
    /examples/writer
    /examples/reader
    /examples/sreader

    For SVN to visit the site: fireforge.net
    But sometimes this site does not work. ((
    So write to me at mail and I'll send you the latest from SVN.
    Address mail to the source code QSerialDevice.

    PS: better to take from SVN, because QSerialDevice 0.1.0 version has more bugs.

    Best regards,
    Denis
    Last edited by kuzulis; 27th November 2009 at 05:43.

  3. #3
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Frustrating problem with QextSerialPort write()

    kuzulis: I hate when people use forums this way. Did kachofool ask for any alternatives or for helping with a specific problem? If you want to advertise your library maybe start new thread in a proper subforum with info about it. If you really have to do it here, maybe first try to answer for the question, or point that the code has bug or is incomplete and then propose an alternative.
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Frustrating problem with QextSerialPort write()

    Unless something changed recently, QExtSerialPort doesn't emit readyRead(). You need to periodically check if there is anything to read with a timer. I don't know if that's related to your problem, in theory problems with reading should not stop you from writing (unless some buffer gets full which might happen here).
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Oct 2007
    Location
    Grenoble, France
    Posts
    80
    Thanked 9 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Frustrating problem with QextSerialPort write()

    Qt Code:
    1. while (inpSt.compare("quit") != 0)
    2. {
    3. std::getline(std::cin, inpSt);
    4. std::cerr << serialPort->write(inpSt.c_str());
    5. std::cerr << "Sent command: " << inpSt << std::endl;
    6. }
    To copy to clipboard, switch view to plain text mode 
    What do you get from this on stderr?
    And am I missing something or you're not opening the port in your code?
    The rest as wysota wrote
    You have to run a level 3 diagnostic.

    Ashes to ashes, Qt to Qt ( wysota )

  6. #6
    Join Date
    Oct 2009
    Posts
    33
    Thanks
    2

    Default Re: Frustrating problem with QextSerialPort write()

    Thanks to all for the replies.

    @wysota
    Emit seems to be working. I downloaded the latest source from the project website and read under their 'issues' section that event based I/O had been implemented (in POSIX at least, and I'm on linux).

    @calhan
    For some odd reason (maybe copy/paste) the line of code for opening the port isn't in my original post. The line is actually

    Qt Code:
    1. // open serial port
    2. port->open(QIODevice::ReadWrite);
    To copy to clipboard, switch view to plain text mode 

    And it's right after I set up the port parameters, before I launch the helper threads.

    The output of stderr is the expected number of characters I write. An example of a command string I'm outputting to the device is "1 RN".
    The return value of the write command is 4.

    EDIT:
    The issue was with the command string I was sending to the device. The string needed to be terminated with *both* newline and carriage return characters... everything seems okay now. Thanks!


    EDIT (EDIT):
    @kuzulis: I've tried your library and it works as well (since the error was on my part). Thank you for providing the community with a Qt serial i/o library, however I want to agree with what calhan said in that you've advertised your library often (already in another thread of mine!) without really replying to the question.
    Last edited by kachofool; 27th November 2009 at 18:10.

Similar Threads

  1. qextserialport BAUDRATE problem
    By dheeraj in forum Qt Programming
    Replies: 6
    Last Post: 19th March 2009, 19:29
  2. Replies: 7
    Last Post: 29th August 2008, 10:24
  3. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  4. Problem in QExtserialport program.
    By dheeraj in forum Qt Programming
    Replies: 5
    Last Post: 31st May 2008, 10:24
  5. Problem on set QGraphicsTextItem write protect.
    By patrik08 in forum Qt Programming
    Replies: 1
    Last Post: 22nd July 2007, 20:53

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.