YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
FakeSerialPort.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2023-2023 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6#include "FakeSerialPort.h"
7
8#include <yarp/os/Log.h>
10
11#include <cstdio>
12#include <cstdlib>
13
14using namespace yarp::os;
15using namespace yarp::dev;
16
17namespace {
18YARP_LOG_COMPONENT(FAKESERIALPORT, "yarp.device.FakeSerialPort")
19}
20
23
28
30{
31 if (!this->parseParams(config)) {return false;}
32
33 return true;
34}
35
37{
38 return true;
39}
40
41bool FakeSerialPort::setDTR(bool value)
42{
43 return true;
44}
45
47{
48 if (msg.size() > 0)
49 {
50 int message_size = msg.get(0).asString().length();
51
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
60 {
61 if (verbose)
62 {
63 yCDebug(FAKESERIALPORT, "The input command bottle contains an empty string.");
64 }
65 return false;
66 }
67 }
68 else
69 {
70 if (verbose)
71 {
72 yCDebug(FAKESERIALPORT, "The input command bottle is empty. \n");
73 }
74 return false;
75 }
76
77 return true;
78}
79
80bool FakeSerialPort::send(const char *msg, size_t size)
81{
82 if (size > 0)
83 {
84 if (verbose)
85 {
86 yCDebug(FAKESERIALPORT, "Sending string: %s", msg);
87 }
88
89 // Write message in the serial device
90 size_t bytes_written = size;
91
92 if (bytes_written == -1)
93 {
94 yCError(FAKESERIALPORT, "Unable to write to serial port");
95 return false;
96 }
97 }
98 else
99 {
100 if (verbose) {
101 yCDebug(FAKESERIALPORT, "The input message is empty. \n");
102 }
103 return false;
104 }
105
106 yCInfo (FAKESERIALPORT, "sent command: %s \n",msg);
107 return true;
108}
109
111{
112 char chr='c';
113
114 size_t bytes_read = 1;
115
116 if (bytes_read == -1)
117 {
118 yCError(FAKESERIALPORT, "Error in SerialDeviceDriver::receive()");
119 return 0;
120 }
121
122 if (bytes_read == 0)
123 {
124 return 0;
125 }
126
127 c=chr;
128 return 1;
129}
130
132{
133 return 1;
134}
135
136int FakeSerialPort::receiveBytes(unsigned char* bytes, const int size)
137{
138 for (size_t i=0; i< size; i++)
139 bytes[i]='0'+i;
140 return size;
141}
142
144{
145 int i;
146 for (i = 0; i < MaxLineLength -1; ++i)
147 {
148 char recv_ch;
149 int n = receiveChar(recv_ch);
150 if (n <= 0)
151 {
152 //this invalidates the whole line, because no line terminator \n was found
153 return 0;
154
155 //use this commented code here if you do NOT want to invalidate the line
156 //buffer[i] = '\0';
157 //return i;
158 }
160 {
161 buffer[i] = recv_ch;
162 i++;
163 break;
164 }
165 buffer[i] = recv_ch;
166 }
167 buffer[i] = '\0';
168 return i;
169}
170
172{
173 char message[10] = "123456789";
174
175 //this function call blocks
176 size_t bytes_read = 9;
177
178 if (bytes_read == -1)
179 {
180 yCError(FAKESERIALPORT, "Error in SerialDeviceDriver::receive()");
181 return false;
182 }
183
184 if (bytes_read == 0) { //nothing there
185 return true;
186 }
187
188 message[bytes_read] = 0;
189
190 if (verbose)
191 {
192 yCDebug(FAKESERIALPORT, "Data received from serial device: %s", message);
193 }
194
195
196 // Put message in the bottle
197 msg.addString(message);
198
199 return true;
200}
bool parseParams(const yarp::os::Searchable &config) override
Parse the DeviceDriver parameters.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
int receiveBytes(unsigned char *bytes, const int size) override
Gets an array of bytes (unsigned char) with size <= 'size' parameter.
virtual ~FakeSerialPort()
int flush() override
Flushes the internal buffer.
bool receive(Bottle &msg) override
Gets the existing chars in the receive queue.
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.
bool send(const Bottle &msg) override
Sends a string of chars to the serial communications channel.
bool close() override
Close the DeviceDriver.
bool setDTR(bool value) override
Enable/Disable DTR protocol.
int receiveChar(char &chr) override
Gets one single char from the receive queue.
A simple collection of objects that can be described and transmitted in a portable way.
Definition Bottle.h:64
size_type size() const
Gets the number of elements in the bottle.
Definition Bottle.cpp:251
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
Definition Bottle.cpp:246
void addString(const char *str)
Places a string in the bottle, at the end of the list.
Definition Bottle.cpp:170
A mini-server for performing network communication in the background.
A base class for nested structures that can be searched.
Definition Searchable.h:31
virtual std::string asString() const
Get string value.
Definition Value.cpp:234
#define yCInfo(component,...)
#define yCError(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
For streams capable of holding different kinds of content, check what they actually have.
An interface to the operating system, including Port based communication.