LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Does Python Close Session close open ports in py file?

Hello, 

 

I am running a Python script to open a port to a TCP connection.  However, in the second session, when trying to run the scrip again, I get an error that the TCP connection was refused.

 

In the python script, I open and close the TCP port like this:

 

def TCP_socket_conn():
    # create TCP socket and UBX reader object
    HOST = "102.104.22.33"
    PORT = 1056
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect((HOST, PORT))
    ubr = UBXReader(s, protfilter=2)

    return s, ubr

s, ubr = TCP_socket_conn()
 
<<RUN SOME CODE>>
 
s.close()
 
It works the first time, but I get the error the second attempt and I have to reset the TCP connection.
 
Here is a snippet of the LabVIEW Python call:
 
hiNI_0-1700276670833.png

 

 Does LabVIEW automatically close the TCP port?
 
As an aside, when I also do this to talk to a piece of equipment, I also get an error the second go around if I close the pyvisa session.  Here is an example of the python code:
 
import pyvisa
import time

t = 0.5
rm = pyvisa.ResourceManager()
instrument = rm.open_resource('TCPIP0::05.20.133.05::8856::SOCKET')

def power_level(command):
       
    instrument.write(command)

# shutdown
rm.close()
 
 Any guidance is sincerely appreciated.
 
Thanks!
0 Kudos
Message 1 of 3
(1,825 Views)

Hello,

 

Could it be a conflict of resources?...as I am opening the instrument's TCPIP port:

instrument = rm.open_resource('TCPIP0::05.20.133.05::8856::SOCKET')

 

then opening the device port:

s, ubr = TCP_socket_conn()

 

then in the end, doing a:

 

s.close()

 

???

0 Kudos
Message 2 of 3
(1,796 Views)

Not sure about the Python implementation of TCP-IP sockets but in the case of your pyVISA implementation I would definitely try to do a close of the instrument session too, before closing of the resource manager session.

 

import pyvisa
import time

t = 0.5
rm = pyvisa.ResourceManager()
instrument = rm.open_resource('TCPIP0::05.20.133.05::8856::SOCKET')

def power_level(command):
       
    instrument.write(command)

instrument.close()
# shutdown
rm.close()
Rolf Kalbermatter
My Blog
Message 3 of 3
(1,732 Views)