YARP
Yet Another Robot Platform
ServerSerial.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3  * SPDX-FileCopyrightText: 2006 Alexandre Bernardino
4  * SPDX-FileCopyrightText: 2006 Carlos Beltran-Gonzalez
5  * SPDX-License-Identifier: BSD-3-Clause
6  */
7 
8 #ifndef YARP_DEV_SERVERSERIAL_H
9 #define YARP_DEV_SERVERSERIAL_H
10 
11 #include <cstdio>
12 #include <cstdlib>
13 
14 #include <yarp/os/BufferedPort.h>
15 #include <yarp/dev/PolyDriver.h>
16 #include <yarp/dev/ISerialDevice.h>
17 #include <yarp/os/Time.h>
18 #include <yarp/os/Network.h>
19 #include <yarp/os/Thread.h>
20 #include <yarp/os/Vocab.h>
21 #include <yarp/os/Bottle.h>
22 
23 
24 using namespace yarp::os;
25 using namespace yarp::sig;
26 using namespace yarp::dev;
27 
28 
29 class ServerSerial;
30 
35  public yarp::os::TypedReaderCallback<yarp::os::Bottle>
36 {
37 protected:
38  yarp::dev::ISerialDevice* ser{nullptr};
39 
40 public:
47 
53  void onRead(Bottle& b) override;
54 };
55 
56 
68 class ServerSerial :
71  private yarp::os::Thread
72 {
73 private:
74  bool verb{false};
76  yarp::os::Port toDevice;
77  yarp::os::Port fromDevice;
78 
81 
82  yarp::dev::ISerialDevice *serial{nullptr};
83  ImplementCallbackHelper2 callback_impl{this};
84 
85 
86  bool closeMain()
87  {
88  if (Thread::isRunning()) {
89  Thread::stop();
90  }
91  //close the port connection here
92  toDevice.close();
93  fromDevice.close();
94  poly.close();
95  return true;
96  }
97 
98 public:
99  ServerSerial() = default;
100  ServerSerial(const ServerSerial&) = delete;
104  ~ServerSerial() override;
105 
106  bool send(const Bottle& msg) override;
107  bool send(char *msg, size_t size) override;
108  bool receive(Bottle& msg) override;
109  int receiveChar(char& c) override;
110  int flush () override;
111  int receiveLine(char* line, const int MaxLineLength) override;
112  int receiveBytes(unsigned char* bytes, const int size) override;
113  bool setDTR(bool enable) override;
114 
119  virtual bool open();
120 
125  bool close() override;
126 
137  bool open(Searchable& prop) override;
138 
142  void run() override;
143 };
144 
145 #endif // YARP_DEV_SERVERSERIAL_H
Callback implementation after buffered input.
Definition: ServerSerial.h:36
ImplementCallbackHelper2()
Constructor.
serial: Export a serial sensor.
Definition: ServerSerial.h:72
ServerSerial()=default
ServerSerial & operator=(ServerSerial &&)=delete
ServerSerial(ServerSerial &&)=delete
ServerSerial & operator=(const ServerSerial &)=delete
ServerSerial(const ServerSerial &)=delete
Interface implemented by all device drivers.
Definition: DeviceDriver.h:35
A generic interface to serial port devices.
Definition: ISerialDevice.h:25
A container for a device driver.
Definition: PolyDriver.h:24
bool close() override
Close the DeviceDriver.
Definition: PolyDriver.cpp:173
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:74
A mini-server for network communication.
Definition: Port.h:47
void close() override
Stop port activity.
Definition: Port.cpp:354
A base class for nested structures that can be searched.
Definition: Searchable.h:66
An abstraction for a thread of execution.
Definition: Thread.h:22
bool stop()
Stop the thread.
Definition: Thread.cpp:81
bool isRunning()
Returns true if the thread is running (Thread::start has been called successfully and the thread has ...
Definition: Thread.cpp:105
A callback for typed data from a port.
An interface for the device drivers.
An interface to the operating system, including Port based communication.
Signal processing.
Definition: Image.h:22