Could you please give me some advise on this piece of code?

Discussion in 'Python' started by G., Sep 12, 2011.

  1. G.

    G. Guest

    Dear all,
    I am a python newbie, but I intend to program a python script that
    communicates with Labview via a UDP socket.
    I managed to send LabView strings, but the other way around is not
    The code seems to stop working while in the first while loop. I can
    see the word "test" being print out.

    When I start LabView it is supposed to send a string
    back to python, but sometimes it ends with an error saying the port
    and the ip is already in usage. Do I have to start first LabView or
    the python scrip when listening to LabView, please?

    It would be great if you could have a look on my code and could maybe
    see why it does nothing after "print test", please.

    Thank you very much.

    Kind regards,

    # To change this template, choose Tools | Templates
    # and open the template in the editor.

    __date__ ="$11.09.2011 19:34:03$"

    if __name__ == "__main__":
    print "Das ist ein Test"

    import socket
    import time

    #Define global variables for UDP_IP and UDP_Port, needs to be changed
    for PETRA-3
    #global UDP_IP
    print "UDP_IP is", UDP_IP

    #global UDP_PORT
    print "UDP_PORT is", UDP_PORT

    def openUDPSocket(UDP_IP,UDP_PORT):
    print "Creating socket ..."
    sock=socket.socket( socket.AF_INET, # Internet
    socket.SOCK_DGRAM ) # UDP
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    print "Done."
    return sock

    def fastPump(sock):
    # Start pumping and return

    # print "UDP target IP:", UDP_IP
    # print "UDP target port:", UDP_PORT
    # print "message:", MESSAGE

    sock.sendto( MESSAGE, (UDP_IP, UDP_PORT) )

    def slowPump(sock):
    sock.sendto( MESSAGE, (UDP_IP, UDP_PORT) )


    # Receive messages
    print "test"
    while counter_while_loop < 3:
    data,addr = pumpsocketUDP.recvfrom(1024)
    print "test"
    if not data:
    print "Client has exited!"
    print "\nReceived message '", data,"'"


    while counter_while_loop < 3:

    print "Counter_while_loop", counter_while_loop

    # Close socket
    G., Sep 12, 2011
    1. Advertisements

  2. G.

    Ian Kelly Guest

    If LabView is sending, then the Python script should already be
    receiving, so start Python first.
    I don't think this is sufficient to create a UDP socket. To be safe,
    you should specify the protocol as well:

    sock = socket.socket(socket.AF_INET,
    socket.SOCK_DGRAM, socket.IPPROTO_UDP)
    Your bind call is commented out, which means that your socket will be
    bound to an arbitrary port selected by the OS. This will make it hard
    for LabView to send it messages, since it won't know what port to use.

    Also, you appear to be using the same address and port for both
    endpoints. The UDP socket in Python and the UDP socket in LabView
    should be bound to two separate ports. This is probably why you were
    getting the "port already in use" error, before you commented this
    You don't need this at all. listen() is for TCP sockets.
    The IP and port here should be that of the destination, i.e. the
    address of the LabView socket.
    You should really use a for loop here:

    for counter in range(3):
    data, addr = pumpsocketUPD.recvfrom(1024)
    print "Received message %r" % data

    Note that if data is empty, it means that it received an empty
    message, not that the client has exited. It may be that you're having
    the LabView client send an empty message when it exits, but this does
    not happen automatically.

    Ian Kelly, Sep 12, 2011
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.