Skip to content

Commit 9e1814b

Browse files
committed
ISerialDevice interface refactored to use yarp::dev::ReturnValue
breaking changes to the following devices: SerialPort_nws_yarp, SerialPort_nwc_yarp, FakeSerialPort, SerialDeviceDriver, laserHokuyo added ISerialTest
1 parent 58b4686 commit 9e1814b

File tree

19 files changed

+2529
-484
lines changed

19 files changed

+2529
-484
lines changed

src/devices/fake/fakeSerialPort/FakeSerialPort.cpp

Lines changed: 56 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -38,119 +38,125 @@ bool FakeSerialPort::close()
3838
return true;
3939
}
4040

41-
bool FakeSerialPort::setDTR(bool value)
41+
ReturnValue FakeSerialPort::setDTR(bool value)
4242
{
43-
return true;
43+
return ReturnValue_ok;
4444
}
4545

46-
bool FakeSerialPort::send(const Bottle& msg)
46+
ReturnValue FakeSerialPort::sendString(const std::string& msg)
4747
{
48-
if (msg.size() > 0)
49-
{
50-
int message_size = msg.get(0).asString().length();
48+
size_t message_size = msg.size();
5149

52-
if (message_size > 0)
53-
{
54-
if (verbose)
55-
{
56-
yCDebug(FAKESERIALPORT, "Sending string: %s", msg.get(0).asString().c_str());
57-
}
58-
}
59-
else
50+
if (message_size > 0)
51+
{
52+
if (verbose)
6053
{
61-
if (verbose)
62-
{
63-
yCDebug(FAKESERIALPORT, "The input command bottle contains an empty string.");
64-
}
65-
return false;
54+
yCDebug(FAKESERIALPORT, "Sending string: %s", msg.c_str());
6655
}
6756
}
6857
else
6958
{
7059
if (verbose)
7160
{
72-
yCDebug(FAKESERIALPORT, "The input command bottle is empty. \n");
61+
yCDebug(FAKESERIALPORT, "The string contains an empty string.");
7362
}
74-
return false;
63+
return yarp::dev::ReturnValue::return_code::return_value_error_method_failed;
7564
}
7665

77-
return true;
66+
return ReturnValue_ok;
7867
}
7968

80-
bool FakeSerialPort::send(const char *msg, size_t size)
69+
ReturnValue FakeSerialPort::sendByte(unsigned char byt)
8170
{
82-
if (size > 0)
71+
yCInfo(FAKESERIALPORT, "sent byte : %c \n", byt);
72+
return ReturnValue_ok;
73+
}
74+
75+
ReturnValue FakeSerialPort::sendBytes(const std::vector<unsigned char>& msg)
76+
{
77+
if (msg.size() > 0)
8378
{
8479
if (verbose)
8580
{
8681
yCDebug(FAKESERIALPORT, "Sending string: %s", msg);
8782
}
8883

8984
// Write message in the serial device
90-
size_t bytes_written = size;
85+
size_t bytes_written = msg.size();
9186

9287
if (bytes_written == -1)
9388
{
9489
yCError(FAKESERIALPORT, "Unable to write to serial port");
95-
return false;
90+
return ReturnValue::return_code::return_value_error_method_failed;
9691
}
9792
}
9893
else
9994
{
10095
if (verbose) {
10196
yCDebug(FAKESERIALPORT, "The input message is empty. \n");
10297
}
103-
return false;
98+
return ReturnValue::return_code::return_value_error_method_failed;
10499
}
105100

106101
yCInfo (FAKESERIALPORT, "sent command: %s \n",msg);
107-
return true;
102+
return ReturnValue_ok;
108103
}
109104

110-
int FakeSerialPort::receiveChar(char& c)
105+
ReturnValue FakeSerialPort::receiveByte(unsigned char& c)
111106
{
112-
char chr='c';
107+
char chr='R';
113108

114109
size_t bytes_read = 1;
115110

116111
if (bytes_read == -1)
117112
{
118113
yCError(FAKESERIALPORT, "Error in SerialDeviceDriver::receive()");
119-
return 0;
114+
return ReturnValue::return_code::return_value_error_method_failed;
120115
}
121116

122117
if (bytes_read == 0)
123118
{
124-
return 0;
119+
return ReturnValue::return_code::return_value_error_method_failed;
125120
}
126121

127122
c=chr;
128-
return 1;
123+
return ReturnValue_ok;
129124
}
130125

131-
int FakeSerialPort::flush()
126+
ReturnValue FakeSerialPort::flush()
132127
{
133-
return 1;
128+
size_t dummy_counter = 0;
129+
return flush(dummy_counter);
134130
}
135131

136-
int FakeSerialPort::receiveBytes(unsigned char* bytes, const int size)
132+
ReturnValue FakeSerialPort::flush(size_t& flushed)
137133
{
138-
for (size_t i=0; i< size; i++)
139-
bytes[i]='0'+i;
140-
return size;
134+
flushed = 3;
135+
return ReturnValue_ok;
141136
}
142137

143-
int FakeSerialPort::receiveLine(char* buffer, const int MaxLineLength)
138+
ReturnValue FakeSerialPort::receiveBytes(std::vector<unsigned char>& line, const int MaxSize)
139+
{
140+
line.clear();
141+
line.resize(1000);
142+
for (size_t i = 0; i < MaxSize; i++)
143+
{
144+
line[i]= ('0' + i);
145+
}
146+
return ReturnValue_ok;
147+
}
148+
149+
ReturnValue FakeSerialPort::receiveLine(std::vector<char>& line, const int MaxLineLength)
144150
{
145151
int i;
146152
for (i = 0; i < MaxLineLength -1; ++i)
147153
{
148-
char recv_ch;
149-
int n = receiveChar(recv_ch);
150-
if (n <= 0)
154+
unsigned char recv_ch;
155+
auto ret = receiveByte(recv_ch);
156+
if (!ret)
151157
{
152158
//this invalidates the whole line, because no line terminator \n was found
153-
return 0;
159+
return ReturnValue::return_code::return_value_error_method_failed;
154160

155161
//use this commented code here if you do NOT want to invalidate the line
156162
//buffer[i] = '\0';
@@ -168,7 +174,7 @@ int FakeSerialPort::receiveLine(char* buffer, const int MaxLineLength)
168174
return i;
169175
}
170176

171-
bool FakeSerialPort::receive(Bottle& msg)
177+
ReturnValue FakeSerialPort::receiveString(std::string& msg)
172178
{
173179
char message[10] = "123456789";
174180

@@ -178,11 +184,11 @@ bool FakeSerialPort::receive(Bottle& msg)
178184
if (bytes_read == -1)
179185
{
180186
yCError(FAKESERIALPORT, "Error in SerialDeviceDriver::receive()");
181-
return false;
187+
return ReturnValue::return_code::return_value_error_method_failed;
182188
}
183189

184190
if (bytes_read == 0) { //nothing there
185-
return true;
191+
return ReturnValue_ok;
186192
}
187193

188194
message[bytes_read] = 0;
@@ -193,8 +199,8 @@ bool FakeSerialPort::receive(Bottle& msg)
193199
}
194200

195201

196-
// Put message in the bottle
197-
msg.addString(message);
202+
// Return the message
203+
msg=message;
198204

199-
return true;
205+
return ReturnValue_ok;
200206
}

src/devices/fake/fakeSerialPort/FakeSerialPort.h

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,16 @@ class FakeSerialPort :
4343
bool open(yarp::os::Searchable& config) override;
4444
bool close() override;
4545

46-
bool send(const Bottle& msg) override;
47-
bool send(const char *msg, size_t size) override;
48-
bool receive(Bottle& msg) override;
49-
int receiveChar(char& chr) override;
50-
int receiveBytes(unsigned char* bytes, const int size) override;
51-
int receiveLine(char* line, const int MaxLineLength) override;
52-
bool setDTR(bool value) override;
53-
int flush() override;
46+
yarp::dev::ReturnValue sendString(const std::string& msg) override;
47+
yarp::dev::ReturnValue sendBytes(const std::vector<unsigned char>& line) override;
48+
yarp::dev::ReturnValue sendByte(unsigned char byte) override;
49+
yarp::dev::ReturnValue receiveString(std::string& msg) override;
50+
yarp::dev::ReturnValue receiveBytes(std::vector<unsigned char>& line, const int MaxSize) override;
51+
yarp::dev::ReturnValue receiveByte(unsigned char& chr) override;
52+
yarp::dev::ReturnValue receiveLine(std::vector<char>& line, const int MaxLineLength) override;
53+
yarp::dev::ReturnValue setDTR(bool enable) override;
54+
yarp::dev::ReturnValue flush() override;
55+
yarp::dev::ReturnValue flush(size_t& flushed_chars) override;
5456
};
5557

5658
#endif

src/devices/fake/fakeSerialPort/tests/fakeSerialPort_test.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#include <yarp/os/Network.h>
88
#include <yarp/dev/PolyDriver.h>
99
#include <yarp/dev/WrapperSingle.h>
10-
//#include <yarp/dev/tests/ILocalization2DTest.h>
10+
#include <yarp/dev/tests/ISerialTest.h>
1111

1212
#include <catch2/catch_amalgamated.hpp>
1313
#include <harness.h>
@@ -23,10 +23,10 @@ TEST_CASE("dev::fakeSerialPort", "[yarp::dev]")
2323

2424
SECTION("Checking fakeSerialPort device")
2525
{
26-
yarp::dev::ISerialDevice* iser=nullptr;
26+
yarp::dev::ISerialDevice* iser = nullptr;
2727
PolyDriver ddfake;
2828

29-
//open the device
29+
// open the device
3030
{
3131

3232
Property pdev_cfg;
@@ -38,8 +38,10 @@ TEST_CASE("dev::fakeSerialPort", "[yarp::dev]")
3838
REQUIRE(iser);
3939
}
4040

41-
iser->flush();
42-
//yarp::dev::tests::exec_iLocalization2D_test_1(iser);
41+
// tests
42+
{
43+
yarp::dev::tests::exec_iSerial_test_1(iser);
44+
}
4345

4446
//"Close all polydrivers and check"
4547
{

0 commit comments

Comments
 (0)