Results 1 to 2 of 2

Thread: Qt UDP network accessing to camera connected by LAN

  1. #1
    Join Date
    Jan 2019
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Question Qt UDP network accessing to camera connected by LAN

    Hello, I am trying to get data from a LAN-connected infrared camera.
    I don't know how to receive packets from the external network.
    Here's my code.

    Qt Code:
    1. MyUDP::MyUDP(QObject *parent) :
    2. QObject(parent)
    3. {
    4. // create a QUDP socket
    5. socket = new QUdpSocket(this);
    6. socket_cam = new QUdpSocket(this);
    7. // The most common way to use QUdpSocket class is
    8. // to bind to an address and port using bind()
    9. // bool QAbstractSocket::bind(const QHostAddress & address,
    10. // quint16 port = 0, BindMode mode = DefaultForPlatform)
    11. socket->bind(QHostAddress::LocalHost, 1234);
    12.  
    13. connect(socket, SIGNAL(readyRead()), this, SLOT(readyRead()));
    14. }
    15.  
    16. void MyUDP::HelloUDP()
    17. {
    18. QByteArray Data;
    19.  
    20. QHostAddress camera_loc = QHostAddress("192.168.10.197");
    21. quint16 cameraPort = 32197;
    22.  
    23. qint64 deg_num = socket->readDatagram(Data.data() ,964, &camera_loc, &cameraPort);
    24. qDebug() << "Deg_num: " << deg_num; //this returns -1. Therefore, it seems it can't read any data from camera_loc.
    25. // Sends the datagram datagram
    26. // to the host address and at port.
    27. // qint64 QUdpSocket::writeDatagram(const QByteArray & datagram,
    28. // const QHostAddress & host, quint16 port)
    29. socket->writeDatagram(Data, QHostAddress::LocalHost, 1234);
    30. }
    31.  
    32. void MyUDP::readyRead()
    33. {
    34. // when data comes in
    35. QByteArray buffer;
    36. buffer.resize(socket->pendingDatagramSize());
    37.  
    38. QHostAddress sender = QHostAddress("192.168.10.197");
    39. quint16 senderPort = 32197;
    40.  
    41. // qint64 QUdpSocket::readDatagram(char * data, qint64 maxSize,
    42. // QHostAddress * address = 0, quint16 * port = 0)
    43. // Receives a datagram no larger than maxSize bytes and stores it in data.
    44. // The sender's host address and port is stored in *address and *port
    45. // (unless the pointers are 0).
    46.  
    47. socket->readDatagram(buffer.data(), buffer.size(),
    48. &sender, &senderPort);
    49. qDebug() << "Message from: " << sender.toString();
    50. qDebug() << "Message port: " << senderPort;
    51. qDebug() << "Message: " << buffer;
    52. }
    To copy to clipboard, switch view to plain text mode 

    I changed a little from Qt5 Tutorial QUdpSocket(https://www.bogotobogo.com/Qt/Qt5_QUdpSocket.php)
    As I've seen on Wireshark, packets arrived correctly as I expected.
    However, my code doesn't work as I expected. (readDatagram on camera_loc returns (-1))
    According to other threads, we don't need to connect them because UDP communication doesn't need to make connections.
    What I want to make with this code is as follow.

    (0. Save data from the camera (192.168.10.197) on a variable using readDatagram) I am not sure this is a really necessary process...
    1. Write data to buffer as written in this code (using writeDatagram function).

    I could not find solutions even if I struggled.
    I thought it would be easy but it wasn't ...
    Any advice will be very grateful because I am a newbie to qt and UDP network.

    Thanks in advance.

    RadioKong

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,335
    Thanks
    36
    Thanked 1,528 Times in 1,479 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt UDP network accessing to camera connected by LAN

    Does the readyRead() slot get called?
    Which value is returned by pendingDatagramSize()?

    The readDatagram() call in HelloUDP() passes a null pointer as the receive buffer. Probably not what you want.

    Cheers,
    _

Similar Threads

  1. Accessing android camera
    By MTRsport in forum Qt Programming
    Replies: 1
    Last Post: 19th August 2015, 11:13
  2. Replies: 3
    Last Post: 9th September 2013, 01:52
  3. QFileSystemModel with network drives not connected
    By GothmoG in forum Qt Programming
    Replies: 2
    Last Post: 3rd October 2012, 01:18
  4. Replies: 3
    Last Post: 9th April 2012, 00:50
  5. find connected network devices
    By Remco in forum Qt Programming
    Replies: 4
    Last Post: 9th September 2010, 09:03

Tags for this Thread

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.