counter.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 "counter.h"
15 
16 #include "interface.h"
17 #include "analyzer.h"
18 
19 XCounter::XCounter(const char *name, bool runtime,
20  Transaction &tr_meas, const shared_ptr<XMeasure> &meas) :
21  XPrimaryDriverWithThread(name, runtime, ref(tr_meas), meas) {
22 }
23 
24 void
26  for(unsigned int ch = 0; ch < m_entries.size(); ch++) {
27  m_entries[ch]->value(tr, reader.pop<double>());
28  }
29 }
30 void
32 }
33 void
34 XCounter::createChannels(Transaction &tr_meas, const shared_ptr<XMeasure> &meas,
35  const char **channel_names) {
36  shared_ptr<XScalarEntryList> entries(meas->scalarEntries());
37 
38  for(int i = 0; channel_names[i]; i++) {
39  shared_ptr<XScalarEntry> entry(create<XScalarEntry>(
40  channel_names[i], false,
41  dynamic_pointer_cast<XDriver>(shared_from_this()), "%.8g"));
42  m_entries.push_back(entry);
43  entries->insert(tr_meas, entry);
44  }
45 }
46 void *
47 XCounter::execute(const atomic<bool> &terminated) {
48  while( !terminated) {
49  msecsleep(50);
50 
51  auto writer = std::make_shared<RawData>();
52  // try/catch exception of communication errors
53  try {
54  unsigned int num = m_entries.size();
55  for(unsigned int ch = 0; ch < num; ch++)
56  writer->push((double)getLevel(ch));
57  }
58  catch (XKameError &e) {
59  e.print(getLabel());
60  continue;
61  }
62 
63  finishWritingRaw(writer, XTime::now(), XTime::now());
64  }
65  return NULL;
66 }
67 
68 #include "charinterface.h"
69 #include "analyzer.h"
70 
71 REGISTER_TYPE(XDriverList, MutohCounterNPS, "Mutoh Digital Counter NPS");
72 
73 #define STX "\x02"
74 
75 XMutohCounterNPS::XMutohCounterNPS(const char *name, bool runtime,
76  Transaction &tr_meas, const shared_ptr<XMeasure> &meas) :
77  XCharDeviceDriver<XCounter>(name, runtime, ref(tr_meas), meas) {
78  const char *channels_create[] = {"Ch0", 0L};
79  createChannels(ref(tr_meas), meas, channels_create);
80 
81  interface()->setSerialParity(XCharInterface::PARITY_EVEN);
82  interface()->setSerialBaudRate(19200);
83  interface()->setSerial7Bits(true);
84  interface()->setEOS("\x03"); //ETX
85 }
86 
87 double
88 XMutohCounterNPS::getLevel(unsigned int ch) {
89  XScopedLock<XInterface> lock( *interface());
90  interface()->query(STX "00F102");
91  int fun2;
92  if(interface()->scanf(STX "00F202%d", &fun2) != 1)
93  throw XInterface::XConvError(__FILE__, __LINE__);
94 
95  interface()->query(STX "00F105");
96  int fun5;
97  if(interface()->scanf(STX "00F205%d", &fun5) != 1)
98  throw XInterface::XConvError(__FILE__, __LINE__);
99 
100  interface()->query(STX "00P1");
101  int x;
102  if(interface()->scanf(STX "00P2%d", &x) != 1)
103  throw XInterface::XConvError(__FILE__, __LINE__);
104  double z;
105  switch(fun5) {
106  case 00:
107  case 01:
108  z = x / pow(10.0, fun2 % 10);
109  break;
110  case 10:
111  case 11:
112  case 12:
113  z = (x / 100) + ((x > 0) ? 1 : -1) * (abs(x) % 100) / 60.0;
114  break;
115  case 16:
116  case 17:
117  case 18:
118  case 19:
119  z = (x / 10000) + ((x > 0) ? 1 : -1) * ((abs(x) % 10000) / 100 + (abs(x) % 100) / 60.0) / 60.0;
120  break;
121  case 13:
122  case 14:
123  case 15:
124  z = x / 10000.0;
125  break;
126  case 50:
127  z = x / 10;
128  break;
129  default:
130  throw XInterface::XConvError(__FILE__, __LINE__);
131  }
132  return z;
133 }

Generated for KAME4 by  doxygen 1.8.3