Visual Servoing Platform  version 3.0.0
parallelPort.cpp
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See http://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Example of keybord management.
32  *
33  * Author:
34  * Fabien Spindler
35  *
36  *****************************************************************************/
37 
44 #include <visp3/core/vpConfig.h>
45 #include <visp3/core/vpDebug.h>
46 
47 #if defined VISP_HAVE_PARPORT
48 #include <stdlib.h>
49 #include <stdio.h>
50 #include <iostream>
51 #include <signal.h>
52 
53 #include <visp3/io/vpParallelPort.h>
54 #include <visp3/io/vpParseArgv.h>
55 
56 // List of allowed command line options
57 #define GETOPTARGS "d:h"
58 
68 void usage(const char *name, const char *badparam, unsigned char &data)
69 {
70  fprintf(stdout, "\n\
71 Send a data to the parallel port.\n\
72 \n\
73 SYNOPSIS\n\
74  %s [-d <data>] [-h]\n\
75 ", name);
76 
77  fprintf(stdout, "\n\
78 OPTIONS: Default\n\
79  -d <data> %d\n\
80  Data to send to the parallel port.\n\
81  Value should be in [0:255].\n\
82 \n\
83  -h\n\
84  Print the help.\n\n",
85  data);
86 
87  if (badparam) {
88  fprintf(stderr, "ERROR: \n" );
89  fprintf(stderr, "\nBad parameter [%s]\n", badparam);
90  }
91 
92 }
93 
105 bool getOptions(int argc, const char **argv, unsigned char &data)
106 {
107  const char *optarg;
108  int c;
109 
110  int value;
111  while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
112 
113  switch (c) {
114  case 'd': {
115  value = atoi(optarg);
116  if ((value < 0) || (value > 255)) {
117  usage(argv[0], optarg, data);
118  std::cerr << "ERROR: " << std::endl;
119  std::cerr << " Bad value \"-d " << optarg << "\""
120  << std::endl << std::endl;
121  return false;
122  }
123  else {
124  data = (unsigned char) value;
125  }
126  break;
127  }
128  case 'h': usage(argv[0], NULL, data); return false; break;
129 
130  default:
131  usage(argv[0], optarg, data); return false; break;
132  }
133  }
134 
135  if ((c == 1) || (c == -1)) {
136  // standalone param or error
137  usage(argv[0], NULL, data);
138  std::cerr << "ERROR: " << std::endl;
139  std::cerr << " Bad argument " << optarg << std::endl << std::endl;
140  return false;
141  }
142 
143  return true;
144 }
145 
151 int
152 main(int argc, const char **argv)
153 {
154  // data to send to the parallel port
155  unsigned char data = 0;
156 
157  // Read the command line options
158  if (getOptions(argc, argv, data) == false) {
159  exit (-1);
160  }
161  try {
162 
163  vpParallelPort parport;
164 
165  printf("Send data \"%d\" to the parallel port\n", data);
166  parport.sendData(data);
167 
168 
169  }
170  catch (vpParallelPortException e) {
171  switch(e.getCode()) {
173  printf("Can't open the parallel port\n");
174  break;
176  printf("Can't close the parallel port\n");
177  break;
178  }
179  }
180  catch(...) {
181  printf("An error occurs...\n");
182  }
183  return 0;
184 }
185 #else
186 int
187 main()
188 {
189  vpTRACE("Sorry, for the moment, vpParallelPort class works only on unix...");
190  return 0;
191 }
192 #endif
int getCode(void)
send the object code
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
Definition: vpParseArgv.cpp:76
#define vpTRACE
Definition: vpDebug.h:414
void sendData(unsigned char &data)
Error that can be emited by the vpParallelPort class and its derivates.
Parallel port management under unix.