-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSDM3055.py
154 lines (112 loc) · 5.07 KB
/
SDM3055.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env python
#-----------------------------------------------------------------------------
# https://github.com/marclura/scpi-py-scripts
# Python scripts for Standard Commands for Programmable Instruments
#-----------------------------------------------------------------------------
import socket # for sockets
import sys # for exit
import time # for sleep
import csv # for file saving
from os.path import exists
from datetime import datetime
from array import *
#-----------------------------------------------------------------------------
# Device IP
remote_ip = "192.168.3.51" # should match the instrument’s IP address
port = 5025 # the port number of the instrument service
#-----------------------------------------------------------------------------
# CSV file setting
location = ''
filename = 'M1'
header = ['time', 'VDC'] # setup the header of the CSV file as you need
#-----------------------------------------------------------------------------
# Measurements parameters
count = 5 # amount of measurements
interval = 1 # interval betwween measurements in seconds s
# type of measurement, refer to https://bit.ly/3R07mlq
measlist = [['Voltage DC', 'MEAS:VOLT:DC?'], ['Current DC', 'MEAS:CURR:DC?'], ['Voltage AC', 'MEAS:VOLT:AC?'], ['Current AC', 'MEAS:CURR:AC?'], ['Temperature', 'MEAS:TEMP?'], ['Frequency', 'MEAS:FREQ?'], ['Resistance 2-wire', 'MEAS:RES?'], ['Resistance 4-wire', 'MEAS:FRES?'], ['Capacitance', 'MEAS:CAP?']]
#-----------------------------------------------------------------------------
# Variables
def SocketConnect():
try:
#create an AF_INET, STREAM socket (TCP)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error:
print ('Failed to create socket.')
sys.exit();
try:
#Connect to remote server
s.connect((remote_ip , port))
except socket.error:
print ('failed to connect to ip ' + remote_ip)
return s
def SocketQuery(Sock, cmd):
try :
#Send cmd string
Sock.sendall(cmd)
Sock.sendall(b'\n')
time.sleep(1)
except socket.error:
#Send failed
print ('Send failed')
sys.exit()
reply = Sock.recv(4096)
return reply
def SocketClose(Sock):
#close the socket
Sock.close()
time.sleep(.300)
def cleanqStr(qStr):
return eval(str(qStr.strip(b'\n')).strip('b'))
def getDeviceIDN(s):
IDN = SocketQuery(s, b'*IDN?')
print('\nDEVICE properly connected:\n' + cleanqStr(IDN))
def main():
global remote_ip
global port
global count
print('\n*******************************************************************\nSCPI python: automation measurement for SIGLENT SDM3000 family\n*******************************************************************\n')
remote_ip = input("Enter the remote IP of your device: ")
# Body: send the SCPI commands *IDN? 10 times and print the return message
s = SocketConnect()
getDeviceIDN(s);
print('\nMEASUREMENTS PARAMETERS:\n')
count = input('Enter the amount (a number) of measures you want to do: ')
interval = input('Enter the interval in seconds between measurement: ')
print('\nMeasurement types:\n')
for i in range(len(measlist)):
print(str(i) + ": " + measlist[i][0])
meassel = int(input('\nSelect the measurement type from the list above (enter the line number): '))
measurement = measlist[meassel][1]
filename = ''
filename = input('\nEnter the name of the csv file for the data that you want to use: ')
while(len(filename) < 2):
filename = input('Filename too short, try again: ')
# crete the csv file with the header
path = location + filename + ".csv"
print('Data storage location: ' + path)
if exists(path):
key = input('\nWARNING:\nFile already exists! Press "ENTER" to override and continue or CTRL+C to Cancel\n')
with open(path, 'w', encoding='UTF8', newline='') as f:
writer = csv.writer(f)
writer.writerow(['time', measlist[meassel][0]])
print('Measurements parameters\nCount: ' + str(count) + '\nInterval: ' + str(interval) + ' seconds\nMeasurement: ' + measlist[meassel][0] + '\n')
input('Press "Enter" to start')
print('\nMeasures:\n')
for i in range(int(count)):
qStr = SocketQuery(s, bytes(measurement, 'ascii'))
qStr = cleanqStr(qStr)
date = datetime.now()
# log measurements
print (str(i+1) + " of " + str(count) + " | " + date.strftime("%d/%m/%Y") + " @ " + date.strftime("%H:%M:%S") + " | " + measlist[meassel][0] + ": " + qStr)
# write data to csv file
with open(path, 'a', encoding='UTF8', newline='') as f:
writer = csv.writer(f)
writer.writerow([date, qStr])
# interval time
time.sleep(int(interval))
SocketClose(s)
print('\nMeasures done!\n')
input('Press "Enter" to exit')
if __name__ == '__main__':
proc = main()