Results 1 to 12 of 12

Thread: Serial port split data

  1. #1
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Serial port split data

    Hello,
    I'm using MCU from TI MSP430FR with FTDI FT230.
    Code in QT project to receive data from COM port:
    Qt Code:
    1. connect(m_serialPort, &QSerialPort::readyRead, this, &TokozSerial::HandleReadyRead);
    2.  
    3.  
    4. void TokozSerial::HandleReadyRead()
    5. {
    6. if(m_serialPort->bytesAvailable() > 0)
    7. {
    8. while (!m_serialPort->atEnd())
    9. {
    10. m_readData.append(m_serialPort->readAll());
    11. }
    12. HandleTimeout();
    13. }
    14. }
    15.  
    16. void TokozSerial::HandleTimeout()
    17. {
    18. m_serialPort->clear();
    19. qDebug() << "RawData" << m_readData;
    20. connect(this, &TokozSerial::HandleReady, gp_protocolUart, &ProtocolUart::HandleReady);
    21. emit HandleReady(m_readData);
    22. disconnect(this, &TokozSerial::HandleReady, gp_protocolUart, &ProtocolUart::HandleReady);
    23. disconnect(this, &TokozSerial::HandleReady, 0, 0);
    24. m_readData.clear();
    25. }
    To copy to clipboard, switch view to plain text mode 

    In HandleReady I parse this data so I think it's not necessary for this problem.
    My problem is that eg. every 5th or 10th (sometimes) I get two packets instead of one.
    Example:
    MCU send: "\xA1\x00\x00\x01\x81\xDC\xD6"
    I get: "\xA1\x00\x00\x01" and then "\x81\xDC\xD6 -- HandleReadyRead called twotimes. I know that together it is correct, but I think this is not working correctly.
    It's not always the same. Sometimes I get first 2 bytes, then 5, or 1-6, 6-1 ...


    Thanks for any advice.
    Last edited by ado130; 16th May 2017 at 13:51.

  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: Serial port split data

    The serial port is just a data transfer pipe and so it works. Search the forum - the topic has been explained many times.

  3. #3
    Join Date
    May 2011
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Serial port split data

    The SerialPort has no way of knowing what the start and/or end of your data packet is. It only notifies you that there is data available, which you read with the readAll() command.

    You should set up a buffer to collect data into until YOU find that you have a complete data packet with your header and footer, then process it...

    It's up to you to decide that you have a full data packet. The SerialPort only knows that there is data available to be read, it does not know WHAT the data is, or what it represents.

  4. #4
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Serial port split data

    Yes, I understand this process. But I do not have any "forbidden char" and the length of received data is not always same. MCU send all data at once, so I hope I can read it all. I'm not sure why data are sometimes fine and sometimes splitted.

  5. #5
    Join Date
    May 2011
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Serial port split data

    As Lesiok said... search and read.

    The SerialPort is in no way guaranteed to send all data at once... no matter what the size of the data. It is up to the programmer to make sure YOU know how much data is being sent and/or what marks the beginning and end of the data.

    If you truly have no way to know how much data is being transmitted, or what the beginning and end of the data is... then how can you possibly know WHAT the data is?

  6. #6
    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: Serial port split data

    Ask uncle Google about : ISO OSI RM
    Hint: serial port is the lowest layer.

  7. #7
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Serial port split data

    Thanks. It's not entirely true, that I do not know how much data I'm expecting. I have 3-4 patterns of data packets. So I'll check the data length.
    My ignorance about serial port. Thanks for explain.

  8. #8
    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: Serial port split data

    And these packages do not have any fixed structure? If so, the protocol is incorrectly designed.

  9. #9
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Serial port split data

    Of course not, these packages have fixed structure. I have just 3 or 4 different patterns.

  10. #10
    Join Date
    May 2017
    Posts
    6
    Thanks
    2
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Serial port split data

    I had the same Problem few days ago,
    try to add special characters like crlf and append your data on a QString (or anything you want) until you recieve crlf.
    QSerialPort doesn't know your frame so it only recieve data when it is available.

  11. #11
    Join Date
    Jan 2017
    Posts
    58
    Thanks
    2
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Serial port split data

    Yes, it is possible way to solve this problem. But I can't be sure that this special character will not be part of correct data - packet and I think, in 21th century is possible to transfer all data without any special characters at the end. I'm just saying, when I sent 20B I also get 20B. So, I changed my program to check this length and it seems working correctly.

  12. #12
    Join Date
    May 2011
    Posts
    81
    Thanks
    6
    Thanked 5 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11

    Default Re: Serial port split data

    Quote Originally Posted by ado130 View Post
    Yes, it is possible way to solve this problem. But I can't be sure that this special character will not be part of correct data - packet and I think, in 21th century is possible to transfer all data without any special characters at the end. I'm just saying, when I sent 20B I also get 20B. So, I changed my program to check this length and it seems working correctly.
    In any century, it is not possible to just 'transfer all data' via a serial port without some way to verify the data on the receiving end. The way you are doing WILL fail at some point. If your do not have a starting element or an ending element (preferably both), you can never be absolutely 100% sure you have the correct data.

    My advice is if you truly know the structure of the 3 or 4 patterns like you mentioned above, then you should implement code to check the received data fits into one of those patterns.

    If something should happen during transmission, and a byte is dropped... or part of the packet is corrupted, then your data will be wrong...and assuming that just because you received 20 bytes that the data is good... is a very, very bad idea.

    Just my 2 cents...

Similar Threads

  1. read data from serial port
    By amitpatel22 in forum Qt for Embedded and Mobile
    Replies: 2
    Last Post: 20th July 2011, 17:11
  2. DataPlot with data from Serial Port
    By przemek in forum Qwt
    Replies: 4
    Last Post: 2nd April 2011, 17:11
  3. recieve continious data from serial port
    By ready in forum Qt Programming
    Replies: 11
    Last Post: 2nd April 2011, 09:06
  4. can't set serial port data bits
    By yyiu002 in forum Qt Programming
    Replies: 6
    Last Post: 23rd June 2010, 22:28
  5. data from serial port
    By bhe in forum Newbie
    Replies: 4
    Last Post: 3rd May 2009, 10:19

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.