0
$\begingroup$

Rosanswers logo

Hi, rosserial windows works on visual studio win32 console mode but does not work on windows forms mode. Here is what I have done:

I am porting win32 Console rosserial_hello_world from http://wiki.ros.org/rosserial_windows/Tutorials/Hello%20World to windows forms in visual studio. It built successfully but when I run the app but the ROS master on Ubuntu did not receive any data. It works on Win32 console mode. Please help. Thanks.

The code for windows forms:

#pragma once

#include #include <stdio.h> #include "ros.h" #include <geometry_msgs/Twist.h> #include <windows.h>

ros::NodeHandle nh; char *ros_master = "192.168.186.132"; geometry_msgs::Twist twist_msg; ros::Publisher cmd_vel_pub ("cmd_vel", &twist_msg); using std::string;

namespace rosserial_hello_world_forms {

using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;

/// <summary>
/// Summary for Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
    Form1(void)
    {
        InitializeComponent();
        //
        //TODO: Add the constructor code here
        //
    }

protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~Form1()
    {
        if (components)
        {
            delete components;
        }
    }
private: System::Windows::Forms::Timer^  timer1;
private: System::Windows::Forms::Button^  button1;
private: System::Windows::Forms::ListBox^  listBox1;
private: System::Windows::Forms::TextBox^  textBox1;
protected: 
private: System::ComponentModel::IContainer^  components;

private:
    /// <summary>
    /// Required designer variable.
    /// </summary>

#pragma region Windows Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// void InitializeComponent(void) { this->components = (gcnew System::ComponentModel::Container()); this->timer1 = (gcnew System::Windows::Forms::Timer(this->components)); this->button1 = (gcnew System::Windows::Forms::Button()); this->listBox1 = (gcnew System::Windows::Forms::ListBox()); this->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->SuspendLayout(); // // timer1 // this->timer1->Interval = 1000; this->timer1->Tick += gcnew System::EventHandler(this, &Form1::timer1_Tick); // // button1 // this->button1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_castSystem::Byte(0))); this->button1->Location = System::Drawing::Point(135, 22); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(163, 52); this->button1->TabIndex = 0; this->button1->Text = L"button1"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click); // // listBox1 // this->listBox1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_castSystem::Byte(0))); this->listBox1->FormattingEnabled = true; this->listBox1->ItemHeight = 25; this->listBox1->Location = System::Drawing::Point(22, 111); this->listBox1->Name = L"listBox1"; this->listBox1->Size = System::Drawing::Size(383, 79); this->listBox1->TabIndex = 1; // // textBox1 // this->textBox1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 10, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point, static_castSystem::Byte(0))); this->textBox1->Location = System::Drawing::Point(474, 37); this->textBox1->Name = L"textBox1"; this->textBox1->Size = System::Drawing::Size(336, 30); this->textBox1->TabIndex = 2; // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(9, 20); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(431, 244); this->Controls->Add(this->textBox1); this->Controls->Add(this->listBox1); this->Controls->Add(this->button1); this->Name = L"Form1"; this->Text = L"Rosserial Windows"; this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); this->ResumeLayout(false); this->PerformLayout();

    }

#pragma endregion private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) {

    twist_msg.linear.x = 5.1;
    twist_msg.linear.y = 0;
    twist_msg.linear.z = 0;
    twist_msg.angular.x = 0;
    twist_msg.angular.y = 0;
    twist_msg.angular.z = -1.8;
    cmd_vel_pub.publish (&twist_msg);

    nh.spinOnce ();
    Sleep (100);
    listBox1->Items->Add("Go robot go!");

}
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
             
    String ^message;

            
    listBox1->Items->Add("Connecting to server at");
    message = gcnew String(reinterpret_cast<const char*>(ros_master));
    listBox1->Items->Add (message);
    nh.initNode (ros_master);
    //Sleep(1000);

    //printf ("Advertising cmd_vel message\n");
    listBox1->Items->Add("Advertising cmd_vel message\n");
    nh.advertise (cmd_vel_pub);
    //Sleep(1000);
    timer1->Enabled=true;            
}
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

    timer1->Enabled=false;      

}

}; }


Originally posted by lukevan on ROS Answers with karma: 16 on 2017-05-02

Post score: 0

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

FYI, I have already solved the problem. I need to call at least nh.SpinOnce() one time in the initialization thread before enable the timer.


Originally posted by lukevan with karma: 16 on 2017-05-03

This answer was ACCEPTED on the original site

Post score: 0

$\endgroup$

Your Answer

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