forked from BogdoKhan/multichannel-spec
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
248 lines (200 loc) · 11.2 KB
/
test.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
'''
Project : Spectrometer
Author : chumadan
Contacts : [email protected]
Workfile : test.py
Description : Main file for multi-channel spectrometer
'''
import sys
import os
import pyqtgraph as pg
from PyQt5.QtWidgets import (
QApplication, QMainWindow,
QLabel,QPushButton, QWidget,
QVBoxLayout, QHBoxLayout,
QTabWidget, QToolBar, QCheckBox, QTableWidgetItem, QGridLayout, QLineEdit
)
from PyQt5.QtCore import (
pyqtSignal, QThread,
QThreadPool, QSettings,
QEvent, Qt, pyqtSlot
)
from PyQt5.QtGui import (
QPixmap, QFont, QIcon
)
sys.path.append("./transport/")
import oscilloscope.oscilloscope as oscope
import spectrometer.spmeters as spmeters
from spectrometer.spmeter_base import SpectraDAQ
from widgets.widgets import ConnectionWidget, DaqWidget
from transport import api, api_param
from devices.devices import DeviceConn_MasterSlave, DevicesMap, Device, Channel, ChannelWindow, BoardsManager, BoardWindow
import numpy as np
from analitics.analitics import Analitics
class MainWindow(QMainWindow):
signal_dump_wave = pyqtSignal(object, object, object)
def __init__(self):
super().__init__()
self.setupUI()
#connection parameters
self.transport_param = {'comname':'', 'speed':'', 'stopbits':'', 'ip':'', 'user':'', 'password':'', 'transport':2, 'port':5000}
self.daq_cont = {'runned':False, 'need_reset':True, 'dumping_wave':False}
self.settings = {'ip':'192.168.0.20', 'port':5000}
#threads for board connection and DAQ process
self.board_thread = QThread()
self.thr_daq = QThread()
#add board through SCPI transport and its IP
self.board = api.api(self.daq_cont, self.transport_param, False, False)
self.board.moveToThread(self.board_thread)
#connect oscilloscope widget
self.oscope_widget.signal_dump_wave.connect(self.slot_dump_wave)
self.signal_dump_wave.connect(self.board.dump_wave)
self.oscope_widget.signal_dump_wave_stop.connect(self.slot_dump_wave_done)
self.board.signal_upd_wave.connect(self.oscope_widget.slot_on_wave_update)
self.oscope_widget.signal_dump_wave_board.connect(self.slot_dump_wave_board) #vve: connection for change ip board for get oscillogram
#make connections for device connection window
self.cwidget.conn_window.btn_connect.clicked.connect(lambda: self.connectDevice(self.cwidget.conn_window))
self.cwidget.conn_window.btn_delete.clicked.connect(lambda: self.removeDevice(self.cwidget.conn_window))
#start thread for board connection
self.board_thread.start()
#create thread, connections and object for DAQ processor
self.worker = SpectraDAQ(self.devicesMap, True)
self.worker.moveToThread(self.thr_daq)
self.worker.signal_dataReady.connect(lambda devicesMap, entry, useSum=True: self.m_sumSpectrometer.slot_on_spec_update(devicesMap, entry, useSum))
self.worker.signal_dataReady.connect(lambda devicesMap, entry, useSum=False: self.m_specWidget.slot_on_spec_update(devicesMap, entry, useSum))
self.worker.finished.connect(self.thr_daq.terminate)
self.thr_daq.started.connect(self.worker.run)
self.daqwidget.btn_get_single_spectrum.clicked.connect(self.get_single_spectrum)
self.daqwidget.btn_get_full_spectrum.clicked.connect(self.get_series_of_spectra)
self.daqwidget.btn_get_full_spectrum.clicked.connect(self.m_sumSpectrometer.clear_data)
self.daqwidget.btn_interrupt.clicked.connect(self.setStop)
#create connections for spectrometer widgets
self.m_specWidget.btn_clear.clicked.connect(self.clear_spec)
self.m_sumSpectrometer.btn_clear.clicked.connect(self.clear_spec)
def clear_spec(self): #clear spectrum physically, for each channel
for device in self.devicesMap.values():
for chan in device.channels:
chan.data = [0] * 1024
print("Data cleared")
def setStop(self): #slot that stops spectrum measurement
self.worker.setStop = True
def get_single_spectrum(self): #shoot only one frame of spectrum
self.worker.nEntries = 1
self.thr_daq.start()
def get_series_of_spectra(self): #shoot many frames of spectrum during the data acquisition time
self.worker.nEntries = self.daqwidget.nFrames
self.thr_daq.start()
#ocsope connections for wave dumping
def slot_dump_wave_done(self):
self.daq_cont['dumping_wave'] = False
def slot_dump_wave(self, samples_num, waves_num, channel_id):
self.signal_dump_wave.emit(samples_num, waves_num, channel_id)
def slot_dump_wave_board(self):
self.oscope_widget.chnum, self.devip = self.oscope_widget.m_comboBox_channel_id.currentData()
self.settings["ip"] = self.devip
self.transport_param["ip"] = self.settings["ip"]
#self.transport_param["port"] = self.settings["port"]
self.board.connect(self.settings)
#vve: slot for change ip board every time when get wave pressed
def setupUI(self): #sets up UI in main window
self.setWindowTitle("Multi-channel spectrometer")
self.cwidget = ConnectionWidget()
win_lyout = QVBoxLayout()
self.boards_mgr = BoardsManager()
self.devicesMap = self.cwidget.tbl_devices_list.devicesMap #!!!connect DeviceMap from table to main container
self.daqwidget = DaqWidget(self.devicesMap)
self.oscope_widget = oscope.OscilloscopeW()
self.m_specWidget = spmeters.ChanSpectrometer()
self.m_sumSpectrometer = spmeters.SumSpectrometer()
toolbar = QToolBar("Main toolbar")
self.addToolBar(toolbar)
hlayout = QHBoxLayout()
hlayout.addLayout(win_lyout)
tabs_lyout = QTabWidget()
tabs_lyout.addTab(self.oscope_widget, "Oscilloscope")
tabs_lyout.addTab(self.m_specWidget, "Spectrometer")
tabs_lyout.addTab(self.m_sumSpectrometer, "SMeter (SUM)")
hlayout.addWidget(tabs_lyout)
centerwidget = QWidget()
win_lyout.addWidget(self.cwidget)
win_lyout.addWidget(self.daqwidget)
win_lyout.addWidget(self.boards_mgr)
centerwidget.setLayout(hlayout)
self.setCentralWidget(centerwidget)
def connectDevice(self, window): #sets connection configuration and connect to device
self.settings["ip"] = window.lne_ip_edit.text()
self.transport_param["ip"] = self.settings["ip"]
self.transport_param["port"] = self.settings["port"]
self.board.connect(self.settings)
self.board.transport.client.timeout = 20000 #large timeout for SCPI commands
#get uptime and connection status
uptime_seconds = self.board.transport.transaction([api_param.SCPI_GET_UPTIME], True)[1]
window.setValuesAfterConnection(uptime_seconds, self.board.check_connect()) #update data in connection widget
#if device was not connected previously, add it to the devices table,
#otherwise update information in devices table
if not self.settings["ip"] in self.devicesMap:
self.cwidget.setupData_onConnected(self.board.check_connect(), uptime_seconds, self.devicesMap, False)
else:
self.cwidget.setupData_onConnected(self.board.check_connect(), uptime_seconds, self.devicesMap, True)
isConnected = self.board.check_connect()
print("Connection:", isConnected)
if window.isTableDevices and isConnected: #if current connection manager is with TableDevices and device is connected
#fill table with connected devices
#assign board in devices table to currently connected device
device = self.devicesMap[self.settings["ip"]]
if device.board == None:
device.board = self.board
device.board.transport.client.write('sp:channels?')
numChannels = int(device.board.transport.client.read_raw().decode('utf-8').rstrip())
device.nChannels = numChannels
device.channels = [None] * numChannels
self.buildBoardTab(device) #create tab widget with channels for board
for deviceIP in self.devicesMap:
self.devicesMap[deviceIP].board.transport.client.write('*IDN?')
print("{0}: {1}".format(deviceIP, self.devicesMap[deviceIP].board.transport.client.read_raw().decode('utf-8').rstrip()))
def removeDevice(self, window): #removes device from table
for i in range(self.boards_mgr.count()):
if self.boards_mgr.tabText(i) == window.lne_ip_edit.text():
self.boards_mgr.removeTab(i)
#remove channel from comboBox of oscope
indx = self.oscope_widget.m_comboBox_channel_id.findText(str(window.lne_ip_edit.text()), Qt.MatchContains)
while indx != (-1):
self.oscope_widget.m_comboBox_channel_id.removeItem(indx)
indx = self.oscope_widget.m_comboBox_channel_id.findText(str(window.lne_ip_edit.text()), Qt.MatchContains)
#self.oscope_widget.m_comboBox_channel_id.setCurrentIndex(1)
#self.oscope_widget.m_comboBox_channel_id.setItemText(0,)
else:
pass
self.cwidget.tbl_devices_list.removeData(window.lne_ip_edit.text())
def buildBoardTab(self, device): #creates tab widget with channels for board
if device.deviceTab == None:
newtab = BoardWindow()
device.updTab(newtab)
device.deviceTab.board_ip.setText(self.settings["ip"])
self.boards_mgr.addTab(device.deviceTab, device.ip)
for numCh in range(device.nChannels): #adds channel tabs in board widget
channel = Channel()
channelWin = ChannelWindow()
channel.number = numCh
channel.change_name("Ch{0}".format(channel.number))
#add list of channel to comboBox of oscope
self.oscope_widget.m_comboBox_channel_id.addItem("Ch {0} : Chb{1} : {2}".format(self.oscope_widget.m_comboBox_channel_id.count(), channel.number, device.ip), [channel.number, device.ip])
device.channels[numCh] = channel
channel.channelTab = channelWin
channelWin.isChannelActive.setChecked(device.channels[numCh].isActive)
device.deviceTab.channels_tab.addTab(device.channels[numCh].channelTab, device.channels[numCh].name)
device.channels[numCh].connectActive()
self.updateChannelNames() #update channel names if new boards were connected
def updateChannelNames(self): #update channel names if new boards were connected
i = 0
for device in self.devicesMap.values():
for channel in device.channels:
channel.change_name("Channel{0}".format(i))
channel.channelTab.channel_id.setText(channel.name)
device.deviceTab.channels_tab.setTabText(channel.number, channel.name)
channel.updUI()
i += 1
app = QApplication(sys.argv)
window = MainWindow()
window.show()
app.exec()