DL1direct.cc 5.51 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: DL1direct.cc,v 1.6 2008-12-01 19:46:07 tforb Exp $
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
33
34
35
36
37
38
 * \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
 * 
 * ============================================================================
 */
#define DL1DIRECT_VERSION \
  "DL1DIRECT   V1.0   directly send command to DL1 and receive the response"
#define DL1DIRECT_CVSID \
thomas.forbriger's avatar
thomas.forbriger committed
39
  "$Id: DL1direct.cc,v 1.6 2008-12-01 19:46:07 tforb Exp $"
thomas.forbriger's avatar
thomas.forbriger committed
40
41

#include <iostream>
thomas.forbriger's avatar
thomas.forbriger committed
42
#include <string>
thomas.forbriger's avatar
thomas.forbriger committed
43
#include <tfxx/commandline.h>
thomas.forbriger's avatar
thomas.forbriger committed
44
#include <serialxx/serialport.h>
45
#include "thiesdl1.h"
46
#include "error.h"
thomas.forbriger's avatar
thomas.forbriger committed
47
48
49
50
51

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

thomas.forbriger's avatar
thomas.forbriger committed
52
struct Options {
53
54
  bool verbose, debug;
  std::string device;
thomas.forbriger's avatar
thomas.forbriger committed
55
56
};

thomas.forbriger's avatar
thomas.forbriger committed
57
58
59
60
61
62
63
int main(int iargc, char* argv[])
{

  // define usage information
  char usage_text[]=
  {
    DL1DIRECT_VERSION "\n"
64
    "usage: DL1direct [-v] [-D] [-d device] command [argument]" "\n"
thomas.forbriger's avatar
thomas.forbriger committed
65
66
67
68
69
70
71
    "   or: DL1direct --help|-h" "\n"
  };

  // define full help text
  char help_text[]=
  {
    DL1DIRECT_CVSID
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
    "\n"
    "This program submits exactly one command to the DL1 data logger" "\n"
    "and presents the response on standard output." "\n"
    "\n"
    "-v     verbose mode" "\n"
    "-D     debug mode" "\n"
    "-d dev device file dev" "\n"
    "\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
90
    "SCD    set current date and time" "\n"
91
92
    "ts d   request data of day d" "\n"
    "ds d   request data since date d" "\n"
thomas.forbriger's avatar
thomas.forbriger committed
93
94
95
96
97
98
99
100
101
102
  };

  // 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
103
    // 2: device file name
104
105
106
    {"d",arg_yes,"/dev/ttyS0"},
    // 3: debug mode
    {"D",arg_no,"-"},
thomas.forbriger's avatar
thomas.forbriger committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
    {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
128
129
130
  Options opt;
  opt.verbose=cmdline.optset(1);
  opt.device=cmdline.string_arg(2);
131
  opt.debug=cmdline.optset(3);
thomas.forbriger's avatar
thomas.forbriger committed
132
133

  if (opt.verbose) { cout << "open port " << opt.device << endl; }
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
  dl1::DL1 port(opt.device, opt.debug);

  int timeout=2;

  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
    timeout=20;
    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
190

191
  while (true) { cout << port.getline(timeout) << endl; }
thomas.forbriger's avatar
thomas.forbriger committed
192
193
194
}

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