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


G

G.

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
working.
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 UDP_sender.vi 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,
G.



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

__author__="User"
__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
UDP_IP="127.0.0.1"
print "UDP_IP is", UDP_IP

#global UDP_PORT
UDP_PORT=21312
print "UDP_PORT is", UDP_PORT





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

def fastPump(sock):
# Start pumping and return
MESSAGE="pumpfast"

# 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):
MESSAGE="pumpslow"
sock.sendto( MESSAGE, (UDP_IP, UDP_PORT) )


pumpsocketUDP=openUDPSocket(UDP_IP,UDP_PORT)


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


counter_while_loop=0

while counter_while_loop < 3:
fastPump(pumpsocketUDP)
time.sleep(5)
slowPump(pumpsocketUDP)
time.sleep(3)

counter_while_loop=counter_while_loop+1
print "Counter_while_loop", counter_while_loop


# Close socket
pumpsocketUDP.close()
 
Ad

Advertisements

I

Ian Kelly

When I start LabView UDP_sender.vi 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?

If LabView is sending, then the Python script should already be
receiving, so start Python first.
def openUDPSocket(UDP_IP,UDP_PORT):
   #UDP_IP="127.0.0.1"
   #UDP_PORT=5005
   print "Creating socket ..."
   sock=socket.socket( socket.AF_INET, # Internet
                       socket.SOCK_DGRAM ) # UDP

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)
   #sock.bind((UDP_IP,UDP_PORT))

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
out.
   #sock.listen(1)

You don't need this at all. listen() is for TCP sockets.
def fastPump(sock):
   # Start pumping and return
   MESSAGE="pumpfast"

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

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

The IP and port here should be that of the destination, i.e. the
address of the LabView socket.
counter_while_loop=0
print "test"
while counter_while_loop < 3:
       data,addr = pumpsocketUDP.recvfrom(1024)
       print "test"
       if not data:
               print "Client has exited!"
               break
       else:
               print "\nReceived message '", data,"'"
       counter_while_loop=counter_while_loop+1

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.

HTH,
Ian
 

Top