DL1direct.cc 6.03 KB
Newer Older
thomas.forbriger's avatar
thomas.forbriger committed
1
2
3
4
5
/*! \file DL1direct.cc
 * \brief directly send command to DL1 and receive the response
 * 
 * ----------------------------------------------------------------------------
 * 
thomas.forbriger's avatar
thomas.forbriger committed
6
 * $Id$
thomas.forbriger's avatar
thomas.forbriger committed
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
 * \author Thomas Forbriger
 * \date 24/11/2008
 * 
 * directly send command to DL1 and receive the response
 * 
 * Copyright (c) 2008 by Thomas Forbriger (BFO Schiltach) 
 *
 * ----
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * ----
 *
 * 
 * REVISIONS and CHANGES 
 *  - 24/11/2008   V1.0   Thomas Forbriger
33
 *  - 16/01/2009   V1.1   add timeout commandline parameter
thomas.forbriger's avatar
thomas.forbriger committed
34
35
36
 * 
 * ============================================================================
 */
thomas.forbriger's avatar
thomas.forbriger committed
37
38
39
40
41
42
/*! \page DL1direct The program DL1direct
 *
 * This program supports interactive control of the DL1 logger.
 * You can use it to send commands and read data to standard output.
 * Execute \code DL1direct -help\endcode to obtain a program description.
 */
thomas.forbriger's avatar
thomas.forbriger committed
43
#define DL1DIRECT_VERSION \
44
  "DL1DIRECT   V1.1   directly send command to DL1 and receive the response"
thomas.forbriger's avatar
thomas.forbriger committed
45
#define DL1DIRECT_CVSID \
thomas.forbriger's avatar
thomas.forbriger committed
46
  "$Id$"
thomas.forbriger's avatar
thomas.forbriger committed
47
48

#include <iostream>
thomas.forbriger's avatar
thomas.forbriger committed
49
#include <string>
thomas.forbriger's avatar
thomas.forbriger committed
50
#include <tfxx/commandline.h>
thomas.forbriger's avatar
thomas.forbriger committed
51
#include <serialxx/serialport.h>
52
#include "thiesdl1.h"
53
#include "error.h"
thomas.forbriger's avatar
thomas.forbriger committed
54
55
56
57
58

using std::cout;
using std::cerr;
using std::endl;

thomas.forbriger's avatar
thomas.forbriger committed
59
struct Options {
60
61
  bool verbose, debug;
  std::string device;
62
  int timeout;
thomas.forbriger's avatar
thomas.forbriger committed
63
64
};

