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