Results 1 to 9 of 9

Thread: Cutting information

  1. #1
    Join Date
    Oct 2011
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Cutting information

    Hi all,

    I'm new in qt and need some help...

    1. I do success to read data from port USB, but it is not continuous reading automatically and i must click the read button many times to get the information from GPS in realtime mode. My question is: How can i get this information automatic with single click(read button)?
    i have read this topic http://www.qtcentre.org/threads/6824...t-data-on-GUI? and still confuse.

    2. If the information i got from GPS: D=1, B=10, Longitude=102, Latitude=3, LS-A=1 LS=1 LS-L-U=1 LS-EL-D=1
    And all i need is "Longitude and latitude" information only, how can i cut this information and display in my QTextEdit only the Long and Lat information?
    example: 102, 3

    Cloud you please help me to get some solution?

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Cutting information

    1. Since there is no USB functionality in Qt there is not much we can say to help with reading your USB port. If you can show how your code is currently structured then perhaps there are some pointers we could give. What have you tried to do to get "continuous" input?

    2. Assuming that data comes as a string then you want to read the friendly documentation for QString and QRegexp. You will definitely need the first, and could possibly do it with the second.

  3. #3
    Join Date
    Oct 2011
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Cutting information

    Quote Originally Posted by ChrisW67 View Post
    1. Since there is no USB functionality in Qt there is not much we can say to help with reading your USB port. If you can show how your code is currently structured then perhaps there are some pointers we could give. What have you tried to do to get "continuous" input?

    2. Assuming that data comes as a string then you want to read the friendly documentation for QString and QRegexp. You will definitely need the first, and could possibly do it with the second.
    Hi Chris,

    Many thanks for your answer.
    1. You are right there's no USB functionality in Qt, I'm sorry i mean Serial port, not USB Port since i used serial to USB converter.
    I used QextSerialPort for open and configure the serial and success get information from GPS. My GPS always send information continuously without any comment sent to GPS device. I want to receive all this information that given but right now i only get information when i clicked receive button in my GUI. My mission is display this continuous information in one click
    (sorry i couldn't give the code right now since i used my mobile phone to reply)

    2. It solved after i read many references. Only QString function in my mind.
    for example:
    QString a = "D=1, B=10, Longitude=102.00, Latitude=003.00, LS-A=1 LS=1 LS-L-U=1 LS-EL-D=1";
    QString b = a.section(',' , 2, 4); //the result: " Longitude=102.00, Latitude=003.00"
    QString c = b.mid(11, 6); //the result: "102.00"
    QString d = b.mid(28, 6); //the result: "003.00"

    Please correct me if i do wrong, and tell me if there's any better idea.

    Thanks..

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Cutting information

    1. Create a persistent QByteArray to act as a buffer, connect the readyRead() signal from the QExtSerialPort QIODevice to a slot that puts all available data in the buffer. Still in the slot code, after adding the new data to the buffer check if you have a whole line then process that line and remove it from the buffer, repeat until no complete lines remain. You exit the slot with nothing, or a partial line, in the buffer waiting for the next time data is ready to read. The code is identical to how you might handle a simple network connection (also a QIODevice) and you will find examples in the forums.

    You may have people suggest using a multi-threaded solution... my advice is don't use threads unless you must for other reasons.

    2. If it works, then it works! There are numerous ways to tackle it.

  5. #5
    Join Date
    Oct 2011
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Cutting information

    Quote Originally Posted by ChrisW67 View Post
    1. Create a persistent QByteArray to act as a buffer, connect the readyRead() signal from the QExtSerialPort QIODevice to a slot that puts all available data in the buffer. Still in the slot code, after adding the new data to the buffer check if you have a whole line then process that line and remove it from the buffer, repeat until no complete lines remain. You exit the slot with nothing, or a partial line, in the buffer waiting for the next time data is ready to read. The code is identical to how you might handle a simple network connection (also a QIODevice) and you will find examples in the forums.

    You may have people suggest using a multi-threaded solution... my advice is don't use threads unless you must for other reasons.

    2. If it works, then it works! There are numerous ways to tackle it.

    Hi Chris,
    Okay, Thanks before for your answer. I will figure it out first and come back with another problem for sure .
    i will update soon..

    regards,

  6. #6
    Join Date
    Oct 2011
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Cutting information

    Hi Chris and all,

    Still have problems:
    1. How to do repeat function? When i put "while" in my function my program in GUI became hung. If i may analyze it, port Open and function read repeatly but it never display in QTextEdit. When i tried to make the same function (using "while" or "do while") in console application, it normal (not in GUI mode)

    2. Another question : What if i 2 serial connected and i want to read all information from this both serials in the same time? Please advice.

    Thanks alot.

  7. #7
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Cutting information

    Quote Originally Posted by cairos View Post
    1. How to do repeat function? When i put "while" in my function my program in GUI became hung. If i may analyze it, port Open and function read repeatly but it never display in QTextEdit. When i tried to make the same function (using "while" or "do while") in console application, it normal (not in GUI mode)
    Repeat what? The program responds when more data is received, processes all the complete data it has, leaves what remains, and then waits for more data. You do not execute any sort of busy-wait loop, which is what it sounds like you are doing, because it locks the entire program. You could save us the effort of guessing by posting your actual code.
    2. Another question : What if i 2 serial connected and i want to read all information from this both serials in the same time? Please advice.
    You do the same thing twice (or three, four...).

  8. #8
    Join Date
    Oct 2011
    Posts
    5
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Cutting information

    Quote Originally Posted by ChrisW67 View Post
    Repeat what? The program responds when more data is received, processes all the complete data it has, leaves what remains, and then waits for more data. You do not execute any sort of busy-wait loop, which is what it sounds like you are doing, because it locks the entire program. You could save us the effort of guessing by posting your actual code.
    here is my simple program (newbie's program):
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include "qextserialport.h"
    4.  
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. port = new QextSerialPort("/dev/ttyUSB0");
    12. port->setBaudRate(BAUD9600);
    13. port->setFlowControl(FLOW_OFF);
    14. port->setParity(PAR_NONE);
    15. port->setDataBits(DATA_8);
    16. port->setStopBits(STOP_1);
    17. port->setRts(true);
    18. port->setDtr(true);
    19.  
    20. connect(ui->btnExit, SIGNAL(clicked()), SLOT(exit()));
    21. connect(ui->btnConnect, SIGNAL(clicked()), SLOT(openPort()));
    22. connect(ui->btnRead, SIGNAL(clicked()), SLOT(readSerial()));
    23. connect(ui->btnDisconnect, SIGNAL(clicked()), SLOT(closePort()));
    24. }
    25.  
    26. MainWindow::~MainWindow()
    27. {
    28. delete ui;
    29. }
    30.  
    31. void MainWindow::openPort()
    32. {
    33. port->open(QIODevice::ReadOnly | QIODevice::Unbuffered);
    34.  
    35. if (port->bytesAvailable())
    36. {
    37. ui->textEdit->setText("Connected");
    38. }
    39. else
    40. ui->textEdit->append("Please Check Your Serial");
    41. }
    42.  
    43. void MainWindow::readSerial()
    44. {
    45. char buff[64];
    46. qint64 i=port->readLine(buff,63);
    47. while(1)
    48. {
    49. if (port->bytesAvailable())
    50. {
    51. buff[i]='\0';
    52. QString msg = buff;
    53.  
    54. //Dispaly
    55. ui->textEdit->append(msg);
    56. }
    57. }
    58.  
    59. void MainWindow::closePort()
    60. {
    61. port->flush();
    62. port->close();
    63. }
    64.  
    65. void MainWindow::exit()
    66. {
    67. close();
    68. }
    69.  
    70. void MainWindow::changeEvent(QEvent *e)
    71. {
    72. QMainWindow::changeEvent(e);
    73. switch (e->type()) {
    74. case QEvent::LanguageChange:
    75. ui->retranslateUi(this);
    76. break;
    77. default:
    78. break;
    79. }
    80. }
    To copy to clipboard, switch view to plain text mode 

    You do the same thing twice (or three, four...).
    I'm sorry i don't really understand which step should i do to make serial works in the same time.
    You mean that i should open first port, second port?
    Is it possible if we open port in the same application?

    I really appreciate for your kindness to help me, Thank you..

  9. #9
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Cutting information

    Line 47 is a busy-wait loop. Since you never exit this loop your program can never respond to user interaction or update the display.

    You don't appear the have tried to follow any of the advice. Here are the steps again:
    • Create a persistent QByteArray to act as a buffer (member variable)
    • Connect the readyRead() signal from the QExtSerialPort QIODevice to a slot
    • In the slot code:
      • Put all available data on the end of the buffer
      • Check if you have a whole line. If so, process that line and remove it from the buffer.
      • Repeat until no complete lines remain.
      • Exit the slot with nothing, or a partial line, in the buffer waiting for the next time data is ready to read.

    It is important that you do not stay in the slot.

Similar Threads

  1. Cutting Elements QStringlist
    By codeman in forum Qt Programming
    Replies: 3
    Last Post: 5th June 2009, 11:19
  2. QT app information
    By CHeader in forum Qt Programming
    Replies: 5
    Last Post: 4th April 2008, 11:07
  3. How to read Raw Information from CD in MAC?
    By vishal.chauhan in forum General Programming
    Replies: 0
    Last Post: 10th July 2007, 12:26
  4. Getting MAC Information from qt?
    By vishal.chauhan in forum Qt Programming
    Replies: 4
    Last Post: 23rd May 2007, 09:31
  5. Border cutting with QGraphicsSvgItem
    By akiross in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2007, 09:12

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.