thomas.forbriger's avatar
thomas.forbriger committed
65
66
67
68
69
70
71
int main(int iargc, char* argv[])
{

  // define usage information
  char usage_text[]=
  {
    DL1DIRECT_VERSION "\n"
72
    "usage: DL1direct [-v] [-D] [-d device] command [argument]" "\n"
73
    "                 [-timeout d]" "\n"
thomas.forbriger's avatar
thomas.forbriger committed
74
75
76
77
78
79
80
    "   or: DL1direct --help|-h" "\n"
  };

  // define full help text
  char help_text[]=
  {
    DL1DIRECT_CVSID
81
82
83
84
    "\n"
    "This program submits exactly one command to the DL1 data logger" "\n"
    "and presents the response on standard output." "\n"
    "\n"
85
86
87
88
    "-v           verbose mode" "\n"
    "-D           debug mode" "\n"
    "-d dev       device file dev" "\n"
    "-timeout d   set timeout to \"d\" seconds" "\n"
89
90
91
92
93
94
95
96
97
98
99
    "\n"
    "supported commands are:" "\n"
    "HH     display commands accepted by DL1" "\n"
    "LL     dump status" "\n"
    "PD     power down" "\n"
    "SS     dump entire data buffer" "\n"
    "MM     display current data value" "\n"
    "DD     display date" "\n"
    "ZZ     display time" "\n"
    "XX     display station code" "\n"
    "SD d   set date and time" "\n"
thomas.forbriger's avatar
thomas.forbriger committed
100
    "SCD    set current date and time" "\n"
101
102
    "ts d   request data of day d" "\n"
    "ds d   request data since date d" "\n"
thomas.forbriger's avatar
thomas.forbriger committed
103
104
105
106
107
108
109
110
111
112
  };

  // define commandline options
  using namespace tfxx::cmdline;
  static Declare options[]= 
  {
    // 0: print help
    {"help",arg_no,"-"},
    // 1: verbose mode
    {"v",arg_no,"-"},
thomas.forbriger's avatar
thomas.forbriger committed
113
    // 2: device file name
114
    {"device",arg_yes,"/dev/ttyS0"},
115
116
    // 3: debug mode
    {"D",arg_no,"-"},
117
118
    // 4: debug mode
    {"timeout",arg_yes,"2"},
thomas.forbriger's avatar
thomas.forbriger committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
    {NULL}
  };

  // no arguments? print usage...
  if (iargc<2) 
  {
    cerr << usage_text << endl;
    exit(0);
  }

  // collect options from commandline
  Commandline cmdline(iargc, argv, options);

  // help requested? print full help text...
  if (cmdline.optset(0))
  {
    cerr << usage_text << endl;
    cerr << help_text << endl;
    exit(0);
  }

thomas.forbriger's avatar
thomas.forbriger committed
140
141
142
  Options opt;
  opt.verbose=cmdline.optset(1);
  opt.device=cmdline.string_arg(2);
143
  opt.debug=cmdline.optset(3);
144
  opt.timeout=cmdline.int_arg(4);
thomas.forbriger's avatar
thomas.forbriger committed
145
146

  if (opt.verbose) { cout << "open port " << opt.device << endl; }
147
148
  dl1::DL1 port(opt.device, opt.debug);

149
150
  int timeout=opt.timeout;
  if (opt.verbose) { cout << "timeout: " << timeout << "seconds" << endl; }
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

  DL1_assert(cmdline.extra(), "ERROR: missing command");
  std::string command=cmdline.next();
  if (opt.verbose) { cout << "issue command " << command << endl; }
  if (command == "LL") { port.sendLL(); }
  else if ( command == "HH") { port.sendHH(); }
  else if ( command == "PD") { port.sendPD(); }
  else if ( command == "SS") { port.sendSS(); }
  else if ( command == "MM") { port.sendMM(); }
  else if ( command == "DD") { port.sendDD(); }
  else if ( command == "ZZ") { port.sendZZ(); }
  else if ( command == "XX") { port.sendXX(); }
  else if ( command == "ts") 
  { 
    DL1_assert(cmdline.extra(), "ERROR: missing time string");
    libtime::TAbsoluteTime date(cmdline.next());
    if (opt.verbose) { cout << "request data for day " 
      << date.timestring() << endl; }
    port.sendts(date);
  }
  else if ( command == "ds") 
  { 
    DL1_assert(cmdline.extra(), "ERROR: missing time string");
    libtime::TAbsoluteTime date(cmdline.next());
    if (opt.verbose) { cout << "request all data since " 
      << date.timestring() << endl; }
    port.sendds(date);
    // increase timeout
179
    timeout=opt.timeout*10;
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
    if (opt.verbose) { cout << "increased timeout value to "
      << timeout << " seconds" << endl; }
  }
  else if ( command == "SD") 
  { 
    port.activate();
    DL1_assert(cmdline.extra(), "ERROR: missing time string");
    libtime::TAbsoluteTime date(cmdline.next());
    if (opt.verbose) { cout << "set time to " << date.timestring() << endl; }
    port.settime(date);
    port.sendLL();
  }
  else if ( command == "SCD") 
  { 
    if (opt.verbose) { cout << "set curent time" << endl; }
    libtime::TAbsoluteTime now=libtime::utc();
    cout << "waiting for " << 60-now.second() << " seconds." << endl;
    port.setcurrenttime();
    port.sendLL();
  }
  else
  {
    DL1_abort("ERROR: unknown command");
  }
thomas.forbriger's avatar
thomas.forbriger committed
204

205
  while (true) { cout << port.getline(timeout) << endl; }
thomas.forbriger's avatar
thomas.forbriger committed
206
207
208
}

/* ----- END OF DL1direct.cc ----- */