Visual Servoing Platform version 3.5.0
parallelPort.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2019 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 http://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 keybord management.
33 *
34 * Author:
35 * Fabien Spindler
36 *
37 *****************************************************************************/
38
45#include <visp3/core/vpConfig.h>
46#include <visp3/core/vpDebug.h>
47
48#if defined VISP_HAVE_PARPORT
49#include <iostream>
50#include <signal.h>
51#include <stdio.h>
52#include <stdlib.h>
53
54#include <visp3/io/vpParallelPort.h>
55#include <visp3/io/vpParseArgv.h>
56
57// List of allowed command line options
58#define GETOPTARGS "d:h"
59
69void usage(const char *name, const char *badparam, unsigned char &data)
70{
71 fprintf(stdout, "\n\
72Send a data to the parallel port.\n\
73\n\
74SYNOPSIS\n\
75 %s [-d <data>] [-h]\n\
76", name);
77
78 fprintf(stdout, "\n\
79OPTIONS: Default\n\
80 -d <data> %d\n\
81 Data to send to the parallel port.\n\
82 Value should be in [0:255].\n\
83\n\
84 -h\n\
85 Print the help.\n\n", data);
86
87 if (badparam) {
88 fprintf(stderr, "ERROR: \n");
89 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
90 }
91}
92
104bool getOptions(int argc, const char **argv, unsigned char &data)
105{
106 const char *optarg;
107 int c;
108
109 int value;
110 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg)) > 1) {
111
112 switch (c) {
113 case 'd': {
114 value = atoi(optarg);
115 if ((value < 0) || (value > 255)) {
116 usage(argv[0], optarg, data);
117 std::cerr << "ERROR: " << std::endl;
118 std::cerr << " Bad value \"-d " << optarg << "\"" << std::endl << std::endl;
119 return false;
120 } else {
121 data = (unsigned char)value;
122 }
123 break;
124 }
125 case 'h':
126 usage(argv[0], NULL, 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], NULL, 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
153int 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 exit(-1);
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 } catch (vpParallelPortException &e) {
170 switch (e.getCode()) {
172 printf("Can't open the parallel port\n");
173 break;
175 printf("Can't close the parallel port\n");
176 break;
177 }
178 } catch (const vpException &e) {
179 std::cout << "An error occurs: " << e.getMessage() << std::endl;
180 }
181 return EXIT_SUCCESS;
182}
183#else
184int main()
185{
186 std::cout << "vpParallelPort class works only on unix..." << std::endl;
187 return EXIT_SUCCESS;
188}
189#endif
error that can be emited by ViSP classes.
Definition: vpException.h:72
int getCode() const
Send the object code.
Definition: vpException.cpp:94
const char * getMessage() const
Definition: vpException.cpp:90
Error that can be emited 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