Results 1 to 9 of 9

Thread: QSerialDevice Problem

  1. #1
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Question QSerialDevice Problem

    This is probably due to my total inexperience with libraries.
    Trying to include QSerialDevice as a static library.
    Generated the library file libqserialdevice.a and put it in my project directory.
    Added to my .pro

    Qt Code:
    1. INCLUDEPATH += C:\cpp\qt_projects\qtlogger\include
    2. LIBS += -L"C:\cpp\qt_projects\qtlogger\" -llibqserialdevice.a
    To copy to clipboard, switch view to plain text mode 

    Builds ok, but doesn't look like the .exe file got any bigger. That worried me.
    Then create a class to use the library functions in - SerialComm

    Adding to SerialComm:

    class AbstractSerial; // from the library

    Causes build error: forward declaration of 'struct AbstractSerial'

    Obviously not the right way. So what is?

  2. #2
    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: QSerialDevice Problem

    1. What function SerialComm?
    2. What operating system?
    3. And what do you want to do? (more detail the essence of the problem)

    PS: try to use the library from SVN:
    http://fireforge.net/snapshots.php?group_id=199

    PPS: to LIBS in my opinion you need to insert the library name without extension, ie instead -llibqserialdevice.a need to write -lqserialdevice
    Last edited by kuzulis; 16th January 2010 at 09:28.

  3. #3
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSerialDevice Problem

    Thanks for your reply Kuzulis. I have changed the pro file as you suggested and built the library from the svn. Same build problem.
    The SerialComm class was added to my project as a place to put the serial read and write methods - a place to call the library functions from.
    I just need to be able to read and write to a serial port on Win, *nix, or MAC OS.
    I have included the initial code below - just copied your writer as a starting point.
    OS is XP/SP3

    Qt Code:
    1. INCLUDEPATH += C:\cpp\qt_projects\qtlogger\qserialdevice\src
    2. LIBS += -L"C:\cpp\qt_projects\qtlogger\qserialdevice\src\release"
    3. LIBS += -llibqserialdevice
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #ifndef SERIALCOMM_H
    2. #define SERIALCOMM_H
    3.  
    4. #include <QObject>
    5. #include <QByteArray>
    6.  
    7. #include "abstractserial.h"
    8. #include "abstractserialengine.h"
    9. #include "datatypes.h"
    10. #include "nativeserialengine.h"
    11. #include "winserialnotifier.h"
    12.  
    13. class SerialComm : public QObject
    14. {
    15. Q_OBJECT
    16. public:
    17. explicit SerialComm(QObject *parent = 0);
    18. void printDataToHex(const QByteArray &data);
    19. void writeComm(QByteArray);
    20.  
    21. signals:
    22.  
    23. public slots:
    24.  
    25. };
    26.  
    27. #endif // SERIALCOMM_H
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. #include "serialcomm.h"
    2. #include <iostream>
    3. #include <QString>
    4. #include <QDebug>
    5. #include <QByteArray>
    6.  
    7. #include "abstractserial.h"
    8. #include "abstractserialengine.h"
    9. #include "datatypes.h"
    10. #include "nativeserialengine.h"
    11. #include "winserialnotifier.h"
    12.  
    13. SerialComm::SerialComm(QObject *parent) :
    14. QObject(parent)
    15. {
    16. }
    17.  
    18. void SerialComm::printDataToHex(const QByteArray &data)
    19. {
    20. QByteArray baTmp;
    21. baTmp.clear();
    22. #if QT_VERSION >= 0x040300
    23. baTmp = (data.toHex()).toUpper();
    24. #else
    25. quint8 n=0;
    26. for (int i=0;i<data.size();i++) {
    27. n=data.at(i);
    28. if ((n >= 0) && (n <= 15)) baTmp.append(QByteArray::number(0,16).toUpper());
    29. baTmp.append(QByteArray::number(n,16).toUpper());
    30. }
    31. #endif
    32.  
    33. for (int i=0;i<baTmp.size();i+=2) {
    34. qDebug() << "[" << baTmp.at(i) << baTmp.at(i+1) << "]";
    35. }
    36. }
    37.  
    38. void SerialComm::writeComm(QByteArray ba) {
    39. AbstractSerial *MyDevice = new AbstractSerial();
    40.  
    41. //cout << "Please enter serial device name, specific by OS : ";
    42. //char dn[50];
    43. //cin >> dn;
    44. QString dn = "COM3";
    45. MyDevice->setDeviceName(dn);
    46.  
    47. /*!
    48.   \warning
    49.   \~english To annex "correctly" to work - need to open the device with the flag Unbuffered!
    50.   \~russian Чтобы приложение "корректно" работало - необходимо открывать устройство с флагом Unbuffered!
    51.   */
    52. if (MyDevice->open(QIODevice::WriteOnly | QIODevice::Unbuffered)) {
    53. qDebug() << "Serial device " << MyDevice->deviceName() << " open in " << MyDevice->openMode();
    54. /*
    55.   MyDevice->setDataBits(AbstractSerial::DataBits6);
    56.   MyDevice->setParity(AbstractSerial::ParityMark);
    57.   */
    58. qDebug() << "= Defaults parameters =";
    59. qDebug() << "Device name : " << MyDevice->deviceName();
    60. qDebug() << "Baud rate : " << MyDevice->baudRate();
    61. qDebug() << "Data bits : " << MyDevice->dataBits();
    62. qDebug() << "Parity : " << MyDevice->parity();
    63. qDebug() << "Stop bits : " << MyDevice->stopBits();
    64. qDebug() << "Flow : " << MyDevice->flowControl();
    65. qDebug() << "Char timeout, msec : " << MyDevice->charIntervalTimeout();
    66.  
    67. qint64 bw=0;
    68. while (1) {
    69. qDebug() << "Please enter count bytes for wtitten : ";
    70. bw=0;
    71. //cin >> bw;
    72. qDebug() << "Starting writting " << bw << " bytes in time : " << QTime::currentTime();
    73. ba.clear();
    74. ba.resize(bw);
    75. for (int i=0;i<bw;i++) { //filling data array
    76. ba[i] = i;
    77. }
    78. bw=MyDevice->write(ba);
    79. qDebug() << "Writed is : " << bw << " bytes";
    80. qDebug() << "Tx : ";
    81. printDataToHex(ba);
    82. }
    83. }
    84. else {
    85. qDebug() << "Error opened serial device " << MyDevice->deviceName();
    86. }
    87. MyDevice->close();
    88. qDebug() << "Serial device " << MyDevice->deviceName() << " is closed";
    89. delete MyDevice;
    90. MyDevice=0;
    91. }
    To copy to clipboard, switch view to plain text mode 

    The library headers are visible from my gui application now, but it still won't build.
    Just returns: :-1: error: collect2: ld returned 1 exit status

    No clue as to why it won't build. Any ideas?
    Last edited by waynew; 16th January 2010 at 15:24.

  4. #4
    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: QSerialDevice Problem

    I looked at your example and say the following:
    1. You have in your code is # include "datatypes.h". However, the SVN is no longer the file datatypes.h and therefore it is not necessary.
    2. Just plug the only # include "abstractserial.h" (see examples in / examples)
    3. Show your project file *. pro, because instead of LIBS + =-llibqserialdevice to write LIBS + =-lqserialdevice

    PS: but the main idea - that you downloaded, try to compile the examples from the directory examples without changing them. If writer, reader, or sreader will bring together - then you're in your project properly connect the library

  5. The following user says thank you to kuzulis for this useful post:

    waynew (16th January 2010)

  6. #5
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSerialDevice Problem

    Thanks Kuzulis, builds ok now.
    I'll let you know how it goes.

  7. #6
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSerialDevice Problem

    hi,

    i'm want to use QSerialDevice. (examples working fine). So i copied some code from examples to my project. I want to read data from serial port and write incoming data to a QTextField.
    not a big thing, but my application always freeze after few seconds

    had anyone the same problem and solved it?

  8. #7
    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: QSerialDevice Problem

    1. If the amount of data is small, then they can be read using the signal readyRead() (see the example /examples/sreader)
    2. If a large amount of data - then use the thread
    Last edited by kuzulis; 21st January 2010 at 14:14.

  9. #8
    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: QSerialDevice Problem

    1. If the amount of data is small, then they can be read using the signal [b] readyRead() [/ b] (see the example /examples/sreader)
    2. If a large amount of data - then use the thread

  10. #9
    Join Date
    Oct 2009
    Location
    Germany
    Posts
    13
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSerialDevice Problem

    very fast answer thx

    i'll try and tell you if it runs or not...


    edit:
    now i can start my app 2 times and send messages over serial port thx
    Last edited by drizzt; 21st January 2010 at 15:08.

Similar Threads

  1. QSerialDevice
    By clinisbut in forum Qt Programming
    Replies: 2
    Last Post: 23rd October 2009, 13:30
  2. [solved] QSerialDevice: How to set baudrate?
    By schlingel in forum Qt Programming
    Replies: 1
    Last Post: 14th October 2009, 11:52
  3. Library QSerialDevice v 0.1.0 released
    By kuzulis in forum Qt Programming
    Replies: 0
    Last Post: 15th September 2009, 06:47
  4. question about QSerialDevice (baudrate)
    By mastupristi in forum Qt Programming
    Replies: 1
    Last Post: 18th August 2009, 08:54
  5. new library QSerialDevice
    By kuzulis in forum Qt Programming
    Replies: 2
    Last Post: 13th July 2009, 05:42

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.