Example #1
0
    def __init__(self, host=None, port=None, type=None, parent=None):
        super().__init__(parent)

        mcast_addr = host
        mcast_port = port
        type = type
        self.statusLabel = QLabel("Listening for broadcasted messages")
        quitButton = QPushButton("&Quit")

        self.udpSocket = QUdpSocket(self)
        dstAddress = QHostAddress()
        dstAddress.setAddress(mcast_addr)
        self.udpSocket.bind(dstAddress, mcast_port)

        self.udpSocket.readyRead.connect(self.processPendingDatagrams)
        quitButton.clicked.connect(self.close)

        buttonLayout = QHBoxLayout()
        buttonLayout.addStretch(1)
        buttonLayout.addWidget(quitButton)
        buttonLayout.addStretch(1)

        mainLayout = QVBoxLayout()
        mainLayout.addWidget(self.statusLabel)
        mainLayout.addLayout(buttonLayout)
        self.setLayout(mainLayout)

        self.setWindowTitle("Broadcast Receiver")
Example #2
0
    def __init__(self, params={}, parent=None):
        '''
        Constructor
        '''
        super(UdpDevice, self).__init__(params, parent)

        self.iodevice = QUdpSocket()
        self.reconnect = int(params.get('Reconnect', 1000))
        self.host = params.get('Host', None)
        self.port = int(params.get('Port', 2000))
        self.iodevice.readyRead.connect(self.readyRead)
        self.buffer = bytearray()
Example #3
0
class UdpDevice(DataDevice):
    '''
    Implementation of a UDP server socket
    '''

    readyRead = pyqtSignal()

    def __init__(self, params={}, parent=None):
        '''
        Constructor
        '''
        super(UdpDevice, self).__init__(params, parent)

        self.iodevice = QUdpSocket()
        self.reconnect = int(params.get('Reconnect', 1000))
        self.host = params.get('Host', None)
        self.port = int(params.get('Port', 2000))
        self.iodevice.readyRead.connect(self.readyRead)
        self.buffer = bytearray()

    def connectDevice(self):
        result = False
        if self.host is None:
            result = self.iodevice.bind(self.port)
        else:
            ha = QHostAddress(self.host)
            result = self.iodevice.bind(ha, self.port)
        if result is False:
            if self.reconnect > 0:
                QTimer.singleShot(self.reconnect, self.onReconnectTimer)
        else:
            self.deviceConnected.emit(True)

    def disconnectDevice(self):
        if self.iodevice.state() is QAbstractSocket.BoundState:
            self.iodevice.disconnectFromHost()
            self.deviceDisconnected.emit(True)

    def readData(self):
        (data, ha, port) = self.iodevice.readDatagram(self.iodevice.pendingDatagramSize())
        self.remoteHost = ha.toString()
        self.remotePort = port
        return data

    def readLine(self):
        if self.iodevice.hasPendingDatagrams():
            self.buffer.extend(self.readData())
        try:
            i = self.buffer.index(b'\n')
            data = self.buffer[0:i]
            del self.buffer[0:i + 1]
            return data.decode()
        except UnicodeDecodeError:
            return '<decode error>'
        except ValueError:
            return ''
Example #4
0
class UDPClient(QDialog):

    def __init__(self, host=None, port=None, type=None, parent=None):
        super().__init__(parent)

        mcast_addr = host
        mcast_port = port
        type = type
        self.statusLabel = QLabel("Listening for broadcasted messages")
        quitButton = QPushButton("&Quit")

        self.udpSocket = QUdpSocket(self)
        dstAddress = QHostAddress()
        dstAddress.setAddress(mcast_addr)
        self.udpSocket.bind(dstAddress, mcast_port)

        self.udpSocket.readyRead.connect(self.processPendingDatagrams)
        quitButton.clicked.connect(self.close)

        buttonLayout = QHBoxLayout()
        buttonLayout.addStretch(1)
        buttonLayout.addWidget(quitButton)
        buttonLayout.addStretch(1)

        mainLayout = QVBoxLayout()
        mainLayout.addWidget(self.statusLabel)
        mainLayout.addLayout(buttonLayout)
        self.setLayout(mainLayout)

        self.setWindowTitle("Broadcast Receiver")

    def processPendingDatagrams(self):
        while self.udpSocket.hasPendingDatagrams():
            datagram, host, port = self.udpSocket.readDatagram(
                self.udpSocket.pendingDatagramSize())
            buf = QBuffer()
            b = buf.write(self.udpSocket.readAll())
            buf.seek(buf.pos() - b)
            image = QImage()
            image.loadFromData(buf.buffer())
            image.save(r"D:\test.png")