YARP
Yet Another Robot Platform
ServerSerial.cpp
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 #include "ServerSerial.h"
12 
13 #include <yarp/os/LogComponent.h>
14 #include <yarp/os/Os.h>
15 
16 namespace {
17 YARP_LOG_COMPONENT(SERVERSERIAL, "yarp.devices.ServerSerial")
18 }
19 
21 {
22  closeMain();
23 }
24 
25 bool ServerSerial::send(const Bottle& msg)
26 {
27  if(verb)
28  yCDebug(SERVERSERIAL, "std::string to send : %s", msg.toString().c_str());
29  if(serial != nullptr) {
30  serial->send(msg);
31  return true;
32  }
33  else
34  return false;
35 }
36 
37 bool ServerSerial::send(char *msg, size_t size)
38 {
39  if(verb)
40  yCDebug(SERVERSERIAL, "std::string to send : %s", msg);
41  if(serial != nullptr) {
42  serial->send(msg, size);
43  return true;
44  }
45  else
46  return false;
47 }
48 
50 {
51  if(serial != nullptr) {
52  serial->receive(msg);
53  return true;
54  }
55  else
56  return false;
57 }
58 
60 {
61  if(serial != nullptr) {
62  return serial->receiveChar(c);
63  }
64  else
65  return -1;
66 }
67 
69 {
70  if(serial != nullptr) {
71  return serial->flush();
72  }
73  else
74  return -1;
75 }
76 
77 bool ServerSerial::setDTR(bool enable)
78 {
79  if (serial != nullptr) {
80  return serial->setDTR(enable);
81  }
82  else
83  return false;
84 }
85 
86 int ServerSerial::receiveLine(char* line, const int MaxLineLength)
87 {
88  if(serial != nullptr) {
89  return serial->receiveLine(line, MaxLineLength);
90  }
91  else
92  return -1;
93 }
94 
95 int ServerSerial::receiveBytes(unsigned char* bytes, const int size)
96 {
97  if (serial != nullptr) {
98  return serial->receiveBytes(bytes, size);
99  }
100  else
101  return -1;
102 }
103 
105  return false;
106 }
107 
109  return closeMain();
110 }
111 
113 {
114  verb = (prop.check("verbose",Value(0),"Specifies if the device is in verbose mode (0/1).").asInt32())>0;
115  if (verb)
116  yCInfo(SERVERSERIAL, "running with verbose output");
117 
118  Value *name;
119  if (prop.check("subdevice",name,"name of specific control device to wrap")) {
120  yCDebug(SERVERSERIAL, "Subdevice %s", name->toString().c_str());
121  if (name->isString()) {
122  // maybe user isn't doing nested configuration
123  Property p;
124  p.setMonitor(prop.getMonitor(),
125  "subdevice"); // pass on any monitoring
126  p.fromString(prop.toString());
127  p.put("device",name->toString());
128  poly.open(p);
129  } else {
130  Bottle subdevice = prop.findGroup("subdevice").tail();
131  poly.open(subdevice);
132  }
133  if (!poly.isValid()) {
134  yCError(SERVERSERIAL, "cannot make <%s>", name->toString().c_str());
135  }
136  } else {
137  yCError(SERVERSERIAL, "\"--subdevice <name>\" not set for serial");
138  return false;
139  }
140 
141  if (!poly.isValid()) {
142  return false;
143  }
144 
145  std::string rootName =
146  prop.check("name",Value("/serial"),
147  "prefix for port names").asString();
148 
149  command_buffer.attach(toDevice);
150  reply_buffer.attach(fromDevice);
151 
152  command_buffer.useCallback(callback_impl);
153 
154  toDevice.open(rootName+"/in");
155  fromDevice.open(rootName+"/out");
156 
157 
158 
159  if (poly.isValid())
160  poly.view(serial);
161 
162  if(serial != nullptr) {
163  start();
164  return true;
165  }
166 
167  yCError(SERVERSERIAL, "subdevice <%s> doesn't look like a serial port (no appropriate interfaces were acquired)",
168  name->toString().c_str());
169 
170  return false;
171 }
172 
174  yCInfo(SERVERSERIAL, "Server Serial starting");
175  //double before, now;
176  while (!isStopping()) {
177  //before = SystemClock::nowSystem();
178  Bottle& b = reply_buffer.get();
179  b.clear();
180  receive( b );
181  /*if(b.size() > 0)*/ /* this line was creating a memory leak !! */
182  reply_buffer.write();
183  //now = SystemClock::nowSystem();
184  // give other threads the chance to run
186  }
187  yCInfo(SERVERSERIAL, "Server Serial stopping");
188 }
189 
190 
191 // ImplementCallbackHelper class.
192 
193 
195 {
196  ser = dynamic_cast<yarp::dev::ISerialDevice *> (x);
197  if (ser==nullptr) {
198  yCError(SERVERSERIAL, "Could not get serial device");
199  std::exit(1);
200  }
201 
202 
203 }
204 
206 {
207  if (ser) {
208  bool ok = ser->send(b);
209  if (!ok)
210  yCError(SERVERSERIAL, "Problems while trying to send data");
211  }
212 }
yarp::os::Bottle
A simple collection of objects that can be described and transmitted in a portable way.
Definition: Bottle.h:73
yarp::os::Bottle::toString
std::string toString() const override
Gives a human-readable textual representation of the bottle.
Definition: Bottle.cpp:214
yarp::os::Property::put
void put(const std::string &key, const std::string &value)
Associate the given key with the given string.
Definition: Property.cpp:998
yarp::os::Bottle::clear
void clear()
Empties the bottle of any objects it contains.
Definition: Bottle.cpp:124
yarp::os::Searchable
A base class for nested structures that can be searched.
Definition: Searchable.h:69
ServerSerial::~ServerSerial
~ServerSerial() override
Definition: ServerSerial.cpp:20
yarp::dev::ISerialDevice::receive
virtual bool receive(yarp::os::Bottle &msg)=0
Gets the existing chars in the receive queue.
yarp::os::Searchable::findGroup
virtual Bottle & findGroup(const std::string &key) const =0
Gets a list corresponding to a given keyword.
yarp::os::Searchable::toString
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
yarp::os::Property::fromString
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
Definition: Property.cpp:1046
yarp::dev::PolyDriver::isValid
bool isValid() const
Check if device is valid.
Definition: PolyDriver.cpp:199
YARP_LOG_COMPONENT
#define YARP_LOG_COMPONENT(name,...)
Definition: LogComponent.h:80
ImplementCallbackHelper2::ImplementCallbackHelper2
ImplementCallbackHelper2()
Constructor.
ServerSerial::receiveBytes
int receiveBytes(unsigned char *bytes, const int size) override
Gets an array of bytes (unsigned char) with size <= 'size' parameter.
Definition: ServerSerial.cpp:95
ServerSerial::send
bool send(const Bottle &msg) override
Sends a string of chars to the serial communications channel.
Definition: ServerSerial.cpp:25
ServerSerial::setDTR
bool setDTR(bool enable) override
Enable/Disable DTR protocol.
Definition: ServerSerial.cpp:77
yarp::dev::DeviceDriver::view
bool view(T *&x)
Get an interface to the device driver.
Definition: DeviceDriver.h:77
yarp::os::Port::open
bool open(const std::string &name) override
Start port operation, with a specific name, with automatically-chosen network parameters.
Definition: Port.cpp:82
yarp::os::PortReaderBuffer::attach
void attach(Port &port)
Attach this buffer to a particular port.
Definition: PortReaderBuffer-inl.h:119
yarp::dev::ISerialDevice
A generic interface to serial port devices.
Definition: ISerialDevice.h:28
yarp::dev::ISerialDevice::receiveBytes
virtual int receiveBytes(unsigned char *bytes, const int size)=0
Gets an array of bytes (unsigned char) with size <= 'size' parameter.
yarp::dev::ISerialDevice::setDTR
virtual bool setDTR(bool enable)=0
Enable/Disable DTR protocol.
yarp::dev::PolyDriver::open
bool open(const std::string &txt)
Construct and configure a device by its common name.
Definition: PolyDriver.cpp:143
yarp::dev::ISerialDevice::receiveChar
virtual int receiveChar(char &chr)=0
Gets one single char from the receive queue.
yarp::os::Value::isString
virtual bool isString() const
Checks if value is a string.
Definition: Value.cpp:159
yarp::os::SystemClock::delaySystem
static void delaySystem(double seconds)
Definition: SystemClock.cpp:32
Os.h
yarp::os::Searchable::check
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
yarp::os::PortReaderBuffer::useCallback
void useCallback(TypedReaderCallback< T > &callback) override
Set an object whose onRead method will be called when data is available.
Definition: PortReaderBuffer-inl.h:126
ServerSerial
serial: Export a serial sensor.
Definition: ServerSerial.h:75
ImplementCallbackHelper2::onRead
void onRead(Bottle &b) override
Callback function.
Definition: ServerSerial.cpp:205
ImplementCallbackHelper2::ser
yarp::dev::ISerialDevice * ser
Definition: ServerSerial.h:41
LogComponent.h
yarp::os::PortWriterBuffer::attach
void attach(Port &port)
Set the Port to which objects will be written.
Definition: PortWriterBuffer.h:125
ServerSerial::run
void run() override
The thread main loop deals with writing on ports here.
Definition: ServerSerial.cpp:173
yarp::os::Bottle::tail
Bottle tail() const
Get all but the first element of a bottle.
Definition: Bottle.cpp:391
yCError
#define yCError(component,...)
Definition: LogComponent.h:157
ServerSerial::close
bool close() override
Close the device driver by deallocating all resources and closing ports.
Definition: ServerSerial.cpp:108
yarp::os::Thread::isStopping
bool isStopping()
Returns true if the thread is stopping (Thread::stop has been called).
Definition: Thread.cpp:102
yCInfo
#define yCInfo(component,...)
Definition: LogComponent.h:135
yCDebug
#define yCDebug(component,...)
Definition: LogComponent.h:112
ServerSerial::receiveLine
int receiveLine(char *line, const int MaxLineLength) override
Gets one line (a sequence of chars with a ending '\n' or '\r') from the receive queue.
Definition: ServerSerial.cpp:86
ServerSerial::receive
bool receive(Bottle &msg) override
Gets the existing chars in the receive queue.
Definition: ServerSerial.cpp:49
ServerSerial::open
virtual bool open()
Default open() method.
Definition: ServerSerial.cpp:104
ServerSerial::flush
int flush() override
Flushes the internal buffer.
Definition: ServerSerial.cpp:68
ServerSerial::receiveChar
int receiveChar(char &c) override
Gets one single char from the receive queue.
Definition: ServerSerial.cpp:59
yarp::os::PortWriterBuffer::get
T & get()
A synonym of PortWriterBuffer::prepare.
Definition: PortWriterBuffer.h:102
yarp::os::PortWriterBuffer::write
void write(bool forceStrict=false)
Try to write the last buffer returned by PortWriterBuffer::get.
Definition: PortWriterBuffer.h:133
yarp::dev::ISerialDevice::send
virtual bool send(const yarp::os::Bottle &msg)=0
Sends a string of chars to the serial communications channel.
yarp::os::Thread::start
bool start()
Start the new thread running.
Definition: Thread.cpp:96
yarp::dev::ISerialDevice::receiveLine
virtual int receiveLine(char *line, const int MaxLineLength)=0
Gets one line (a sequence of chars with a ending '\n' or '\r') from the receive queue.
ServerSerial.h
yarp::os::Value::toString
std::string toString() const override
Return a standard text representation of the content of the object.
Definition: Value.cpp:359
yarp::os::Value
A single value (typically within a Bottle).
Definition: Value.h:47
yarp::dev::ISerialDevice::flush
virtual int flush()=0
Flushes the internal buffer.
yarp::os::Property
A class for storing options and configuration information.
Definition: Property.h:37