|
YARP
Yet Another Robot Platform
|
|
Go to the documentation of this file.
22 #define MAX_FLUSHED_BYTES 10000
33 line_terminator_char1 =
'\r';
34 line_terminator_char2 =
'\n';
50 if(_serialConnector.connect(_serial_dev, ACE_DEV_Addr(config.
CommChannel)) == -1)
52 yCError(SERIALPORT,
"Invalid communications port in %s: %s\n", config.
CommChannel, strerror(errno));
58 if (_serial_dev.control (ACE_TTY_IO::SETPARAMS, &config.
SerialParams) == -1)
70 strcpy(config2.
CommChannel, config.
check(
"comport",
Value(
"COM3"),
"name of the serial channel").asString().c_str());
71 this->verbose = (config.
check(
"verbose",
Value(1),
"Specifies if the device is in verbose mode (0/1).").asInt32())>0;
72 config2.
SerialParams.baudrate = config.
check(
"baudrate",
Value(9600),
"Specifies the baudrate at which the communication port operates.").asInt32();
73 config2.
SerialParams.xonlim = config.
check(
"xonlim",
Value(0),
"Specifies the minimum number of bytes in input buffer before XON char is sent. Negative value indicates that default value should be used (Win32)").asInt32();
74 config2.
SerialParams.xofflim = config.
check(
"xofflim",
Value(0),
"Specifies the maximum number of bytes in input buffer before XOFF char is sent. Negative value indicates that default value should be used (Win32). ").asInt32();
78 config2.
SerialParams.readmincharacters = config.
check(
"readmincharacters",
Value(1),
"Specifies the minimum number of characters for non-canonical read (POSIX).").asInt32();
79 config2.
SerialParams.readtimeoutmsec = config.
check(
"readtimeoutmsec",
Value(100),
"Specifies the time to wait before returning from read. Negative value means infinite timeout.").asInt32();
81 std::string temp = config.
check(
"paritymode",
Value(
"EVEN"),
"Specifies the parity mode (EVEN, ODD, NONE). POSIX supports even and odd parity. Additionally Win32 supports mark and space parity modes.").asString();
83 config2.
SerialParams.ctsenb = config.
check(
"ctsenb",
Value(0),
"Enable & set CTS mode. Note that RTS & CTS are enabled/disabled together on some systems (RTS/CTS is enabled if either <code>ctsenb</code> or <code>rtsenb</code> is set).").asInt32();
84 config2.
SerialParams.rtsenb = config.
check(
"rtsenb",
Value(0),
"Enable & set RTS mode. Note that RTS & CTS are enabled/disabled together on some systems (RTS/CTS is enabled if either <code>ctsenb</code> or <code>rtsenb</code> is set).\n- 0 = Disable RTS.\n- 1 = Enable RTS.\n- 2 = Enable RTS flow-control handshaking (Win32).\n- 3 = Specifies that RTS line will be high if bytes are available for transmission.\nAfter transmission RTS will be low (Win32).").asInt32();
85 config2.
SerialParams.xinenb = config.
check(
"xinenb",
Value(0),
"Enable/disable software flow control on input.").asInt32();
86 config2.
SerialParams.xoutenb = config.
check(
"xoutenb",
Value(0),
"Enable/disable software flow control on output.").asInt32();
87 config2.
SerialParams.modem = config.
check(
"modem",
Value(0),
"Specifies if device is a modem (POSIX). If not set modem status lines are ignored. ").asInt32();
88 config2.
SerialParams.rcvenb = config.
check(
"rcvenb",
Value(0),
"Enable/disable receiver (POSIX).").asInt32();
89 config2.
SerialParams.dsrenb = config.
check(
"dsrenb",
Value(0),
"Controls whether DSR is disabled or enabled (Win32).").asInt32();
90 config2.
SerialParams.dtrdisable = config.
check(
"dtrdisable",
Value(0),
"Controls whether DTR is disabled or enabled.").asInt32();
91 config2.
SerialParams.databits = config.
check(
"databits",
Value(7),
"Data bits. Valid values 5, 6, 7 and 8 data bits. Additionally Win32 supports 4 data bits.").asInt32();
92 config2.
SerialParams.stopbits = config.
check(
"stopbits",
Value(1),
"Stop bits. Valid values are 1 and 2.").asInt32();
94 if (config.
check(
"line_terminator_char1",
"line terminator character for receiveLine(), default '\r'"))
95 line_terminator_char1 = config.
find(
"line_terminator_char1").
asInt32();
97 if (config.
check(
"line_terminator_char2",
"line terminator character for receiveLine(), default '\n'"))
98 line_terminator_char2 = config.
find(
"line_terminator_char2").
asInt32();
100 return open(config2);
109 ACE_TTY_IO::Serial_Params arg;
110 int ret = _serial_dev.control(_serial_dev.GETPARAMS, &arg);
111 if (
ret == -1)
return false;
112 arg.dtrdisable = !value;
113 ret = _serial_dev.control(_serial_dev.SETPARAMS, &arg);
114 if (
ret == -1)
return false;
124 if (message_size > 0)
132 ssize_t bytes_written = _serial_dev.send_n((
void *) msg.
get(0).
asString().c_str(), message_size);
134 if (bytes_written == -1)
136 yCError(SERIALPORT,
"Unable to write to serial port");
142 if (verbose)
yCDebug(SERIALPORT,
"The input command bottle contains an empty string.");
148 if (verbose)
yCDebug(SERIALPORT,
"The input command bottle is empty. \n");
161 yCDebug(SERIALPORT,
"Sending string: %s", msg);
165 ssize_t bytes_written = _serial_dev.send_n((
void *)msg, size);
167 if (bytes_written == -1)
169 yCError(SERIALPORT,
"Unable to write to serial port");
175 if (verbose)
yCDebug(SERIALPORT,
"The input message is empty. \n");
187 ssize_t bytes_read = _serial_dev.recv ((
void *) &chr, 1);
189 if (bytes_read == -1)
191 yCError(SERIALPORT,
"Error in SerialDeviceDriver::receive()");
211 bytes_read = _serial_dev.recv((
void *) &chr, 100);
215 while (bytes_read>0);
223 return _serial_dev.recv((
void *)bytes, size);
226 for (i = 0; i < size ; ++i)
229 int n = receiveChar(recv_ch);
243 for (i = 0; i < MaxLineLength -1; ++i)
246 int n = receiveChar(recv_ch);
256 if ((recv_ch == line_terminator_char1) || (recv_ch == line_terminator_char2))
270 const int msgSize = 1001;
274 ssize_t bytes_read = _serial_dev.recv ((
void *) message, msgSize - 1);
276 if (bytes_read == -1)
278 yCError(SERIALPORT,
"Error in SerialDeviceDriver::receive()");
285 message[bytes_read] = 0;
289 yCDebug(SERIALPORT,
"Data received from serial device: %s", message);
A simple collection of objects that can be described and transmitted in a portable way.
bool send(const Bottle &msg) override
Sends a string of chars to the serial communications channel.
A base class for nested structures that can be searched.
bool open(yarp::os::Searchable &config) override
Open the DeviceDriver.
size_type size() const
Gets the number of elements in the bottle.
int receiveChar(char &chr) override
Gets one single char from the receive queue.
#define YARP_LOG_COMPONENT(name,...)
An interface for the device drivers.
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.
int receiveBytes(unsigned char *bytes, const int size) override
Gets an array of bytes (unsigned char) with size <= 'size' parameter.
bool setDTR(bool value) override
Enable/Disable DTR protocol.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
bool receive(Bottle &msg) override
Gets the existing chars in the receive queue.
virtual std::string asString() const
Get string value.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
void addString(const char *str)
Places a string in the bottle, at the end of the list.
virtual ~SerialDeviceDriver()
ACE_TTY_IO::Serial_Params SerialParams
#define yCError(component,...)
virtual std::int32_t asInt32() const
Get 32-bit integer value.
int flush() override
Flushes the internal buffer.
#define yCInfo(component,...)
An interface to the operating system, including Port based communication.
#define yCDebug(component,...)
#define MAX_FLUSHED_BYTES
bool close() override
Close the DeviceDriver.
A single value (typically within a Bottle).