-
Notifications
You must be signed in to change notification settings - Fork 1
/
telcommands.py
95 lines (80 loc) · 3.54 KB
/
telcommands.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
from PyQt5.QtCore import (QThread, pyqtSignal)
from telhacks import *
from time import sleep
class TELCommandThread(QThread):
finished = pyqtSignal(int, str)
# we can't print to the QPlainTextEdit from within the thread so we pass the messages using signals/slots
# to the main thread
info = pyqtSignal(str)
warning = pyqtSignal(str)
error = pyqtSignal(str)
critical = pyqtSignal(str)
commands = ['ibwrt', 'ibrd', 'ibrsp', 'ibclr', 'waitsrq', 'pause']
def __init__(self, instr, command, data=None, timeout=None):
super().__init__()
self.instr = instr
self.command = command
self.data = data
self.timeout = timeout
def emitFormatted(self, action, message, status=constants.StatusCode.success):
if 'error' in constants.StatusCode(status).name:
self.error.emit('{:8} {}-> {}'.format('(' + action + ')', message, constants.StatusCode(status).name))
elif 'warning' in constants.StatusCode(status).name:
self.warning.emit('{:8} {}-> {}'.format('(' + action + ')', message, constants.StatusCode(status).name))
else:
if action in ['ibrd', 'ibrsp']:
formatting = '-> '
else:
formatting = ''
self.info.emit('{:8} {}{}'.format('(' + action + ')', formatting, message))
def run(self):
status = constants.StatusCode.success
result = None
# set the timeout if it was specified before performing any action
if self.timeout != None:
self.instr.timeout = self.timeout - self.timeout / 100.0
# perform actions
if self.command == 'ibwrt':
if self.data == '':
self.emitFormatted(self.command, 'Command is not specified')
else:
try:
wr = self.instr.write(self.data)
except VisaIOError as e:
wr = (0, e.error_code)
status = wr[1]
self.emitFormatted(self.command, self.data, status)
elif self.command == 'ibrd':
result = ''
try:
result = self.instr.read()
self.emitFormatted('ibrd', result.rstrip('\r\n'))
except VisaIOError as e:
status = e.error_code
self.emitFormatted('ibrd', '', status)
elif self.command == 'ibrsp':
stb = self.instr.read_stb(previous=self.data)
result = '0x{0:X}'.format(stb)
self.emitFormatted(self.command, result)
elif self.command == 'ibclr':
self.instr.clear()
self.emitFormatted(self.command, '')
elif self.command == 'waitsrq':
self.emitFormatted('waitsrq', 'Waiting on status byte...')
try:
stb = self.instr.wait_for_srq()
#stb = self.instr.read_stb(previous=True)
result = '0x{0:X}'.format(stb)
self.emitFormatted('ibrsp', result)
except VisaIOError as e:
status = e.error_code
self.emitFormatted('waitsrq', '', status)
elif self.command == 'pause':
try:
pause = float(self.data)
self.emitFormatted('pause', 'Pausing execution for {}s...'.format(self.data))
time.sleep(pause)
except:
status = constants.StatusCode.error_invalid_parameter
self.emitFormatted('pause', 'Missing value for pause command', status)
self.finished.emit(status, result)