Results 1 to 4 of 4

Thread: COM port reading with Qextserialport is creating problem:

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Mar 2011
    Posts
    15
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default COM port reading with Qextserialport is creating problem:

    I am new to QT programming. Serial communication application is being developed. For this Qextserialport classes are successfully conformed with QT classes and is being utilised for developing application. writing to port and Reading from port are incorporated in same application. Am able to write all the bytes in a msgpckt to port but reading from that port is creating the problem. It can only read one byte when read function of qextserialport is called. First byte is read properly rest its showing junk values.
    How to debug it??? Is the method am following is correct??? here is the piece of code



    Writing to port (COM1) : here is the code
    ///////////////////////////////////

    Qt Code:
    1. openflag = port ->isOpen();
    2. if(openflag)
    3. {
    4. int i = port->write((const char *) &Hmi_statusMsg, sizeof(Hmi_statusMsg));
    5.  
    6. qDebug("Transmitted bytes =%d",i);
    7. }
    8.  
    9. else
    10. {
    11. qDebug("COM1 port is not open");
    12. }
    To copy to clipboard, switch view to plain text mode 

    //////////////////////////////////////End code ///
    Hmi_statusMsg in above code is Msg packet, each feilds are of type unsigned char, having data : MsgHdr = 0x75
    Data1 = 0x08
    Data2 = 0x02
    Data3 = 0x40
    Data4 = 0x10
    chksum = 0xcd


    Reading from port and displaying it on text-edit box: here is the code

    Qt Code:
    1. char buff [1024];
    2. int numBytes;
    3.  
    4. numBytes = port->bytesAvailable();
    5. if(numBytes > 0)
    6. {
    7.  
    8. int i = port->read(buff, numBytes);
    9.  
    10. buff[i] = '\0';
    11. QString msg = (QString) buff;
    12.  
    13. received_msg->append(msg);
    14. received_msg->ensureCursorVisible();
    15. qDebug("bytes available: %d", numBytes);
    16. qDebug("received: %d", i);
    17.  
    18. ui->textEdit->setText(msg);
    19.  
    20. }
    21.  
    22. else
    23. {
    24. qDebug("bytes not available: %d", numBytes);
    25. }
    To copy to clipboard, switch view to plain text mode 

    /////////////////////////////////////////////////////




    Transmitter output::::

    Status Msg header = 75
    Status Msg Data1 = 8
    Status Msg Data2 = 2
    Status Msg Data3 = 40
    Status Msg Data4 = 10
    Status Msg chksum = cd
    Transmitted bytes =6



    Receiver Output::::


    bytes available: 6
    received: 6
    RCVD Status Msg header = u
    RCVD Status Msg Data1 = 
    RCVD Status Msg Data2 = 
    RCVD Status Msg Data3 = @
    RCVD Status Msg Data4 = 
    RCVD Status Msg chksum = Í



    First byte is displayed in ASCII, but remaining all are junk values...

    Expecting guidance,
    Thanks in advance
    Last edited by high_flyer; 27th April 2011 at 11:21. Reason: code tags

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

    Default Re: COM port reading with Qextserialport is creating problem:

    What you are reading is what you sent, so what is the problem?

  3. #3
    Join Date
    Mar 2011
    Posts
    15
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: COM port reading with Qextserialport is creating problem:

    Thanks for reply.

    I know what am reading, but the method followed to read, was that correct??? because it cud able to read only one byte, rest all showing junk....

    piece of code which i have posted can be referred and correct me where I went wrong......

  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: COM port reading with Qextserialport is creating problem:

    Your code looks how many bytes are available (could be 1, could be 5000), reads that number of bytes, assumes that is some sort of valid string, and appends it to the text edit. Since the data you are sending is not printable characters this will produce rubbish in the text edit. The odd characters you show seem consistent with treating the data you say was sent as ASCII characters, which was squidge's point.

    If you want to display binary data in a readable form you will have to convert it somehow. Hexadecimal is often chosen for this purpose.

    If you need all 6 bytes to be present before you do something with the packet then you need to allow for all 6 bytes to arrive one at at time, buffer them, and do something when you have a full set. You would need to do this to check your checksum.

Similar Threads

  1. Serial Port Reading problem
    By sfabel in forum Qt Programming
    Replies: 12
    Last Post: 18th February 2010, 14:59
  2. Problem in reading port using QextSerialPort
    By cutie.monkey in forum Qt Programming
    Replies: 6
    Last Post: 2nd July 2009, 02:07
  3. Replies: 1
    Last Post: 1st July 2009, 00:36
  4. QextSerialPort with QTimer approch for reading
    By PaceyIV in forum Qt Programming
    Replies: 1
    Last Post: 18th May 2009, 14:33
  5. Replies: 7
    Last Post: 29th August 2008, 10:24

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.