-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRunDcMode.py
More file actions
107 lines (85 loc) · 4.44 KB
/
RunDcMode.py
File metadata and controls
107 lines (85 loc) · 4.44 KB
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
# Goal: Connect to a SpikeSafe and run DC mode into an LED, Laser, or electrical component for 10 seconds
# Expectation: Channel 1 will be driven with 100mA with a forward voltage of <1V during this time
import sys
import time
import logging
import spikesafe_python
### set these before starting application
# SpikeSafe IP address and port number
ip_address: str = '10.0.0.220'
port_number: int = 8282
### setting up sequence log
log = logging.getLogger(__name__)
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s.%(msecs)03d, %(levelname)s, %(message)s',
datefmt='%m/%d/%Y %I:%M:%S',
handlers=[
logging.FileHandler("SpikeSafePythonSamples.log"),
logging.StreamHandler(sys.stdout)
]
)
### start of main program
try:
log.info("RunDcMode.py started.")
log.info("Python version: {}".format(sys.version))
# instantiate new TcpSocket to connect to SpikeSafe
tcp_socket = spikesafe_python.TcpSocket(enable_logging=False)
tcp_socket.open_socket(ip_address, port_number)
# reset to default state and check for all events,
# it is best practice to check for errors after sending each command
tcp_socket.send_scpi_command('*RST')
spikesafe_python.ReadAllEvents.log_all_events(tcp_socket)
# parse the SpikeSafe information
spikesafe_info = spikesafe_python.SpikeSafeInfoParser.parse_spikesafe_info(tcp_socket)
# set Channel 1's pulse mode to DC and check for all events
tcp_socket.send_scpi_command('SOUR1:FUNC:SHAP DC')
spikesafe_python.ReadAllEvents.log_all_events(tcp_socket)
# set Channel 1's safety threshold for over current protection to 50% and check for all events
tcp_socket.send_scpi_command('SOUR1:CURR:PROT 50')
spikesafe_python.ReadAllEvents.log_all_events(tcp_socket)
# set Channel 1's current to 100 mA and check for all events
tcp_socket.send_scpi_command(f'SOUR1:CURR {spikesafe_python.Precision.get_precise_current_command_argument(0.1)}')
spikesafe_python.ReadAllEvents.log_all_events(tcp_socket)
# set Channel 1's voltage to 10 V and check for all events
compliance_voltage: float = 20
tcp_socket.send_scpi_command(f'SOUR1:VOLT {spikesafe_python.Precision.get_precise_compliance_voltage_command_argument(compliance_voltage)}')
spikesafe_python.ReadAllEvents.log_all_events(tcp_socket)
# turn on Channel 1 and check for all events
tcp_socket.send_scpi_command('OUTP1 1')
spikesafe_python.ReadAllEvents.log_all_events(tcp_socket)
# wait until the channel is fully ramped to 10mA
spikesafe_python.ReadAllEvents.read_until_event(tcp_socket, spikesafe_python.SpikeSafeEvents.CHANNEL_READY) # event 100 is "Channel Ready"
# check for all events and measure readings on Channel 1 once per second for 15 seconds,
# it is best practice to do this to ensure Channel 1 is on and does not have any errors
time_end = time.time() + 15
while time.time() < time_end:
spikesafe_python.ReadAllEvents.log_all_events(tcp_socket)
spikesafe_python.MemoryTableReadData.log_memory_table_read(tcp_socket)
spikesafe_python.Threading.wait(1)
# turn off Channel 1 and check for all events
tcp_socket.send_scpi_command('OUTP1 0')
spikesafe_python.ReadAllEvents.log_all_events(tcp_socket)
# wait until the channel is fully discharged
spikesafe_python.Discharge.wait_for_spikesafe_channel_discharge(
spikesafe_socket=tcp_socket,
spikesafe_info=spikesafe_info,
compliance_voltage=compliance_voltage,
channel_number=1)
# check Channel 1 is off
spikesafe_python.MemoryTableReadData.log_memory_table_read(tcp_socket)
# disconnect from SpikeSafe
tcp_socket.close_socket()
log.info("RunDcMode.py completed.\n")
except spikesafe_python.SpikeSafeError as ssErr:
# print any SpikeSafe-specific error to both the terminal and the log file, then exit the application
error_message = 'SpikeSafe error: {}\n'.format(ssErr)
log.error(error_message)
print(error_message)
sys.exit(1)
except Exception as err:
# print any general exception to both the terminal and the log file, then exit the application
error_message = 'Program error: {}\n'.format(err)
log.error(error_message)
print(error_message)
sys.exit(1)