Results 1 to 7 of 7

Thread: Problem in reading port using QextSerialPort

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jul 2008
    Location
    Philippines
    Posts
    60
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Problem in reading port using QextSerialPort

    hi all, i'm currently doing an application that will send and receive SMS messages using GSM Modem and by sending AT Commands. I used the QextSerialPort class. I connect to the port using this code:
    Qt Code:
    1. port = new QextSerialPort("COM1");
    2. port->setBaudRate(BAUD19200);
    3. port->setFlowControl(FLOW_OFF);
    4. port->setParity(PAR_NONE);
    5. port->setDataBits(DATA_8);
    6. port->setStopBits(STOP_2);
    7. port->open(QIODevice::ReadWrite);
    To copy to clipboard, switch view to plain text mode 

    then send AT COMMANDS:

    Qt Code:
    1. QString command ="AT\r\n"; //sample command - in Hyperterminal this returns OK
    2. QByteArray byte;
    3. byte.clear();
    4. byte.append(command);
    5. port->write(byte);
    6. port->waitForBytesWritten (500);
    To copy to clipboard, switch view to plain text mode 

    then try to read the response:
    Qt Code:
    1. char buff[1024];
    2. int numBytes;
    3. numBytes = port->bytesAvailable(); //always returns -1
    4. if(numBytes > 0)
    5. {
    6. if(numBytes > 1024)
    7. numBytes = 1024;
    8. int i = port->read(buff, numBytes);
    9. buff[i] = '\0';
    10. QString msg = buff;
    11. qDebug(msg);
    12. qDebug("bytes available: %d", numBytes);
    13. qDebug("received: %d", i);
    14. }
    To copy to clipboard, switch view to plain text mode 

    i'm not sure if im doing it right cause when i read the response the bytesAvailable() always returns -1. Is there anybody here can help me or give me a piece of code on how to do it right? thnks in advance...

  2. #2
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Thanks
    8
    Thanked 133 Times in 128 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Problem in reading port using QextSerialPort

    did you checked what port->open(QIODevice::ReadWrite); is returning?

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem in reading port using QextSerialPort

    Hi,

    Are you executing the write and read sequentially?
    You have to wait some time until there is data on the port. You can use a timer that will call a slot where you have to read the data or use a QThread to perfrom read and write with a "msleep" that will wait some time.
    The sleep time needed can be calculated with the baud rate.
    Òscar Llarch i Galán

  4. #4
    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: Problem in reading port using QextSerialPort

    2 cutie.monkey, use QSerialDevice :-)

    this library work in asynchronous mode and support method: waitForReadyRead(msecs)

    this library is alternative QextSerialPort

    in this library is /examples/reader and /examples/writer ... for testing use it.

    PS:
    in the library may contain errors!
    you can fix them myself! :-)
    Attached Files Attached Files

  5. The following user says thank you to kuzulis for this useful post:

    cutie.monkey (2nd July 2009)

  6. #5
    Join Date
    Jul 2008
    Location
    Philippines
    Posts
    60
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in reading port using QextSerialPort

    did you checked what port->open(QIODevice::ReadWrite); is returning?
    yes, this returns true.. but what i encountered here is this, i removed the modem so nothing is connected in COM1 then try to run my app, it also returns true.

    Are you executing the write and read sequentially?
    No, i have a button to write command and a button to read its response. I have tried to wait for some seconds before trying get its response but nothing happens. Still the port->bytesAvailable() returns -1

    2 cutie.monkey, use QSerialDevice :-)
    ok, i'll try to use QSerialDevice . hope this one will help..

    thanks

  7. #6
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Problem in reading port using QextSerialPort

    Hi,

    Are you sure that the modem is writing somthing? Try using a Serial sniffer to see what are you writing and what is the modem writing. Search "free serial port monitor" on your favorite web search engine.
    Òscar Llarch i Galán

  8. #7
    Join Date
    Jul 2008
    Location
    Philippines
    Posts
    60
    Thanks
    9
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Problem in reading port using QextSerialPort

    hi, instead of QextSerialPort , im now using QSerialDevice which kuzulis suggessted(tnks to kuzulis), and its working.. cheers!

Similar Threads

  1. QWT 5, QT3, SuSE 10.2. Crash and burn
    By DrMcCleod in forum Qwt
    Replies: 8
    Last Post: 7th September 2007, 20:53
  2. Replies: 6
    Last Post: 18th April 2007, 15:04
  3. problem with reading input data in qt
    By Ahmad in forum Qt Programming
    Replies: 3
    Last Post: 9th April 2007, 10:58
  4. Replies: 16
    Last Post: 7th March 2006, 15:57
  5. Problem with reading a file
    By Buhmann in forum Qt Programming
    Replies: 11
    Last Post: 17th February 2006, 13:02

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
  •  
Qt is a trademark of The Qt Company.