serial_port.h
Go to the documentation of this file.
1 /*
2  * Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifndef HAND_BRIDGE_SERIAL_PORT_H_
19 #define HAND_BRIDGE_SERIAL_PORT_H_
20 
21 #include "pigpio.h"
22 
23 #include <boost/lexical_cast.hpp>
24 #include <boost/chrono.hpp>
25 #include <boost/thread.hpp>
26 
27 class SerialPort {
28  int ser_port;
29 public:
30  bool isOpen() const{
31  return ser_port >= 0 && serDataAvailable(ser_port) >= 0;
32  }
33  bool init(char * param)
34  {
35  unsigned int baud = 57600;
36  if(char * delim = strchr(param, '@')){
37  *delim = 0;
38  baud = boost::lexical_cast<unsigned int>(delim + 1);
39  }
40  ser_port = serOpen(param, baud, 0);
41  printf("%s opened as %d\n", param, ser_port);
42  return isOpen();
43  }
44 
45  template<typename Duration> int waitData(const Duration& duration){
46  boost::chrono::steady_clock::time_point deadline = boost::chrono::steady_clock::now() + duration;
47  int len;
48  while((len = serDataAvailable(ser_port)) == 0 && boost::chrono::steady_clock::now() < deadline)
49  boost::this_thread::sleep_for(boost::chrono::milliseconds(1));
50  return len;
51  }
52 
53  int readByte(){
54  return serReadByte(ser_port);
55  }
56  int read(char *buf, unsigned count){
57  return serRead(ser_port, buf, count);
58  }
59  bool write(uint8_t* data, int length)
60  {
61  return serWrite(ser_port, reinterpret_cast<char*>(data), length) == 0;
62  }
63  bool write(const std::string &line){
64  return serWrite(ser_port, const_cast<char *>(line.c_str()), line.size()) == 0;
65  }
66  void close(){
67  if(isOpen()) serClose(ser_port);
68  }
69  SerialPort() : ser_port(-1) {}
71  close();
72  }
73 };
74 
75 
76 #endif // HAND_BRIDGE_SERIAL_PORT_H_
bool write(uint8_t *data, int length)
Definition: serial_port.h:59
int read(char *buf, unsigned count)
Definition: serial_port.h:56
bool init(char *param)
Definition: serial_port.h:33
int serRead(unsigned handle, char *buf, unsigned count)
Definition: pigpio.c:5000
void close()
Definition: serial_port.h:66
int ser_port
Definition: serial_port.h:28
int waitData(const Duration &duration)
Definition: serial_port.h:45
int serDataAvailable(unsigned handle)
Definition: pigpio.c:5033
int serClose(unsigned handle)
Definition: pigpio.c:4889
bool write(const std::string &line)
Definition: serial_port.h:63
int serWrite(unsigned handle, char *buf, unsigned count)
Definition: pigpio.c:4964
int readByte()
Definition: serial_port.h:53
bool isOpen() const
Definition: serial_port.h:30
int serReadByte(unsigned handle)
Definition: pigpio.c:4934
int serOpen(char *tty, unsigned serBaud, unsigned serFlags)
Definition: pigpio.c:4800
baud


cob_hand_bridge
Author(s): Mathias Lüdtke
autogenerated on Tue Oct 20 2020 03:35:57