Changeset 14315
- Timestamp:
- 08/06/12 21:08:30 (12 years ago)
- Location:
- trunk/FACT++/src
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/FACT++/src/HeadersAgilent.h
r14292 r14315 10 10 kDisconnected = 1, 11 11 kConnected = 2, 12 kVoltage_On = 3, 13 kVoltage_Off = 4, 12 14 }; 13 15 } -
trunk/FACT++/src/agilentctrl.cc
r14314 r14315 17 17 namespace ba = boost::asio; 18 18 namespace bs = boost::system; 19 namespace dummy= ba::placeholders;19 namespace bapla = ba::placeholders; 20 20 21 21 using namespace std; … … 27 27 { 28 28 boost::asio::streambuf fBuffer; 29 30 29 bool fIsVerbose; 31 30 bool fDump; 32 33 int fLineCounter;34 35 36 31 ofstream fDumpStream; 32 int fState; 33 37 34 38 35 protected: … … 59 56 fDumpStream << str << endl; 60 57 } 61 58 59 boost::asio::deadline_timer fCheckStatusTimer; 60 61 void PostStatusRequest() 62 { 63 PostMessage(string("*IDN?\n")); 64 PostMessage(string("meas:volt?\n")); 65 PostMessage(string("meas:curr?\n")); 66 } 67 68 69 void RequestStatus() 70 { 71 PostStatusRequest(); 72 73 fCheckStatusTimer.expires_from_now(boost::posix_time::seconds(60)); 74 fCheckStatusTimer.async_wait(boost::bind(&ConnectionWeather::HandleRequest, 75 this, bapla::error)); 76 } 77 78 void HandleRequest(const bs::error_code &error) 79 { 80 // 125: Operation canceled (bs::error_code(125, bs::system_category)) 81 if (error && error!=ba::error::basic_errors::operation_aborted) 82 { 83 ostringstream str; 84 str << "Write timeout of " << URL() << ": " << error.message() << " (" << error << ")";// << endl; 85 Error(str); 86 87 PostClose(false); 88 return; 89 } 90 91 if (!is_open()) 92 { 93 // For example: Here we could schedule a new accept if we 94 // would not want to allow two connections at the same time. 95 PostClose(true); 96 return; 97 } 98 99 // Check whether the deadline has passed. We compare the deadline 100 // against the current time since a new asynchronous operation 101 // may have moved the deadline before this actor had a chance 102 // to run. 103 if (fKeepAlive.expires_at() > ba::deadline_timer::traits_type::now()) 104 return; 105 106 RequestStatus(); 107 } 62 108 private: 63 109 64 65 void HandleReceivedData(const bs::error_code& err, size_t bytes_received, int /*type*/) 66 { 110 int fLineCounter; 111 112 void ReceivedStatusHandler(const bs::error_code& err, size_t bytes_received, int /*type*/) 113 { 114 float measured_voltage; 115 float measured_current; 116 67 117 // Do not schedule a new read if the connection failed. 68 118 if (bytes_received==0 || err) … … 87 137 if (fIsVerbose) 88 138 Out() << kBold << "Received (" << bytes_received << " bytes):" << endl; 89 90 139 if (fDump) 91 140 { … … 113 162 { 114 163 // this should be a float containing the measured voltage 115 float f; 116 is >> f; 117 data.push_back(f); 118 Out() << "voltage: " << f << endl; 164 is >> measured_voltage; 165 data.push_back(measured_voltage); 166 Out() << "voltage: " << measured_voltage << endl; 119 167 } 120 168 else if (fLineCounter >= 3) 121 169 { 122 170 // this should be a float containing the measured voltage 123 float f; 124 is >> f; 125 data.push_back(f); 126 Out() << "current: " << f << endl; 171 is >> measured_current; 172 data.push_back(measured_current); 173 Out() << "current: " << measured_current << endl; 127 174 fLineCounter = 0; 128 175 fSendRequest = true; 129 176 } 130 131 132 // int status=-1; 133 134 string buffer; 135 while (getline(is, buffer, '\n')) 136 { 137 if (fIsVerbose) 138 Out() << buffer << endl; 139 if (fDump) 140 Dump(buffer); 141 142 buffer = Tools::Trim(buffer); 143 144 if (buffer.empty()) 145 continue; 146 147 /* 148 istringstream in(buffer); 149 while (1) 150 { 151 float f; 152 in >> f; 153 if (!in) 154 break; 155 156 resist.push_back(f); 157 } 158 */ 159 } 160 177 178 if (measured_voltage > 1.0) 179 { 180 fState = State::kVoltage_On; 181 } 182 else 183 { 184 fState = State::kVoltage_Off; 185 } 161 186 162 187 UpdateDim(data); 163 164 StartRead(); 165 } 166 167 void StartRead() 168 { 188 189 Start_async_read_until(); 190 } 191 192 void Start_async_read_until() 193 { 194 // Bind Received Data Handler to the event: "received <enter> character" 195 // this Handler will try to parse the incoming data 169 196 ba::async_read_until(*this, fBuffer, "\n", 170 boost::bind(&ConnectionAgilent:: HandleReceivedData, this,171 dummy::error, dummy::bytes_transferred, 0));197 boost::bind(&ConnectionAgilent::ReceivedStatusHandler, this, 198 bapla::error, bapla::bytes_transferred, 0)); 172 199 173 200 // FIXME: Add timeout here … … 177 204 void ConnectionEstablished() 178 205 { 206 fState = State::kConnected; 207 Start_async_read_until(); 208 PostStatusRequest(); 209 179 210 fLineCounter = 0; 180 fSendRequest = true;181 211 fBuffer.prepare(1000); 182 fTime = Time();183 StartRead();184 212 } 185 213 186 214 public: 187 Time fTime;188 bool fSendRequest;189 215 190 216 ConnectionAgilent(ba::io_service& ioservice, MessageImp &imp) : Connection(ioservice, imp()), 191 217 fIsVerbose(true), 218 fCheckStatusTimer(ioservice), 192 219 fDump(true) 193 220 { 194 221 SetLogStream(&imp); 222 fState = State::kDisconnected; 195 223 } 196 224 … … 216 244 } 217 245 } 218 219 void Identify()220 { 221 PostMessage(string("*IDN?\n"));222 PostMessage(string("meas:volt?\n"));223 PostMessage(string("meas:curr?\n")); 224 } 246 247 int GetState() const 248 { 249 return fState; 250 } 251 252 225 253 }; 226 254 … … 305 333 // synchronously, i.e. within the call to poll_one() 306 334 poll_one(); 307 if (fAgilent.fSendRequest) 308 { 309 if (Time()-fAgilent.fTime > boost::posix_time::seconds(5)) 310 { 311 fAgilent.Identify(); 312 fAgilent.fTime = Time(); 313 fAgilent.fSendRequest = false; 314 } 315 } 316 return fAgilent.IsConnected() ? State::kConnected : State::kDisconnected; 335 336 if ( fAgilent.IsConnected() ) 337 return fWeather.GetState(); 338 else 339 return State::kDisconnected; 317 340 } 318 341 … … 382 405 T::AddStateName(State::kConnected, "Connected", 383 406 "Ethernet connection to Agilent established."); 407 408 T::AddStateName(State::kVoltage_On, "Voltage_On", 409 "The measured output voltage is higher than 1.0V"); 410 411 T::AddStateName(State::kVoltage_Off, "Voltage_Off", 412 "The measured output voltage is lower than 1.0V"); 384 413 385 414 // Verbosity commands
Note:
See TracChangeset
for help on using the changeset viewer.