ROS Serial on Arduino-Due, message not recei

After having installed Ros Serial on my Linux computer, I am doing a simple test.
Om my Windows PC, I have configured my Arduino-Due as follows in the tools menu:

  • Board: Arduino Native USB port
  • Port: COM5 Programming port
    My Arduino code:

#include <ros.h>
#include <std_msgs/String.h>


ros::NodeHandle  nh;

std_msgs::String str_msg;
ros::Publisher fromArduino("fromArduino", &str_msg);

char hello[19] = "hello from Arduino";

void setup()
{
  Serial.println("entering setup");
  nh.initNode();
  nh.advertise(fromArduino);
}

void loop()
{
  Serial.println("in the loop");
  str_msg.data = hello;
  fromArduino.publish( &str_msg );
  nh.spinOnce();
  delay(1000);
}

On the Linux computer I have coded a subscriber and publisher as follows:

#include <ros/ros.h>
#include "std_msgs/String.h"
#include <stdlib.h>
#include <iostream>


std::stringstream s;

//callback handler:
void onReceiveString(const std_msgs::String::ConstPtr& msg)
{
   ROS_INFO("\n received string: [%s] \n", msg->data.c_str());        
}//end call back
   
 
//Main:   
int main(int argc, char** argv)
{
  
  printf("entering main \n");

  //ROS inits:
  ros::init(argc, argv, "test_node");
  ros::NodeHandle nh;
  ros::Publisher test_pub = nh.advertise<std_msgs::String>("toArduino", 50);
  ros::Rate loop_rate(10);  

   ros::Subscriber sub = nh.subscribe("fromArduino", 1000, onReceiveString);
  
  //ROS loop for publishing:
  int count = 0;
  while(ros::ok())
  {                              
    std_msgs::String msg;
    msg.data = "hello to arduino\n";
    ROS_INFO("%s", msg.data.c_str());
    test_pub.publish(msg);
    ros::spinOnce();
    loop_rate.sleep();
    ++count;
 
  }//end while()
   
   return 0;

}//end main()

I checked the port with: dmesg | grep tty, and: "/dev/ttyACM0" came up correctly
I compiled and ran my program the usual way (not forgetting to add: /dev/ttyACM0 at the end of the rosrun command).
I entered: rostopic list. My topics: /fromArduino and /toArduino appeared correctly.
However, when I enter: rostopic echo /fromArduino nothing comes. No errors, no messages.
Anyone can tell if I forgot something?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.