Results 1 to 13 of 13

Thread: Serial Port Reading problem

  1. #1
    Join Date
    Feb 2008
    Location
    Honolulu, HI
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Serial Port Reading problem

    Hi people,

    using Qt4 plus qextserialport 1.1, I am trying to do couple of things. In the Constructor of my "Motor" class, which is to be controlled using the serial port, I have these two lines pertaining to the serial port. The port settings are correct, and I am also opening the correct device.

    Qt Code:
    1. port = new QextSerialPort( portFile, portsettings );
    2. connect(port, SIGNAL(readyRead()),this,SLOT(onReceive()));
    To copy to clipboard, switch view to plain text mode 

    This is my onReceive():

    Qt Code:
    1. void Motor::onReceive()
    2. {
    3. QMutexLocker locker(&m);
    4. if(port->canReadLine()) {
    5.  
    6. // ("data" is a QByteArray)
    7. // this doesn't work: data = port->readLine(); although QIODevice has it?
    8. char tmp[RX_BUF_SIZE];
    9. qint64 len = port->readLine(tmp, RX_BUF_SIZE);
    10. data.fromRawData(tmp,len);
    11.  
    12. quint8 fault = 0;
    13.  
    14. // do something with the data before
    15. // clearing the buffer
    16.  
    17. data.clear();
    18.  
    19. rxcnt++;
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    What I want is that it reads the output of the serial controller board for the motor until it hits '\n' and then interprets the results. I noticed there are a variety of methods available to read data from the port, this seemed (intuitively) to be the most straight-forward, but I might be totally off the mark.

    Any help would be greatly appreciated.

    Thanks,
    Stephan

  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 Reading problem

    First change this historical version of qextserial to this version.

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

    sfabel (18th February 2010)

  4. #3
    Join Date
    Feb 2008
    Location
    Honolulu, HI
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Serial Port Reading problem


  5. #4
    Join Date
    Feb 2010
    Posts
    3
    Qt products
    Qt4

    Default Re: Serial Port Reading problem

    I agree. No Downloads. Do we have to get the source and build it?

  6. #5
    Join Date
    Feb 2010
    Posts
    3
    Qt products
    Qt4

    Default Re: Serial Port Reading problem

    Quote Originally Posted by snappy View Post
    I agree. No Downloads. Do we have to get the source and build it?
    So I got a clone of the project. Using TortoiseHG for Windows.

  7. #6
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Reading problem

    snappy, if you have the operating system Linux or Windows XP - you can use another library QSerialDevice http://fireforge.net/snapshots.php?group_id=199

  8. #7
    Join Date
    Feb 2008
    Location
    Honolulu, HI
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Serial Port Reading problem

    Version changed. I downloaded the latest devel version using hg.

    canReadLine() always returns false. The above code has not changed.

    What could be the trouble?

  9. #8
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Serial Port Reading problem

    Maybe this is your problem. From the canReadLine() docs:
    Note that unbuffered devices, which have no way of determining what can be read, always return false.
    I communicate with an unbuffered device and if I were using your onReceive() function it would look something like this:

    Qt Code:
    1. void Motor::onReceive()
    2. {
    3. QMutexLocker locker(&m);
    4. while(port->bytesAvailable()< RX_BUF_SIZE) {} //wait for data
    5.  
    6. char tmp[RX_BUF_SIZE];
    7. qint64 len = port->read(tmp, RX_BUF_SIZE); //use read instead of readline
    8. data.fromRawData(tmp,len);
    9.  
    10. quint8 fault = 0;
    11.  
    12. // do something with the data before
    13. // clearing the buffer
    14.  
    15. data.clear();
    16.  
    17. rxcnt++;
    18. }
    19. }
    To copy to clipboard, switch view to plain text mode 

  10. The following user says thank you to norobro for this useful post:

    sfabel (18th February 2010)

  11. #9
    Join Date
    Feb 2008
    Location
    Honolulu, HI
    Posts
    21
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Serial Port Reading problem

    My problem is that I need to read until I hit a '\n', then interpret the output as a whole. This is why I (conveniently) thought I could just use readLine(). But I guess I did not understand the "Unbuffered" part of the serial port...

    I suppose the problem is that the readyRead() signal is emitted every time something new arrives, correct? So I need to keep track of what has been read before, until I hit a '\n' and then interpret the whole thing until then.

    Stephan

  12. #10
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Serial Port Reading problem

    My device, a weather station, only sends data when it receives a request, so I don't use readyRead(). I write to the port requesting a certain record (the main records used are 100, 267 & 439 bytes), immediately call my "read" function and wait for data. The record length (equivalent to your RX_BUF_SIZE) is passed into the read function where it waits for that number of bytes to be available(while loop), reads the record and calls the appropriate function to process the record.

    My device sends data as a fixed length record with no end of line terminator so I can't use readLine(). You should try it since your device is sending "\n".

    If readLine() doesn't work and your lines are all the same length you should be able to use bytesAvailable() to wait for "line length" no. of bytes and then read "line length" no. of Bytes.

    I hope this makes some sense.

    By the way, I'm using qextserialport 1.1 with no problems.

  13. #11
    Join Date
    Jan 2009
    Location
    Russia
    Posts
    309
    Thanks
    2
    Thanked 43 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Serial Port Reading problem

    "mouse cried, stung, but continued to chew on a cactus!" (C)
    PS: folklore

  14. #12
    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 Reading problem

    Quote Originally Posted by sfabel View Post
    My problem is that I need to read until I hit a '\n', then interpret the output as a whole. This is why I (conveniently) thought I could just use readLine(). But I guess I did not understand the "Unbuffered" part of the serial port...
    .....
    Stephan
    You must read data byte by byte and complete one record in buffer until You read a '\n' character.

  15. #13
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: Serial Port Reading problem

    From the qextserialport docs:
    qint64 QIODevice::readLine ( char * data, qint64 maxSize )
    . . .
    . . .
    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.
    . . .
    . . .
    This function calls readLineData(), which is implemented using repeated calls to getChar(). . . .
    So iif bytesAvailable() is equal to record size it looks like readLine() should work.

    @kuzulis - I'm in Texas. We are used to being stung by the ubiquitous prickly pear cactus. I'm downloading as I type.

Similar Threads

  1. Problem in reading port using QextSerialPort
    By cutie.monkey in forum Qt Programming
    Replies: 6
    Last Post: 2nd July 2009, 02:07
  2. Replies: 1
    Last Post: 1st July 2009, 00:37
  3. Replies: 1
    Last Post: 1st July 2009, 00:36
  4. serial port communiction
    By jagadish in forum Qt Programming
    Replies: 4
    Last Post: 7th July 2007, 12:04
  5. Serial Port
    By b1 in forum Qt Programming
    Replies: 2
    Last Post: 18th January 2007, 02:05

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.