YARP
Yet Another Robot Platform
ServerSerial.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2020 Istituto Italiano di Tecnologia (IIT)
3  * Copyright (C) 2006 Alexandre Bernardino
4  * Copyright (C) 2006 Carlos Beltran-Gonzalez
5  * All rights reserved.
6  *
7  * This software may be modified and distributed under the terms of the
8  * BSD-3-Clause license. See the accompanying LICENSE file for details.
9  */
10 
11 #ifndef YARP_DEV_SERVERSERIAL_H
12 #define YARP_DEV_SERVERSERIAL_H
13 
14 #include <cstdio>
15 #include <cstdlib>
16 
17 #include <yarp/os/BufferedPort.h>
18 #include <yarp/dev/PolyDriver.h>
19 #include <yarp/dev/ISerialDevice.h>
20 #include <yarp/os/Time.h>
21 #include <yarp/os/Network.h>
22 #include <yarp/os/Thread.h>
23 #include <yarp/os/Vocab.h>
24 #include <yarp/os/Bottle.h>
25 
26 
27 using namespace yarp::os;
28 using namespace yarp::sig;
29 using namespace yarp::dev;
30 
31 
32 class ServerSerial;
33 
38  public yarp::os::TypedReaderCallback<yarp::os::Bottle>
39 {
40 protected:
41  yarp::dev::ISerialDevice* ser{nullptr};
42 
43 public:
50 
56  void onRead(Bottle& b) override;
57 };
58 
59 
70 class ServerSerial :
73  private yarp::os::Thread
74 {
75 private:
76  bool verb{false};
78  yarp::os::Port toDevice;
79  yarp::os::Port fromDevice;
80 
83 
84  yarp::dev::ISerialDevice *serial{nullptr};
85  ImplementCallbackHelper2 callback_impl{this};
86 
87 
88  bool closeMain()
89  {
90  if (Thread::isRunning()) {
91  Thread::stop();
92  }
93  //close the port connection here
94  toDevice.close();
95  fromDevice.close();
96  poly.close();
97  return true;
98  }
99 
100 public:
101  ServerSerial() = default;
102  ServerSerial(const ServerSerial&) = delete;
106  ~ServerSerial() override;
107 
108  bool send(const Bottle& msg) override;
109  bool send(char *msg, size_t size) override;
110  bool receive(Bottle& msg) override;
111  int receiveChar(char& c) override;
112  int flush () override;
113  int receiveLine(char* line, const int MaxLineLength) override;
114  int receiveBytes(unsigned char* bytes, const int size) override;
115  bool setDTR(bool enable) override;
116 
121  virtual bool open();
122 
127  bool close() override;
128 
139  bool open(Searchable& prop) override;
140 
144  void run() override;
145 };
146 
147 #endif // YARP_DEV_SERVERSERIAL_H
Callback implementation after buffered input.
Definition: ServerSerial.h:39
ImplementCallbackHelper2()
Constructor.
Export a serial sensor.
Definition: ServerSerial.h:74
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:38
A generic interface to serial port devices.
Definition: ISerialDevice.h:28
A container for a device driver.
Definition: PolyDriver.h:27
bool close() override
Close the DeviceDriver.
Definition: PolyDriver.cpp:176
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
A mini-server for network communication.
Definition: Port.h:50
void close() override
Stop port activity.
Definition: Port.cpp:357
A base class for nested structures that can be searched.
Definition: Searchable.h:69
An abstraction for a thread of execution.
Definition: Thread.h:25
bool stop()
Stop the thread.
Definition: Thread.cpp:84
bool isRunning()
Returns true if the thread is running (Thread::start has been called successfully and the thread has ...
Definition: Thread.cpp:108
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:25