Results 1 to 12 of 12

Thread: recieve continious data from serial port

  1. #1
    Join Date
    Mar 2011
    Posts
    36
    Thanks
    30
    Qt products
    Qt4
    Platforms
    Windows

    Default recieve continious data from serial port

    I have a problem while reading data from microcontroller...my microcontroller sends char 'a' continiously(just to test)..and I want to receive the character in my ui application..(lineedit)...
    Qt Code:
    1. QextSerialPort * port = new QextSerialPort("COM4", QextSerialPort::EventDriven);
    2. port->setBaudRate(BAUD9600);
    3. port->setFlowControl(FLOW_OFF);
    4. port->setParity(PAR_NONE);
    5. port->setDataBits(DATA_8);
    6. port->setStopBits(STOP_1);
    7. port->open(QIODevice::ReadWrite);
    8. port->setTimeout(500);
    9. port->setRts(true);
    10. port->setDtr(true);
    11. if(port->isReadable())
    12. {
    13. char buff[1024];
    14. qint64 i=port->readLine(buff,1024);
    15.  
    16. buff[i]='\0';
    17. QString str(buff);
    18. if(i!=-1)
    19. {
    20. ui->lineEdit->setText(str);;
    21. port->close();
    22. }
    23. }
    24.  
    25. else
    26. qDebug("port not open");
    To copy to clipboard, switch view to plain text mode 
    this does not help me ............please help me/...

  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: recieve continious data from serial port

    this does not help me
    ^- this doesn't help me to help you.
    ==========================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.

  3. #3
    Join Date
    Feb 2008
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Re: recieve continious data from serial port

    Even if you get the read working, ask yourself what happens to your buffer if readLine returns -1. Specifically, what is buff[i] going to do? It can't be good.

  4. The following user says thank you to jdiewald for this useful post:

    ready (31st March 2011)

  5. #4
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: recieve continious data from serial port

    Have you verified that your micro is even sending the data correctly using something like a terminal emulator?

    After that, you have a possible buffer underrun, and you don't need the null character anyway, as the method you are calling adds it for you.

    Last, you are running the serial port in event driven mode and then attempting to poll it. Doomed to failure.

  6. The following user says thank you to squidge for this useful post:

    ready (31st March 2011)

  7. #5
    Join Date
    Mar 2011
    Posts
    36
    Thanks
    30
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: recieve continious data from serial port

    Yes i have checked data from microcontroller .............but could not recieve in my ui application ....
    please help me..

  8. #6
    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: recieve continious data from serial port

    How do you know that "COM4" is the correct port?
    Use the port enumeration static functions in QextSerialPort to extract the available ports, and be sure you are reading the port you micro is connected to.
    ==========================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.

  9. #7
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: recieve continious data from serial port

    Quote Originally Posted by ready View Post
    Yes i have checked data from microcontroller .............but could not recieve in my ui application ....
    please help me..
    Use signals and slots, then the event loop will run.

  10. #8
    Join Date
    Mar 2011
    Posts
    36
    Thanks
    30
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: recieve continious data from serial port

    Use signals and slots, then the event loop will run.
    can you please elaborate it...

  11. #9
    Join Date
    Sep 2009
    Location
    UK
    Posts
    2,447
    Thanks
    6
    Thanked 348 Times in 333 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: recieve continious data from serial port

    Sure, http://doc.qt.nokia.com/4.7/signalsandslots.html

    BTW, it seems like you are attempting to copy and paste chunks of code. This will not work, you have to understand the code first.

  12. #10
    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: recieve continious data from serial port

    In addition to what was already said you are trying to read a line of data and a line is characterised by the fact that it ends with a newline character. Since your peer sends you only 'a' characters, you don't have a newline character and hence you don't have a line. readLine() probably will work and simply return all pending data but it's good to avoid bad habits.
    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.


  13. #11
    Join Date
    Mar 2011
    Posts
    36
    Thanks
    30
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: recieve continious data from serial port

    finally my lineedit showed some character after I pressed the button .....but it read different character......for example If I send "b" from microcontroller......it reads as 'd',again after pressing the button it reads 'h' and again after pressing button it reads't'.....why this is happening so??

  14. #12
    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: recieve continious data from serial port

    Do you send and receive at the same speed and all the other port settings?
    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.


Similar Threads

  1. DataPlot with data from Serial Port
    By przemek in forum Qwt
    Replies: 4
    Last Post: 2nd April 2011, 17:11
  2. can't set serial port data bits
    By yyiu002 in forum Qt Programming
    Replies: 6
    Last Post: 23rd June 2010, 22:28
  3. data from serial port
    By bhe in forum Newbie
    Replies: 4
    Last Post: 3rd May 2009, 10:19
  4. Replies: 9
    Last Post: 4th June 2008, 12:29
  5. First attempt to display serial port data on GUI
    By ShaChris23 in forum Newbie
    Replies: 12
    Last Post: 4th May 2007, 09:14

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.