-
Notifications
You must be signed in to change notification settings - Fork 5
/
functions.py
95 lines (73 loc) · 2.77 KB
/
functions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
__author__ = 'RoGeorge'
import platform
import os
import telnetlib
import sys
# Constants for field index of the instrument answer at a *IDN? command
COMPANY = 0
MODEL = 1
SERIAL = 2
# Check network response (ping)
def ping_IP(instrument, IP):
if platform.system() == "Windows":
response = os.system("ping -n 1 " + IP + " > nul")
else:
response = os.system("ping -c 1 " + IP + " > /dev/null")
if response != 0:
print
print "No response pinging " + IP
print "Check network cables and settings."
print "You should be able to ping the " + instrument + "."
print
# Open a telnet session for Rigol instrument
def connect_to(instrument, IP, port):
tn = telnetlib.Telnet(IP, port)
# Ask for instrument ID
tn.write("*IDN?\n")
instrument_id = tn.read_until("\n", 1)
id_fields = instrument_id.split(",")
# Check if the instrument is set to accept LAN commands
if id_fields[COMPANY] != "RIGOL TECHNOLOGIES":
print instrument_id
print "Non Rigol:,", instrument, "or the", instrument, "does not accept LAN commands."
print "Check the", instrument, "settings."
if instrument == "oscilloscope":
print "Utility -> IO Setting -> RemoteIO -> LAN must be ON"
if instrument == "power supply":
print "Utility -> IO Config -> LAN -> LAN Status must be Configured"
sys.exit("ERROR")
return tn, id_fields
def connect_verify(instrument, IP, port):
ping_IP(instrument, IP)
tn, idFields = connect_to(instrument, IP, port)
if instrument == "oscilloscope" and idFields[MODEL] != "DS1104Z" or \
instrument == "power supply" and idFields[MODEL] != "DP832":
print idFields[MODEL], "is an unknown", instrument, "type."
sys.exit("ERROR")
return tn, idFields
def command(tn, SCPI):
response = ""
while response != "1\n":
tn.write("*OPC?\n") # operation(s) completed ?
response = tn.read_until("\n", 1) # wait max 1s for an answer
tn.write(SCPI)
def init_oscilloscope(tn):
# Channel 4 ON
# BW Limit 20 MHz
# 10 mV/div
# POS 0
# Trig Auto
# Trig Edge
# Trig CH4
tn.write(":MEAS:ITEM VAVG, CHAN1\n")
tn.write("*OPC?\n") # operation(s) completed ?
tn.read_until("\n", 1) # wait max 1s for an answer
tn.write(":MEAS:ITEM VAVG, CHAN2\n")
tn.write("*OPC?\n") # operation(s) completed ?
tn.read_until("\n", 1) # wait max 1s for an answer
tn.write(":MEAS:ITEM VAVG, CHAN3\n")
tn.write("*OPC?\n") # operation(s) completed ?
tn.read_until("\n", 1) # wait max 1s for an answer
tn.write(":MEAS:ITEM VAVG, CHAN4\n")
tn.write("*OPC?\n") # operation(s) completed ?
tn.read_until("\n", 1) # wait max 1s for an answer