Visual Servoing Platform  version 3.6.1 under development (2024-03-28)
vpParallelPort.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 https://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  * Parallel port management.
32  */
33 
34 #include <visp3/core/vpConfig.h>
35 
36 #ifdef VISP_HAVE_PARPORT
37 
38 #include <fcntl.h>
39 #include <sys/ioctl.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <unistd.h>
43 
44 #include <visp3/io/vpParallelPort.h>
45 
51 static unsigned char vpParallelPortData;
52 
66 vpParallelPort::vpParallelPort() : fd(0), device()
67 {
68  device = "/dev/parport0";
69 
70  this->open();
71 
72  unsigned char data = 0;
73  this->sendData(data);
74 }
75 
82 vpParallelPort::~vpParallelPort() { this->close(); }
83 
93 void vpParallelPort::open()
94 {
95  fd = ::open(device.c_str(), O_WRONLY);
96  if (fd < 0) {
97  printf("Can't open /dev/parport0\n");
98  printf("Check if you have write access to /dev/parport0\n");
99  perror("Open parallel port");
100  throw(vpParallelPortException(vpParallelPortException::opening, "Can't open /dev/parport0"));
101  }
102 
103  int i;
104 
105  ioctl(fd, PPCLAIM);
106  i = PARPORT_MODE_COMPAT;
107  ioctl(fd, PPSETMODE, &i);
108  i = IEEE1284_MODE_COMPAT;
109  ioctl(fd, PPNEGOT, &i);
110 }
111 
126 void vpParallelPort::sendData(unsigned char &data)
127 {
128  ioctl(fd, PPWDATA, &data);
129 
130  // Memorise the last sended data to the static variable
131  vpParallelPortData = data;
132 }
133 
139 unsigned char vpParallelPort::getData() { return vpParallelPortData; }
140 
148 void vpParallelPort::close()
149 {
150  ioctl(fd, PPRELEASE);
151 
152  int err;
153  err = ::close(fd);
154 
155  if (err != 0) {
156  printf("Can't close the parallel port\n");
157  throw(vpParallelPortException(vpParallelPortException::closing, "Can't close the parallel port"));
158  }
159 }
160 
161 #elif !defined(VISP_BUILD_SHARED_LIBS)
162 // Work around to avoid warning: libvisp_core.a(vpParallelPort.cpp.o) has no symbols
163 void dummy_vpParallelPort() { };
164 #endif
Error that can be emitted by the vpParallelPort class and its derivates.
virtual ~vpParallelPort()
unsigned char getData()
void sendData(unsigned char &data)