1. Getting the IP address of the instruments
a. Setting the IP addresses yourself: Most instruments will come with a pre-set IP address. You will need to change it for security reasons, or if you have multiple instruments of the same type, or if you want your own intranet. If you are setting up the intranet, use ipconfig command/ network properties menu on Windows to get the Gateway and Subnet mask. While each device on intranet will have a different IP address, they will share the Gateway and subnet masks. If you will be assigning the IP address to the instruments, you want DHCP = OFF.
b. Read from the instrument
c. Using NI MAX
NI MAX --> My System --> Network Devices --> right click to see Create new VISA TCP/IP resource --> Auto detect of LAN instrument
I used a Chroma DC electronic load, assigned IP address etc. While I could ping it, it did not show up on the NI MAX autodetect. Turns out I needed raw socket (the instrument uses fixed port number).
NI MAX --> My System --> Network Devices --> cright click to see Create new VISA TCP/IP resource --> Manual entry of raw socket --> enter hostname==IP address, port number.
2. Creating a handle for the resource
A. Instruments that do not need a port number
Once NI MAX detected the resources, I could see them on rm.list_resources(). The address is of the type TCPIP0::<ip_addr>::inst0::INSTR
Open the resource using:
import visa
rm = visa.ResourceManager()
<handle_name> = rm.open_resource('TCPIP::<ip_addr>::INSTR)
Use <handle_name>.write(<command_string>) and <handle_name>.ask(<command_string>).
B. Instruments that need a fixed port number for Ethernet connection
For the Chroma Load, the address is of the type TCPIP0::<ip_addr>::<port_number>::SOCKET.
Method1: Using NI MAX
Open visa test panel
Configuration --> I/O settings --> select termination character as per instrument requirements (else you will get timeout on read)
The user manual for Chroma asked to set the termination character to '\n' before reading/ writing to the instrument.
Next go to Input/ Output tab, and write commands. Then send read command and read returned value.
If you do not set the read termination character in NI MAX, then you can still visually read the returned data in its window, but the read operation will timeout.
Method2: Using visa
import visa
rm = visa.ResourceManager()
<handle_name> = rm.open_resource('TCPIP0::<ip_addr>::<port_number>::SOCKET)
<handle_name>.read_termination = '\n'
<handle_name>.ask('*IDN?')
u'CHROMA,63600-5,636005001078,1.61'
If you do not set the read_termination character, then you will get the following errors:
>>> ld1.ask('*IDN?')
Traceback (most recent call last):
VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.
Method3: Using socket
import socket as sock
<handle_name> = sock.socket(sock.AF_INET, sock.SOCK_STREAM)
<handle_name>.connect((<ip_addr>, <port_number>))
To send command: <handle_name>.send(<string_command_ending_with_\n>)
To read data: <handle_name>.recv(<give_a_buffer_size>)
Example:
import socket as sock
self.load = sock.socket(sock.AF_INET, sock.SOCK_STREAM)
self.load_addr = str(load_addr)
self.load.connect((self.load_addr, port_number))
self.load.send('*IDN?\n')
self.load.recv(256)
I prefer Method 2, because once read_termination is set, you can use write and ask commands normally.
References:
1. pyvisa tutorial
2. read termination
3. socket code
4. Another socket code, with telnet (I havent tried this)
a. Setting the IP addresses yourself: Most instruments will come with a pre-set IP address. You will need to change it for security reasons, or if you have multiple instruments of the same type, or if you want your own intranet. If you are setting up the intranet, use ipconfig command/ network properties menu on Windows to get the Gateway and Subnet mask. While each device on intranet will have a different IP address, they will share the Gateway and subnet masks. If you will be assigning the IP address to the instruments, you want DHCP = OFF.
b. Read from the instrument
c. Using NI MAX
NI MAX --> My System --> Network Devices --> right click to see Create new VISA TCP/IP resource --> Auto detect of LAN instrument
I used a Chroma DC electronic load, assigned IP address etc. While I could ping it, it did not show up on the NI MAX autodetect. Turns out I needed raw socket (the instrument uses fixed port number).
NI MAX --> My System --> Network Devices --> cright click to see Create new VISA TCP/IP resource --> Manual entry of raw socket --> enter hostname==IP address, port number.
2. Creating a handle for the resource
A. Instruments that do not need a port number
Once NI MAX detected the resources, I could see them on rm.list_resources(). The address is of the type TCPIP0::<ip_addr>::inst0::INSTR
Open the resource using:
import visa
rm = visa.ResourceManager()
<handle_name> = rm.open_resource('TCPIP::<ip_addr>::INSTR)
Use <handle_name>.write(<command_string>) and <handle_name>.ask(<command_string>).
B. Instruments that need a fixed port number for Ethernet connection
For the Chroma Load, the address is of the type TCPIP0::<ip_addr>::<port_number>::SOCKET.
Method1: Using NI MAX
Open visa test panel
Configuration --> I/O settings --> select termination character as per instrument requirements (else you will get timeout on read)
The user manual for Chroma asked to set the termination character to '\n' before reading/ writing to the instrument.
Next go to Input/ Output tab, and write commands. Then send read command and read returned value.
If you do not set the read termination character in NI MAX, then you can still visually read the returned data in its window, but the read operation will timeout.
Method2: Using visa
import visa
rm = visa.ResourceManager()
<handle_name> = rm.open_resource('TCPIP0::<ip_addr>::<port_number>::SOCKET)
<handle_name>.read_termination = '\n'
<handle_name>.ask('*IDN?')
u'CHROMA,63600-5,636005001078,1.61'
If you do not set the read_termination character, then you will get the following errors:
>>> ld1.ask('*IDN?')
Traceback (most recent call last):
VisaIOError: VI_ERROR_TMO (-1073807339): Timeout expired before operation completed.
Method3: Using socket
import socket as sock
<handle_name> = sock.socket(sock.AF_INET, sock.SOCK_STREAM)
<handle_name>.connect((<ip_addr>, <port_number>))
To send command: <handle_name>.send(<string_command_ending_with_\n>)
To read data: <handle_name>.recv(<give_a_buffer_size>)
Example:
import socket as sock
self.load = sock.socket(sock.AF_INET, sock.SOCK_STREAM)
self.load_addr = str(load_addr)
self.load.connect((self.load_addr, port_number))
self.load.send('*IDN?\n')
self.load.recv(256)
I prefer Method 2, because once read_termination is set, you can use write and ask commands normally.
References:
1. pyvisa tutorial
2. read termination
3. socket code
4. Another socket code, with telnet (I havent tried this)



