Results 1 to 5 of 5

Thread: QT and Libmodbus

  1. #1
    Join Date
    Mar 2015
    Posts
    3
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default QT and Libmodbus

    hi!
    I view the source code of QModMaster and now want to build a Qt GUI application server using libmodbus .
    my application is run on raspberry pi.
    when i run my application, GUI appears only when there is a connection.
    anyone have any examples of this application can send t it? please!

    code:
    tcp.h:
    Qt Code:
    1. #ifndef TCP_H
    2. #define TCP_H
    3.  
    4. #include <QObject>
    5. #include <modbus/modbus.h>
    6. #include <modbus/modbus-tcp.h>
    7. #include <QDebug>
    8. #include <QEventLoop>
    9.  
    10. QT_BEGIN_NAMESPACE
    11.  
    12. //static int NB_CONNECTION;
    13.  
    14. class TcpPrivate;
    15.  
    16. class Q_NETWORK_EXPORT Tcp : public QObject
    17. {
    18. Q_OBJECT
    19. public:
    20. explicit Tcp(QObject *parent = 0);
    21. ~Tcp();
    22.  
    23. bool listen(const char* address, int port,int NB);
    24.  
    25. modbus_t *server;
    26. int ser;
    27.  
    28.  
    29. Q_SIGNALS:
    30. void newconnection();
    31. void accepterror();
    32.  
    33. protected:
    34. virtual void incomingConnection(int handle);
    35. void addPendingConnection(int socket);
    36.  
    37. Tcp(TcpPrivate &dd, QObject *parent = 0);
    38.  
    39. public slots:
    40.  
    41. private:
    42. Q_DISABLE_COPY(Tcp)
    43. Q_DECLARE_PRIVATE(Tcp)
    44. };
    45.  
    46. QT_END_NAMESPACE
    47.  
    48. #endif // TCP_H
    To copy to clipboard, switch view to plain text mode 
    tcp_p.h:
    Qt Code:
    1. #ifndef TCP_P_H
    2. #define TCP_P_H
    3.  
    4. #include "tcp.h"
    5. #include "object_p.h"
    6. #include <QtCore/QList>
    7. #include <QDebug>
    8. //#include <winsock2.h>
    9. #include <sys/socket.h>
    10. #include <netinet/in.h>
    11.  
    12. QT_BEGIN_NAMESPACE
    13.  
    14. class TcpPrivate: public QObjectPrivate
    15. {
    16. Q_DECLARE_PUBLIC(Tcp)
    17. public:
    18. TcpPrivate();
    19. ~TcpPrivate();
    20.  
    21. QList<int *> pendingConnections;
    22.  
    23. quint16 port;
    24. const char* address;
    25. int maxConnections;
    26.  
    27. void readNotification();
    28. void setreadNotification(bool enable);
    29. };
    30.  
    31. QT_END_NAMESPACE
    32.  
    33. #endif // TCP_P_H
    To copy to clipboard, switch view to plain text mode 
    tcp.cpp:
    Qt Code:
    1. #include "tcp.h"
    2. #include "tcp_p.h"
    3. #include <qlist.h>
    4.  
    5. QT_BEGIN_NAMESPACE
    6.  
    7. /*! \internal
    8. */
    9. TcpPrivate::TcpPrivate()
    10. : port(0)
    11. , maxConnections(30)
    12. {
    13. }
    14.  
    15. /*! \internal
    16. */
    17.  
    18. TcpPrivate::~TcpPrivate()
    19. {
    20. }
    21.  
    22. void TcpPrivate::readNotification()
    23. {
    24. Q_Q(Tcp);
    25. qDebug() << "dang o for";
    26. if(pendingConnections.count() > maxConnections)
    27. {
    28. qDebug() << "too many connections";
    29. return;
    30. }
    31.  
    32. socklen_t addrlen;
    33. struct sockaddr_in clientaddr;
    34. int descriptor;
    35.  
    36. qDebug() << "ser" << q->ser;
    37. descriptor = accept(q->ser,(struct sockaddr*)&clientaddr,&addrlen);
    38. qDebug() << "descriptor " << descriptor;
    39. if(descriptor == -1)
    40. {
    41. //q->pauseaccepting();
    42. qDebug() << "accept connection failed";
    43. emit q->accepterror();
    44. return;
    45.  
    46. }
    47.  
    48. qDebug("TcpPrivate::processIncomingConnection() accepted socket %i", descriptor);
    49.  
    50. q->incomingConnection(descriptor);
    51.  
    52. emit q->newconnection();
    53.  
    54. qDebug() << "emit newconnection";
    55. //return;
    56. }
    57.  
    58. /*! \internal
    59. */
    60.  
    61. void TcpPrivate::setreadNotification(bool enable)
    62. {
    63. if(enable)
    64. readNotification();
    65. }
    66.  
    67. /*! \internal
    68. */
    69.  
    70. Tcp::Tcp(TcpPrivate &dd, QObject *parent)
    71. : QObject(dd, parent)
    72. {
    73. }
    74.  
    75. Tcp::Tcp(QObject *parent) : QObject(*new TcpPrivate, parent)
    76. {
    77. }
    78.  
    79. Tcp::~Tcp()
    80. {
    81.  
    82. }
    83.  
    84. bool Tcp::listen(const char* address, int port,int NB)
    85. {
    86. Q_D(Tcp);
    87. modbus_t *mb_tcp;
    88. mb_tcp = modbus_new_tcp(address,port);
    89. ser = modbus_tcp_listen(mb_tcp,NB);
    90. if(ser == -1)
    91. {
    92. qDebug() << "listen fail";
    93. return false;
    94. }
    95.  
    96. qDebug() << "listenning on socket "<<ser;
    97.  
    98. d->setreadNotification(true);
    99. }
    100.  
    101. void Tcp::incomingConnection(int handler)
    102. {
    103. qDebug("QTcpServer::incomingConnection(%i)", handler);
    104.  
    105. int socket;
    106. socket = handler;
    107. // socket->setSocketDescriptor(handler);
    108. addPendingConnection(socket);
    109. }
    110.  
    111. void Tcp::addPendingConnection(int socket)
    112. {
    113. d_func()->pendingConnections.append(&socket);
    114. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp:
    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3.  
    4. MainWindow::MainWindow(QWidget *parent) :
    5. QMainWindow(parent),
    6. ui(new Ui::MainWindow)
    7. {
    8. const char * host = "192.168.0.140";
    9. int port = 1502;
    10. ui->setupUi(this);
    11. new_tcp = new Tcp(this);
    12. connect(new_tcp,SIGNAL(newconnection()),this,SLOT(newconnection()));
    13.  
    14. //connect(this,SIGNAL(newconnection()),&loop,SLOT(quit()));
    15. connect(new_tcp,SIGNAL(accepterror()),this,SLOT(accepterror()));
    16. new_tcp->listen(host,port,5);
    17. //loop.exec();
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by anda_skoa; 2nd March 2015 at 12:20. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT and Libmodbus

    Maybe Tcp::listen() blocks?

    Cheers,
    _

  3. #3
    Join Date
    Mar 2015
    Posts
    3
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT and Libmodbus

    thank for reply!
    I tried to non block non listen socket created by the command deposits,
    fcntl ( socket , F_SETFL , 0 ) However it is still not effective , you have examples of this application does not , sent me ? please!

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT and Libmodbus

    I've not used modbus or its Qt wrapper before so I don't have any examples.
    If you can't make the listen call non-blocking like e.g. QTcpServer's, then you'll have to use a separate thread.

    Cheers,
    _

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

    hoan_do_van (4th March 2015)

  6. #5
    Join Date
    Mar 2015
    Posts
    3
    Thanks
    1
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QT and Libmodbus

    It has been solved.
    Thank you!

Similar Threads

  1. Libmodbus + QT
    By h-n-s in forum Newbie
    Replies: 8
    Last Post: 28th May 2015, 14:31

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.