SERIAL++ library: simple serial port access
portwrite.cc
Go to the documentation of this file.
1 
36 #define PORTWRITE_VERSION \
37  "PORTWRITE V1.0 write to port"
38 #define PORTWRITE_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;
53 };
54 
55 int main(int iargc, char* argv[])
56 {
57 
58  // define usage information
59  char usage_text[]=
60  {
62  "usage: portwrite [-p device] [-v] string" "\n"
63  " or: portwrite --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  {NULL}
83  };
84 
85  // no arguments? print usage...
86  if (iargc<2)
87  {
88  cerr << usage_text << endl;
89  exit(0);
90  }
91 
92  // collect options from commandline
93  Commandline cmdline(iargc, argv, options);
94 
95  // help requested? print full help text...
96  if (cmdline.optset(0))
97  {
98  cerr << usage_text << endl;
99  cerr << help_text << endl;
100  exit(0);
101  }
102 
103  /*
104  // dummy operation: print option settings
105  for (int iopt=0; iopt<2; iopt++)
106  {
107  cout << "option: '" << options[iopt].opt_string << "'" << endl;
108  if (cmdline.optset(iopt)) { cout << " option was set"; }
109  else { cout << "option was not set"; }
110  cout << endl;
111  cout << " argument (string): '" << cmdline.string_arg(iopt) << "'" << endl;
112  cout << " argument (int): '" << cmdline.int_arg(iopt) << "'" << endl;
113  cout << " argument (long): '" << cmdline.long_arg(iopt) << "'" << endl;
114  cout << " argument (float): '" << cmdline.float_arg(iopt) << "'" << endl;
115  cout << " argument (double): '" << cmdline.double_arg(iopt) << "'" << endl;
116  cout << " argument (bool): '";
117  if (cmdline.bool_arg(iopt))
118  { cout << "true"; } else { cout << "false"; }
119  cout << "'" << endl;
120  }
121  while (cmdline.extra()) { cout << cmdline.next() << endl; }
122 
123  // dummy operation: print rest of command line
124  while (cmdline.extra()) { cout << cmdline.next() << endl; }
125  */
126 
127  Options opt;
128 
129  opt.verbose=cmdline.optset(1);
130  opt.device=cmdline.string_arg(2);
131 
132  serialport::SerialPort port(opt.device);
133  while (cmdline.extra())
134  {
135  std::string text=cmdline.next();
136  if (opt.verbose) { cout << text << endl; }
137  port.write(text);
138  port.write(" ");
139  }
140 }
141 
142 /* ----- END OF portwrite.cc ----- */
#define PORTWRITE_VERSION
Definition: portwrite.cc:36
#define PORTWRITE_CVSID
Definition: portwrite.cc:38
code to access a serial port (prototypes)
void write(const std::string &text) const
write string to port
Definition: serialport.cc:73
bool verbose
Definition: portread.cc:51
std::string device
Definition: portread.cc:52
int main(int iargc, char *argv[])
Definition: portwrite.cc:55