SERIAL++ library: simple serial port access
portread.cc
Go to the documentation of this file.
1 
36 #define PORTREAD_VERSION \
37  "PORTREAD V1.0 read from port"
38 #define PORTREAD_CVSID \
39  "$Id$"
40 
41 #include <iostream>
42 #include <string>
43 #include <tfxx/commandline.h>
44 #include <serialxx/serialport.h>
45 
46 using std::cout;
47 using std::cerr;
48 using std::endl;
49 
50 struct Options {
51  bool verbose;
52  std::string device, delimiter;
53 };
54 
55 int main(int iargc, char* argv[])
56 {
57 
58  // define usage information
59  char usage_text[]=
60  {
61  PORTREAD_VERSION "\n"
62  "usage: portread [-p device] [-v] [-d delimiter]" "\n"
63  " or: portread --help|-h" "\n"
64  };
65 
66  // define full help text
67  char help_text[]=
68  {
70  };
71 
72  // define commandline options
73  using namespace tfxx::cmdline;
74  static Declare options[]=
75  {
76  // 0: print help
77  {"help",arg_no,"-"},
78  // 1: verbose mode
79  {"v",arg_no,"-"},
80  // 2: device name
81  {"p",arg_yes,"/dev/ttyS0"},
82  // 3: delimiter name
83  {"d",arg_yes,"\r"},
84  {NULL}
85  };
86 
87  // no arguments? print usage...
88  if (iargc<1)
89  {
90  cerr << usage_text << endl;
91  exit(0);
92  }
93 
94  // collect options from commandline
95  Commandline cmdline(iargc, argv, options);
96 
97  // help requested? print full help text...
98  if (cmdline.optset(0))
99  {
100  cerr << usage_text << endl;
101  cerr << help_text << endl;
102  exit(0);
103  }
104 
105  /*
106  // dummy operation: print option settings
107  for (int iopt=0; iopt<2; iopt++)
108  {
109  cout << "option: '" << options[iopt].opt_string << "'" << endl;
110  if (cmdline.optset(iopt)) { cout << " option was set"; }
111  else { cout << "option was not set"; }
112  cout << endl;
113  cout << " argument (string): '" << cmdline.string_arg(iopt) << "'" << endl;
114  cout << " argument (int): '" << cmdline.int_arg(iopt) << "'" << endl;
115  cout << " argument (long): '" << cmdline.long_arg(iopt) << "'" << endl;
116  cout << " argument (float): '" << cmdline.float_arg(iopt) << "'" << endl;
117  cout << " argument (double): '" << cmdline.double_arg(iopt) << "'" << endl;
118  cout << " argument (bool): '";
119  if (cmdline.bool_arg(iopt))
120  { cout << "true"; } else { cout << "false"; }
121  cout << "'" << endl;
122  }
123  while (cmdline.extra()) { cout << cmdline.next() << endl; }
124 
125  // dummy operation: print rest of command line
126  while (cmdline.extra()) { cout << cmdline.next() << endl; }
127  */
128 
129  Options opt;
130 
131  opt.verbose=cmdline.optset(1);
132  opt.device=cmdline.string_arg(2);
133  opt.delimiter=cmdline.string_arg(3);
134 
135  serialport::SerialPort port(opt.device);
136 // port.debug(true);
137  std::string text=port.read(opt.delimiter);
138  cout << text << endl;
139 }
140 
141 /* ----- END OF portread.cc ----- */
std::string read(const std::string &delim="\) const
read from port until delimiter
Definition: serialport.cc:83
std::string delimiter
Definition: portread.cc:52
code to access a serial port (prototypes)
#define PORTREAD_CVSID
Definition: portread.cc:38
bool verbose
Definition: portread.cc:51
int main(int iargc, char *argv[])
Definition: portread.cc:55
std::string device
Definition: portread.cc:52
#define PORTREAD_VERSION
Definition: portread.cc:36