thamwayprot.cpp
1 /***************************************************************************
2  Copyright (C) 2002-2015 Kentaro Kitagawa
3  kitagawa@phys.s.u-tokyo.ac.jp
4 
5  This program is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License as published by the Free Software Foundation; either
8  version 2 of the License, or (at your option) any later version.
9 
10  You should have received a copy of the GNU Library General
11  Public License and a list of authors along with this program;
12  see the files COPYING and AUTHORS.
13 ***************************************************************************/
14 #include "analyzer.h"
15 #include "charinterface.h"
16 #include "thamwayprot.h"
17 #include "ui_thamwayprotform.h"
18 #include <QStatusBar>
19 
20 REGISTER_TYPE(XDriverList, ThamwayT300ImpedanceAnalyzer, "Thamway T300-1049A Impedance Analyzer");
21 REGISTER_TYPE(XDriverList, ThamwayCharPROT, "Thamway PROT NMR.EXE TCP/IP Control");
22 #ifdef USE_EZUSB
23  REGISTER_TYPE(XDriverList, ThamwayUSBPROT, "Thamway PROT NMR USB Control");
24  XThamwayUSBPROT::XThamwayUSBPROT(const char *name, bool runtime,
25  Transaction &tr_meas, const shared_ptr<XMeasure> &meas) : XThamwayPROT<XThamwayMODCUSBInterface>(name, runtime, ref(tr_meas), meas) {
26  interface()->setEOS("\r\n");
27  }
29  interface()->query("*IDN?");
30  fprintf(stderr, "PROT:%s\n", interface()->toStr().c_str());
32  }
33 #endif
34 
35 XThamwayCharPROT::XThamwayCharPROT(const char *name, bool runtime,
36  Transaction &tr_meas, const shared_ptr<XMeasure> &meas) :
37  XThamwayPROT<XCharInterface>(name, runtime, ref(tr_meas), meas) {
38  trans( *this->interface()->port()) = "127.0.0.1:5025";
39  trans( *this->interface()->device()) = "TCP/IP";
40  this->interface()->setEOS("\n");
41 }
42 
43 template <class tInterface>
44 XThamwayPROT<tInterface>::XThamwayPROT(const char *name, bool runtime,
45  Transaction &tr_meas, const shared_ptr<XMeasure> &meas)
46  : XCharDeviceDriver<XSG, tInterface>(name, runtime, ref(tr_meas), meas),
47  m_rxGain(XSG::create<XDoubleNode>("RXGain", true, "%.0f")),
48  m_rxPhase(XSG::create<XDoubleNode>("RXPhase", true, "%.1f")),
49  m_rxLPFBW(XSG::create<XDoubleNode>("RXLPFBW", true, "%.4g")),
50  m_form(new FrmThamwayPROT(g_pFrmMain)) {
51 
52  m_form->statusBar()->hide();
53  m_form->setWindowTitle(i18n("Thamway PROT Control - ") + this->getLabel() );
54 
55  m_form->m_dblOutput->setRange(0, 1023);
56  m_form->m_dblOutput->setSingleStep(1);
57  m_form->m_dblRXGain->setRange(0, 95);
58  m_form->m_dblRXGain->setSingleStep(2.0);
59  m_form->m_dblRXPhase->setRange(0, 360);
60  m_form->m_dblRXPhase->setSingleStep(1.0);
61 
62 // XSG::m_conUIs.clear();
63 // XSG::m_form.reset();
64  m_conUIs = {
65  xqcon_create<XQToggleButtonConnector>(this->rfON(), m_form->m_ckbRFON),
66  xqcon_create<XQDoubleSpinBoxConnector>(this->oLevel(), m_form->m_dblOutput, m_form->m_slOutput),
67  xqcon_create<XQLineEditConnector>(this->freq(), m_form->m_edFreq),
68  xqcon_create<XQDoubleSpinBoxConnector>(m_rxGain, m_form->m_dblRXGain, m_form->m_slRXGain),
69  xqcon_create<XQDoubleSpinBoxConnector>(m_rxPhase, m_form->m_dblRXPhase, m_form->m_slRXPhase),
70  xqcon_create<XQLineEditConnector>(m_rxLPFBW, m_form->m_edRXLPFBW)
71  };
72  this->rfON()->setUIEnabled(false);
73  this->oLevel()->setUIEnabled(false);
74  this->freq()->setUIEnabled(false);
75  this->amON()->disable();
76  this->fmON()->disable();
77  rxGain()->setUIEnabled(false);
78  rxPhase()->setUIEnabled(false);
79  rxLPFBW()->setUIEnabled(false);
80 }
81 template <class tInterface>
82 void
84  m_form->showNormal();
85  m_form->raise();
86 }
87 template <class tInterface>
88 void
90  XScopedLock<XInterface> lock( *this->interface());
91  this->interface()->query("FREQR");
92  double f;
93  for(int i = 0; ; ++i) {
94  if(this->interface()->scanf("FREQR%lf", &f) == 1)
95  break;
96  if(i > 3)
97  throw XInterface::XConvError(__FILE__, __LINE__);
98  this->interface()->receive(); //flushing not-welcome message if any, although this is TCP connection.
99  }
100  this->interface()->query("ATT1R");
101  double olevel;
102  if(this->interface()->scanf("ATT1R%lf", &olevel) != 1)
103  throw XInterface::XConvError(__FILE__, __LINE__);
104 // olevel = log10(olevel / 1023.0) * 20.0;
105  this->interface()->query("GAINR");
106  double gain;
107  if(this->interface()->scanf("GAINR%lf", &gain) != 1)
108  throw XInterface::XConvError(__FILE__, __LINE__);
109  this->interface()->query("PHASR");
110  double phase;
111  if(this->interface()->scanf("PHASR%lf", &phase) != 1)
112  throw XInterface::XConvError(__FILE__, __LINE__);
113  this->interface()->query("LPF1R");
114  double bw;
115  if(this->interface()->scanf("LPF1R%lf", &bw) != 1)
116  throw XInterface::XConvError(__FILE__, __LINE__);
117  bw *= 1e-3;
118  this->iterate_commit([=](Transaction &tr){
119  tr[ *this->freq()] = f;
120  tr[ *this->oLevel()] = olevel;
121  tr[ *this->rxGain()] = gain;
122  tr[ *this->rxPhase()] = phase;
123  tr[ *this->rxLPFBW()] = bw;
124  });
125 
126  XSG::start();
127 
128  rxGain()->setUIEnabled(true);
129  rxPhase()->setUIEnabled(true);
130  rxLPFBW()->setUIEnabled(true);
131 
132  this->iterate_commit([=](Transaction &tr){
133  m_lsnRFON = tr[ *this->rfON()].onValueChanged().connectWeakly(
134  this->shared_from_this(), &XThamwayPROT::onRFONChanged);
135  m_lsnOLevel = tr[ *this->oLevel()].onValueChanged().connectWeakly(
136  this->shared_from_this(), &XThamwayPROT::onOLevelChanged);
137  m_lsnFreq = tr[ *this->freq()].onValueChanged().connectWeakly(
138  this->shared_from_this(), &XThamwayPROT::onFreqChanged);
139  m_lsnRXGain = tr[ *rxGain()].onValueChanged().connectWeakly(
140  this->shared_from_this(), &XThamwayPROT::onRXGainChanged);
141  m_lsnRXPhase = tr[ *rxPhase()].onValueChanged().connectWeakly(
142  this->shared_from_this(), &XThamwayPROT::onRXPhaseChanged);
143  m_lsnRXLPFBW = tr[ *rxLPFBW()].onValueChanged().connectWeakly(
144  this->shared_from_this(), &XThamwayPROT::onRXLPFBWChanged);
145  });
146 }
147 template <class tInterface>
148 void
150  rxGain()->setUIEnabled(false);
151  rxPhase()->setUIEnabled(false);
152  rxLPFBW()->setUIEnabled(false);
153 
154  m_lsnRFON.reset();
155  m_lsnOLevel.reset();
156  m_lsnFreq.reset();
157  m_lsnRXGain.reset();
158  m_lsnRXPhase.reset();
159  m_lsnRXLPFBW.reset();
160 
161  XSG::stop();
162 }
163 
164 template <class tInterface>
165 void
167  XScopedLock<XInterface> lock( *this->interface());
168  this->interface()->sendf("FREQW%010.6f", mhz);
169  msecsleep(50); //wait stabilization of PLL
170 }
171 template <class tInterface>
172 void
174  this->interface()->sendf("RFSWW%s", shot[ *this->rfON()] ? "1" : "0");
175 }
176 template <class tInterface>
177 void
179  int olevel = (int)shot[ *this->oLevel()]; //pow(10, shot[ *this->oLevel()] / 20.0) * 1023.0);
180  olevel = std::min(1023, std::max(0, olevel));
181  this->interface()->sendf("ATT1W%04.0f", (double)olevel);
182 }
183 template <class tInterface>
184 void
186  double gain = shot[ *rxGain()];
187  gain = std::min(95.0, std::max(0.0, gain));
188  this->interface()->sendf("GAINW%02.0f", gain);
189 }
190 template <class tInterface>
191 void
193  double phase = shot[ *rxPhase()];
194  phase -= floor(phase / 360.0) * 360.0;
195  this->interface()->sendf("PHASW%05.1f", phase);
196 }
197 template <class tInterface>
198 void
200  double bw = shot[ *rxLPFBW()];
201 // bw = std::min(200.0, std::max(0.0, bw));
202  this->interface()->sendf("LPF1W%07.0f", bw * 1e3);
203 }
204 
206 #ifdef USE_EZUSB
208 #endif
209 
210 XThamwayT300ImpedanceAnalyzer::XThamwayT300ImpedanceAnalyzer(const char *name, bool runtime,
211  Transaction &tr_meas, const shared_ptr<XMeasure> &meas) :
212  XCharDeviceDriver<XNetworkAnalyzer>(name, runtime, ref(tr_meas), meas) {
213  interface()->setEOS("\r\n");
214  interface()->setSerialBaudRate(115200);
215  interface()->setSerialStopBits(1);
216  interface()->setSerialFlushBeforeWrite(true);
217  trans( *interface()->device()) = "SERIAL";
218 
219  average()->disable();
220 
221  calThru()->disable();
222 }
223 
224 void
226  interface()->query("GET START?");
227  double freq;
228  if(interface()->scanf("START %lf", &freq) != 1)
229  throw XInterface::XConvError(__FILE__, __LINE__);
230  trans( *startFreq()) = freq;
231  interface()->query("GET STOP?");
232  if(interface()->scanf("STOP %lf", &freq) != 1)
233  throw XInterface::XConvError(__FILE__, __LINE__);
234  trans( *stopFreq()) = freq;
235  unsigned int samples;
236  interface()->query("GET SAMPLE?");
237  if(interface()->scanf("SAMPLE %u", &samples) != 1)
238  throw XInterface::XConvError(__FILE__, __LINE__);
239  trans( *points()) = samples;
240 
241 // interface()->send("RJX FORMAT"); //R+jX format
242  interface()->send("LOG FORMAT"); //log format
243 
244  start();
245 }
246 void
247 XThamwayT300ImpedanceAnalyzer::onStartFreqChanged(const Snapshot &shot, XValueNodeBase *) {
248  interface()->sendf("SET START %f", (double)shot[ *startFreq()]);
249 }
250 void
251 XThamwayT300ImpedanceAnalyzer::onStopFreqChanged(const Snapshot &shot, XValueNodeBase *) {
252  interface()->sendf("SET STOP %f", (double)shot[ *stopFreq()]);
253 }
254 void
255 XThamwayT300ImpedanceAnalyzer::onPointsChanged(const Snapshot &shot, XValueNodeBase *) {
256  interface()->sendf("SET SAMPLE %u", (unsigned int)shot[ *points()]);
257 }
258 void
259 XThamwayT300ImpedanceAnalyzer::onCalOpenTouched(const Snapshot &shot, XTouchableNode *) {
260  interface()->query("CAL OPEN");
261  if(interface()->toStrSimplified() != "CAL OPEN DONE")
262  throw XInterface::XInterfaceError("Calibration has failed.", __FILE__, __LINE__);
263 }
264 void
265 XThamwayT300ImpedanceAnalyzer::onCalShortTouched(const Snapshot &shot, XTouchableNode *) {
266  interface()->query("CAL SHORT");
267  if(interface()->toStrSimplified() != "CAL SHORT DONE")
268  throw XInterface::XInterfaceError("Calibration has failed.", __FILE__, __LINE__);
269 }
270 void
271 XThamwayT300ImpedanceAnalyzer::onCalTermTouched(const Snapshot &shot, XTouchableNode *) {
272  interface()->query("CAL LOAD");
273  if(interface()->toStrSimplified() != "CAL LOAD DONE")
274  throw XInterface::XInterfaceError("Calibration has failed.", __FILE__, __LINE__);
275 }
276 
277 void
278 XThamwayT300ImpedanceAnalyzer::getMarkerPos(unsigned int num, double &x, double &y) {
279  if(num > 2)
280  throw XDriver::XSkippedRecordError(__FILE__, __LINE__);
281  Snapshot shot( *this);
282  const auto trace = shot[ *this].trace();
283  double v = (num == 0) ? 1e10 : -1.0;
284  unsigned int idx;
285  for(unsigned int i = 0; i < shot[ *this].length(); i++) {
286  double z = std::norm( trace[i]);
287  if(num == 0) {
288  if(v > z) {
289  v = z;
290  idx = i;
291  }
292  }
293  else {
294  if(v < z) {
295  v = z;
296  idx = i;
297  }
298  }
299  }
300  x *= idx * shot[ *this].freqInterval() + shot[ *this].startFreq();
301  y = log10(v) * 20.0;
302 }
303 void
304 XThamwayT300ImpedanceAnalyzer::oneSweep() {
305 }
306 void
307 XThamwayT300ImpedanceAnalyzer::startContSweep() {
308 }
309 void
310 XThamwayT300ImpedanceAnalyzer::acquireTrace(shared_ptr<RawData> &writer, unsigned int ch) {
311  XScopedLock<XInterface> lock( *interface());
312  uint32_t len;
313  interface()->query("MEAS ON");
314  if(interface()->scanf("LOG MAG,F=,M=,%u", &len) != 1)
315  throw XInterface::XConvError(__FILE__, __LINE__);
316  writer->push((uint32_t)0); //LOG MAG
317  writer->push(len);
318  for(unsigned int i = 0; i < len; ++i) {
319  interface()->receive();
320  float freq, amp;
321  if(interface()->scanf("%f,%f", &freq, &amp) != 2)
322  throw XInterface::XConvError(__FILE__, __LINE__);
323  writer->push(freq);
324  writer->push(amp);
325  }
326 }
327 void
329  const Snapshot &shot(tr);
330  uint32_t stype = reader.pop<uint32_t>();
331  uint32_t samples = reader.pop<uint32_t>();
332  switch(stype) {
333  case 0: //Log format.
334  break;
335  default:
336  throw XRecordError(i18n("Given format is not supported."), __FILE__, __LINE__);
337  }
338 
339  tr[ *this].trace_().resize(samples);
340  float startfreq = 0.0, stopfreq = 0.0;
341  for(unsigned int i = 0; i < samples; i++) {
342  float freq = reader.pop<float>();
343  if(i == 0)
344  startfreq = freq;
345  if(i == samples - 1)
346  stopfreq = freq;
347  tr[ *this].trace_()[i] = pow(10.0, reader.pop<float>() / 20.0);
348  }
349  tr[ *this].m_startFreq = startfreq;
350  tr[ *this].m_freqInterval = (stopfreq - startfreq) / (samples - 1);
351 }

Generated for KAME4 by  doxygen 1.8.3