0
$\begingroup$

Rosanswers logo

this is the subscriber node from ros tutorials:

void chatterCallback(const std_msgs::String::ConstPtr& msg) 
{
  ROS_INFO("I heard: [%s]", msg->data.c_str());
}

My string array consists of msg which is shown in image, Now i need to access a particular element in the msg (message consists first time and next CAN message, I want to use particularly CAN.ID whic is arrays second element) any suggestion. thanks in advance


Originally posted by newbieros on ROS Answers with karma: 31 on 2017-01-28

Post score: 0


Original comments

Comment by gvdhoorn on 2017-01-28:
First: std_msgs/String is a very bad choice for this, as it has no semantics whatsoever. a String is not the same as a CAN frame, even though both consists of a 'string' of bytes.

Comment by gvdhoorn on 2017-01-28:
Second: if possible, see if you can reuse something that already exists. It could save you a lot of effort.

Perhaps ros_canopen can do what you need.

Comment by newbieros on 2017-01-28:
I am using some CAN USB interface, where I am using those receive and transmit nodes which are already existed for this hardware type. So the message that they have used here is std_msgs/String.

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

C++'s std::string class provides an indexing operator implementation that lets you access individual characters in the string. See std::string::operator[] for details.

In your particular case, you could do something like:

size_t my_index = ...;
char the_byte = msg->data[my_index];

Now the_byte contains the char at position my_index in msg->data (which is of type std::string (wiki/msg)).

As I wrote in my comment on your OP though: std_msgs/String is really not a good message to use for interfacing with CAN wrappers (or for storing byte buffers in, period).


Originally posted by gvdhoorn with karma: 86574 on 2017-01-29

This answer was ACCEPTED on the original site

Post score: 1


Original comments

Comment by gvdhoorn on 2017-01-29:
Note btw that this is not something that is ROS specific at all. It's a basic C++ question.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.