-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLogSpecificTcpSocketScpi.py
77 lines (59 loc) · 2.45 KB
/
LogSpecificTcpSocketScpi.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
# Goal:
# Connect to a SpikeSafe and read all events
import sys
import logging
from spikesafe_python.ReadAllEvents import read_all_events
from spikesafe_python.TcpSocket import TcpSocket
from spikesafe_python.SpikeSafeError import SpikeSafeError
### set these before starting application
# SpikeSafe IP address and port number
ip_address = '10.0.0.220'
port_number = 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("LogSpecificTcpSocketScpi.py started.")
log.info("Python version: {}".format(sys.version))
# instantiate new TcpSocket to connect to SpikeSafe
tcp_socket = TcpSocket()
# set TcpSocket to log no SCPI
tcp_socket.enable_logging = False
# set TcpSocket to log all SCPI as info level messages
tcp_socket.default_log_level = 20
# connect to SpikeSafe
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')
# request SpikeSafe memory table but do not print SCPI to file
tcp_socket.send_scpi_command('MEM:TABL:READ', False)
# read SpikeSafe memory table and print SpikeSafe response to the log file
data = tcp_socket.read_data(True)
# read all events in SpikeSafe event queue, store in list, and print them to the log file
# here it's expected to receive 1 event: 102, External Pause Signal Ended
event_data = read_all_events(tcp_socket, True)
# disconnect from SpikeSafe
tcp_socket.close_socket()
log.info("LogSpecificTcpSocketScpi.py completed.\n")
except 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)