Results 1 to 9 of 9

Thread: QExtSerialPort does weired things!

  1. #1
    Join Date
    Jul 2013
    Posts
    36
    Thanks
    14
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5

    Question QExtSerialPort does weired things!

    I am reading a String from the Arduino, which comes like this:

    Qt Code:
    1. string_outData = 'a' + string_1 + '&' + string_2 + '&' + string_3 + 'E';
    2. char char_buf[40];
    3. string_outData.toCharArray(char_buf, 50);
    4. Serial.write(char_buf);
    To copy to clipboard, switch view to plain text mode 

    In QT, I want to receive the chars and build a string:
    Qt Code:
    1. QString incoming_string = QString::fromLatin1(port->readLine());
    2. ui->recv_text->moveCursor(QTextCursor::End);
    3. ui->recv_text->append(incoming_string);
    To copy to clipboard, switch view to plain text mode 
    This works fine so far - but this is a QTextEdit and not a string. In the QTextEdit I do not get 1 Line with the whole string, but this:
    XXX // 3 chars
    XXXX // 4 chars. ... why?
    XXXX
    XXXX
    XX

    instead of:
    XXXXXXXXXXXXXXXXXXXXXXXX

    What I want to get is the whole content of what is char_buf at the Arduino in incoming_string in the QT5 GUI. But I am failing miserably so far. I want to seperate the incoming_string later so I am able to work with the three strings. Please, I am very much in need of input How can I get a perfectly fine string ?
    Last edited by xtlc; 7th August 2013 at 13:07.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QExtSerialPort does weired things!

    From QIODevice::readLine doc :
    Data is read until either of the following conditions are met:

    The first '\n' character is read.
    maxSize - 1 bytes are read.
    The end of the device data is detected.

    and this is what You have. Remember that serial interface is very slow compared to the processor.
    Use QIODevice::read instead of readLine.

  3. The following user says thank you to Lesiok for this useful post:

    xtlc (7th August 2013)

  4. #3
    Join Date
    Jul 2013
    Posts
    36
    Thanks
    14
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5

    Default Re: QExtSerialPort does weired things!

    ok, sounds logical so far .... but i send 50 chars from the Arduino and I get the same result with read(50):

    X
    XXXX
    XXXX
    XXXX
    XXXX
    XXXX
    X

    XX
    XXXX
    XXXX
    XXXX
    XXXX
    XXXX

    XX
    XXXX
    XXXX
    XXXX
    XXXX
    XXXX
    X

    Shouldnt the GUI wait for 50 chars with read(50)?

  5. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QExtSerialPort does weired things!

    Not because readLine quits after reading all the data. Your 50 is not the number of characters that readLine must return but the maximum number that can be returned. The message of 50 characters at 9600 bps it takes about 47 ms. For a modern PC it is almost an eternity. After 10 ms from the beginning of sending You can read first 5-6 characters and readLine does it. After displaying this next readLine gets next 4-5 characters and so on. This is a basic problem in the data transmission : the difference in the speed of data processing by the transmitter and receiver
    If your data are in the form of a solid frame construction (start character a, end character E) read data by the read() method and build ready-to-display lines by analyzing the received data. Remember that one sample can contain the end of one frame and the beginning of the next.

  6. #5
    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: QExtSerialPort does weired things!

    Just another word of warning:
    Qt Code:
    1. string_outData = 'a' + string_1 + '&' + string_2 + '&' + string_3 + 'E';
    2. char char_buf[40];
    3. string_outData.toCharArray(char_buf, 50);
    To copy to clipboard, switch view to plain text mode 
    Putting up to 50 characters in a 40 byte buffer is a health hazard.


    Since the string you send does not end in '\n' any attempt to read using QIODevice::readLine() is unlikely to be fruitful.

  7. #6
    Join Date
    Jul 2013
    Posts
    36
    Thanks
    14
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5

    Default Re: QExtSerialPort does weired things!

    I found a working solution to this, I am only a little bit worried, somewhere in Australia a kitten may have died.

    Qt Code:
    1. void MainWindow::onReadyRead()
    2. {
    3. boolean eos = false;
    4.  
    5. if (port->bytesAvailable()) {
    6. QString recvData=(QString::fromLatin1(port->read(4)));
    7.  
    8. if(recvData.contains('A')) {
    9. appended_str = recvData.mid(recvData.indexOf('A')+1);
    10. }
    11. else if(recvData.contains('E'=) {
    12. int pos = recvData.length() - recvData.indexOf('E') - 1;
    13. appended_str += recvData.mid(pos);
    14. eos = true;
    15. }
    16. else {
    17. appended_str += recvData;
    18. }
    19.  
    20.  
    21. if(eos) {
    22. int appended_str_length = appended_str.length() - 1;
    23. appended_str.truncate(appended_str_length);
    24. QString incoming_string = appended_str;
    25. ui->recv_text->append(appended_str);
    26. eos = false;
    27. appended_str = "";
    28. }
    To copy to clipboard, switch view to plain text mode 
    is this a crime?

  8. #7
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QExtSerialPort does weired things!

    Better but not quite well. Firstly, what do you do when the receiver buffer is more than four characters? You're reading up to 4 (line 6). You should read all available data and then analyze them char by char. Remember what I said : read a portion of the data may contain several frames.

  9. The following user says thank you to Lesiok for this useful post:

    xtlc (8th August 2013)

  10. #8
    Join Date
    Jul 2013
    Posts
    36
    Thanks
    14
    Thanked 8 Times in 8 Posts
    Qt products
    Qt5

    Default Re: QExtSerialPort does weired things!

    What confuses me: Isn't there a function to do that? An implemented one? This must be a very common problem?!

  11. #9
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QExtSerialPort does weired things!

    Function for what ? Structure of the transmitted data is your private affair.
    In our system, we use two devices: fiscal printer and contactless card programmer. Both devices use a serial interface (physical layer) but they use completely different protocols (logical layer). Physical layer is what QExtSerialPort do. You must create logical layer.

Similar Threads

  1. Things to inlcude in an LGPL distribution
    By The physicist in forum Installation and Deployment
    Replies: 23
    Last Post: 10th January 2011, 15:25
  2. hellogl_es example with weired behavior
    By fleez in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 27th July 2010, 14:10
  3. Replies: 4
    Last Post: 13th June 2010, 12:31
  4. Replies: 4
    Last Post: 10th March 2007, 04:28
  5. Replies: 2
    Last Post: 14th February 2006, 15:28

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.