From 59e7755e978b20566c7e8c0956362809d60607a6 Mon Sep 17 00:00:00 2001 From: Marco Randazzo Date: Mon, 29 May 2023 12:19:17 +0200 Subject: [PATCH 1/4] improvements to serialPort nws/nwc tests --- src/devices/fakeSerialPort/fakeSerialPort.cpp | 7 +- src/devices/fakeSerialPort/fakeSerialPort.h | 4 +- .../serialPort_nwc_yarp.cpp | 66 ++++++++++++------- .../serialPort_nwc_yarp/serialPort_nwc_yarp.h | 6 +- .../tests/serialPort_nwc_yarp_test.cpp | 8 ++- .../tests/serialPort_nws_yarp_test.cpp | 12 ++++ src/devices/serialport/SerialDeviceDriver.cpp | 2 +- src/devices/serialport/SerialDeviceDriver.h | 2 +- src/libYARP_dev/src/yarp/dev/ISerialDevice.h | 2 +- 9 files changed, 77 insertions(+), 32 deletions(-) diff --git a/src/devices/fakeSerialPort/fakeSerialPort.cpp b/src/devices/fakeSerialPort/fakeSerialPort.cpp index 370090bcb0e..df2a24d5473 100644 --- a/src/devices/fakeSerialPort/fakeSerialPort.cpp +++ b/src/devices/fakeSerialPort/fakeSerialPort.cpp @@ -20,7 +20,6 @@ YARP_LOG_COMPONENT(FAKESERIALPORT, "yarp.device.FakeSerialPort") FakeSerialPort::FakeSerialPort() { //system_resources = (SerialHandler*) new SerialHandler(); - verbose=false; line_terminator_char1 = '\r'; line_terminator_char2 = '\n'; } @@ -103,7 +102,8 @@ bool FakeSerialPort::send(const Bottle& msg) } else { - if (verbose) { + if (verbose) + { yCDebug(FAKESERIALPORT, "The input command bottle is empty. \n"); } return false; @@ -112,7 +112,7 @@ bool FakeSerialPort::send(const Bottle& msg) return true; } -bool FakeSerialPort::send(char *msg, size_t size) +bool FakeSerialPort::send(const char *msg, size_t size) { if (size > 0) { @@ -138,6 +138,7 @@ bool FakeSerialPort::send(char *msg, size_t size) return false; } + yCInfo (FAKESERIALPORT, "sent command: %s \n",msg); return true; } diff --git a/src/devices/fakeSerialPort/fakeSerialPort.h b/src/devices/fakeSerialPort/fakeSerialPort.h index c0f5509d8c9..e0242348ae2 100644 --- a/src/devices/fakeSerialPort/fakeSerialPort.h +++ b/src/devices/fakeSerialPort/fakeSerialPort.h @@ -68,7 +68,7 @@ class FakeSerialPort : FakeSerialPort(const FakeSerialPort&); void operator=(const FakeSerialPort&); - bool verbose; + bool verbose = true; char line_terminator_char1; char line_terminator_char2; @@ -81,7 +81,7 @@ class FakeSerialPort : bool close() override; bool send(const Bottle& msg) override; - bool send(char *msg, size_t size) override; + bool send(const char *msg, size_t size) override; bool receive(Bottle& msg) override; int receiveChar(char& chr) override; int receiveBytes(unsigned char* bytes, const int size) override; diff --git a/src/devices/serialPort_nwc_yarp/serialPort_nwc_yarp.cpp b/src/devices/serialPort_nwc_yarp/serialPort_nwc_yarp.cpp index f3a3d9b72c3..5e1573a3e70 100644 --- a/src/devices/serialPort_nwc_yarp/serialPort_nwc_yarp.cpp +++ b/src/devices/serialPort_nwc_yarp/serialPort_nwc_yarp.cpp @@ -19,12 +19,16 @@ SerialPort_nwc_yarp::~SerialPort_nwc_yarp() bool SerialPort_nwc_yarp::send(const Bottle& msg) { - return false; + m_sendPort.write(msg); + return true; } -bool SerialPort_nwc_yarp::send(char *msg, size_t size) +bool SerialPort_nwc_yarp::send(const char *msg, size_t size) { - return false; + Bottle b; + b.addString(std::string(msg)); + m_sendPort.write(b); + return true; } bool SerialPort_nwc_yarp::receive(Bottle& msg) @@ -65,29 +69,47 @@ bool SerialPort_nwc_yarp::close() bool SerialPort_nwc_yarp::open(Searchable& prop) { - std::string local_rpc = - prop.check("local",Value("/serialPort_nwc_yarp"), - "local rpc port name").asString(); - std::string remote_rpc = - prop.check("remote", Value("/serialPort_nws_yarp/rpc"), - "remote rpc port name").asString(); - - if (!m_rpcPort.open(local_rpc)) - { - yCError(SERIAL_NWC, "open() error could not open rpc port %s, check network", local_rpc.c_str()); - return false; - } - - if (!Network::connect(local_rpc, remote_rpc)) { - yCError(SERIAL_NWC, "open() error could not connect to %s", remote_rpc.c_str()); - return false; + std::string local_rpc = + prop.check("local",Value("/serialPort_nwc_yarp"), + "local rpc port name").asString(); + std::string remote_rpc = + prop.check("remote", Value("/serialPort_nws_yarp/rpc"), + "remote rpc port name").asString(); + + if (!m_rpcPort.open(local_rpc)) + { + yCError(SERIAL_NWC, "open() error could not open rpc port %s, check network", local_rpc.c_str()); + return false; + } + + if (!Network::connect(local_rpc, remote_rpc)) + { + yCError(SERIAL_NWC, "open() error could not connect to %s", remote_rpc.c_str()); + return false; + } + + if (!m_rpc.yarp().attachAsClient(m_rpcPort)) + { + yCError(SERIAL_NWC, "Error! Cannot attach the m_rpc_port port as a client"); + return false; + } } - if (!m_rpc.yarp().attachAsClient(m_rpcPort)) { - yCError(SERIAL_NWC, "Error! Cannot attach the m_rpc_port port as a client"); - return false; + std::string local_send = "/serialPort_nwc_yarp/out"; + std::string remote_send = "/serialPort_nws_yarp/in"; + if (!m_sendPort.open(local_send)) + { + yCError(SERIAL_NWC, "open() error could not open rpc port %s, check network", local_send.c_str()); + return false; + } + + if (!Network::connect(local_send, remote_send)) + { + yCError(SERIAL_NWC, "open() error could not connect to %s", remote_send.c_str()); + return false; + } } return true; diff --git a/src/devices/serialPort_nwc_yarp/serialPort_nwc_yarp.h b/src/devices/serialPort_nwc_yarp/serialPort_nwc_yarp.h index b98c29b533f..6ce74cc72e6 100644 --- a/src/devices/serialPort_nwc_yarp/serialPort_nwc_yarp.h +++ b/src/devices/serialPort_nwc_yarp/serialPort_nwc_yarp.h @@ -36,10 +36,14 @@ class SerialPort_nwc_yarp : private: ISerialMsgs m_rpc; yarp::os::Port m_rpcPort; + yarp::os::Port m_sendPort; + yarp::os::Port m_receivePort; bool closeMain() { m_rpcPort.close(); + m_sendPort.close(); + m_receivePort.close(); return true; } @@ -53,7 +57,7 @@ class SerialPort_nwc_yarp : // ISerialDevice methods bool send(const Bottle& msg) override; - bool send(char *msg, size_t size) override; + bool send(const char *msg, size_t size) override; bool receive(Bottle& msg) override; int receiveChar(char& c) override; int flush () override; diff --git a/src/devices/serialPort_nwc_yarp/tests/serialPort_nwc_yarp_test.cpp b/src/devices/serialPort_nwc_yarp/tests/serialPort_nwc_yarp_test.cpp index 498bdc4e9b2..a89aff6f3ab 100644 --- a/src/devices/serialPort_nwc_yarp/tests/serialPort_nwc_yarp_test.cpp +++ b/src/devices/serialPort_nwc_yarp/tests/serialPort_nwc_yarp_test.cpp @@ -51,7 +51,13 @@ TEST_CASE("dev::serialPort_nwc_yarp", "[yarp::dev]") REQUIRE(ddnwc.view(iser)); } - CHECK (iser->setDTR(true)); + //tests + { + CHECK (iser->setDTR(true)); + yarp::os::Time::delay(0.1); + std::string s("fake_command"); + CHECK (iser->send(s.c_str(),s.size())); + } //"Close all polydrivers and check" { diff --git a/src/devices/serialPort_nws_yarp/tests/serialPort_nws_yarp_test.cpp b/src/devices/serialPort_nws_yarp/tests/serialPort_nws_yarp_test.cpp index bce492f499e..1b51d9e40f9 100644 --- a/src/devices/serialPort_nws_yarp/tests/serialPort_nws_yarp_test.cpp +++ b/src/devices/serialPort_nws_yarp/tests/serialPort_nws_yarp_test.cpp @@ -61,6 +61,18 @@ TEST_CASE("dev::serialPort_nws_yarp", "[yarp::dev]") REQUIRE(result_att); } } + //tests + { + Port p; + CHECK(p.open("/test:o")); + CHECK(Network::connect("/test:o","/serialPort_nws/in")); + yarp::os::Time::delay(0.5); + Bottle b; + b.addString("fake_command"); + CHECK(p.write(b)); + yarp::os::Time::delay(0.5); + p.close(); + } yarp::os::Time::delay(0.5); //"Close all polydrivers and check" diff --git a/src/devices/serialport/SerialDeviceDriver.cpp b/src/devices/serialport/SerialDeviceDriver.cpp index 63657bf1305..a942f5fa8f7 100644 --- a/src/devices/serialport/SerialDeviceDriver.cpp +++ b/src/devices/serialport/SerialDeviceDriver.cpp @@ -159,7 +159,7 @@ bool SerialDeviceDriver::send(const Bottle& msg) return true; } -bool SerialDeviceDriver::send(char *msg, size_t size) +bool SerialDeviceDriver::send(const char *msg, size_t size) { if (size > 0) { diff --git a/src/devices/serialport/SerialDeviceDriver.h b/src/devices/serialport/SerialDeviceDriver.h index dbd44978a62..a63a1323398 100644 --- a/src/devices/serialport/SerialDeviceDriver.h +++ b/src/devices/serialport/SerialDeviceDriver.h @@ -108,7 +108,7 @@ class SerialDeviceDriver : * @return true on success */ bool send(const Bottle& msg) override; - bool send(char *msg, size_t size) override; + bool send(const char *msg, size_t size) override; //bool putMessage(Bottle& msg, bool waitreply, double replytimeout, Bottle& reply, char *replydelimiter, int replysize ); /** * Gets the existing chars in the receive queue. diff --git a/src/libYARP_dev/src/yarp/dev/ISerialDevice.h b/src/libYARP_dev/src/yarp/dev/ISerialDevice.h index ebcb3e5fcb4..bbff22de55b 100644 --- a/src/libYARP_dev/src/yarp/dev/ISerialDevice.h +++ b/src/libYARP_dev/src/yarp/dev/ISerialDevice.h @@ -31,7 +31,7 @@ class YARP_dev_API ISerialDevice * \return true on success */ virtual bool send(const yarp::os::Bottle& msg) = 0; - virtual bool send(char *msg, size_t size) = 0; + virtual bool send(const char *msg, size_t size) = 0; //bool putMessage(Bottle& msg, bool waitreply, double replytimeout, Bottle& reply, char *replydelimiter, int replysize ); /** From df3b21a55a05a53c8c16e1f600ad986e7dc6cc6f Mon Sep 17 00:00:00 2001 From: Silvio Traversaro Date: Sat, 13 May 2023 09:16:04 +0200 Subject: [PATCH 2/4] opencv_grabber: Remove use of highgui include --- src/devices/opencv/OpenCVGrabber.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/devices/opencv/OpenCVGrabber.cpp b/src/devices/opencv/OpenCVGrabber.cpp index a0b4739e1d2..b78655dce0c 100644 --- a/src/devices/opencv/OpenCVGrabber.cpp +++ b/src/devices/opencv/OpenCVGrabber.cpp @@ -26,7 +26,6 @@ #include // memcpy -#include #include #include From 83f1767120309763b5c5c10783a2d4f017297f93 Mon Sep 17 00:00:00 2001 From: Silvio Traversaro Date: Sat, 13 May 2023 09:17:38 +0200 Subject: [PATCH 3/4] opencv_grabber: Remove use of highgui include --- src/devices/opencv/OpenCVGrabber.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/devices/opencv/OpenCVGrabber.h b/src/devices/opencv/OpenCVGrabber.h index 4055c70f9d6..cd04b036625 100644 --- a/src/devices/opencv/OpenCVGrabber.h +++ b/src/devices/opencv/OpenCVGrabber.h @@ -20,8 +20,6 @@ #include #include -#include - /** * @ingroup dev_impl_media * From 67b0782727f4800fca1b16b9f542db0ec1608062 Mon Sep 17 00:00:00 2001 From: Silvio Traversaro Date: Sat, 13 May 2023 10:45:28 +0200 Subject: [PATCH 4/4] opencv_grabber: Include what you use --- src/devices/opencv/OpenCVGrabber.cpp | 4 ++-- src/devices/opencv/OpenCVGrabber.h | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/devices/opencv/OpenCVGrabber.cpp b/src/devices/opencv/OpenCVGrabber.cpp index b78655dce0c..f25db6b41b7 100644 --- a/src/devices/opencv/OpenCVGrabber.cpp +++ b/src/devices/opencv/OpenCVGrabber.cpp @@ -26,8 +26,8 @@ #include // memcpy -#include -#include +#include +#include using yarp::dev::DeviceDriver; diff --git a/src/devices/opencv/OpenCVGrabber.h b/src/devices/opencv/OpenCVGrabber.h index cd04b036625..c8db8d3f7d9 100644 --- a/src/devices/opencv/OpenCVGrabber.h +++ b/src/devices/opencv/OpenCVGrabber.h @@ -20,6 +20,8 @@ #include #include +#include + /** * @ingroup dev_impl_media *