0

The code that I have posted below, is basically used to get (continuous streaming data) from a software and display that data as it is received. The issue that I am facing is, that the software (which already has the "server") is on Windows, but I need to get the data on a separate system running Linux (Ubuntu).

Can anyone guide me regarding what changes do I need to make to the code in-order to make it work on Linux?

Also since they are "communicating" over a network, will there be any changes in the code to point to the server on the windows machine? ( I am sorry for such terminology, I am a bit new to this, so please correct me if I am mistaken)

#include "vrpn_Connection.h" // Missing this file?  Get the latest VRPN distro at
#include "vrpn_Tracker.h"    //    ftp://ftp.cs.unc.edu/pub/packages/GRIP/vrpn

#include "conio.h"           // for kbhit()

//== Callback prototype ==--

void VRPN_CALLBACK handle_pos (void *, const vrpn_TRACKERCB t);

//== Main entry point ==--

int main(int argc, char* argv[])
{
vrpn_Connection *connection;

char connectionName[128];
int  port = 3883;

sprintf(connectionName,"localhost:%d", port);

connection = vrpn_get_connection_by_name(connectionName);

vrpn_Tracker_Remote *tracker = new vrpn_Tracker_Remote("Tracker", connection);

tracker->register_change_handler(NULL, handle_pos);


while(!kbhit())
{
    tracker->mainloop();
    connection->mainloop();
    Sleep(5);
}

return 0;
}

//== Position/Orientation Callback ==--

void VRPN_CALLBACK handle_pos (void *, const vrpn_TRACKERCB t)
{
printf("Tracker Position:(%.4f,%.4f,%.4f) Orientation:(%.2f,%.2f,%.2f,%.2f)\n",
    t.pos[0], t.pos[1], t.pos[2],
    t.quat[0], t.quat[1], t.quat[2], t.quat[3]);
}

I would appreciate if anyone could suggest an "easier" alternate to this as well. Thank you!

sj22
  • 101
  • 2
  • 10

1 Answers1

0

kbhit() and Sleep() functions are exclusive to Windows.

Here you don't really need kbhit function. You can use an infinite loop instead.

For the sleep method, you can use this code from this thread : Sleep function in C++

#ifdef _WIN32
    #include <windows.h>

    void sleep(unsigned milliseconds)
    {
        Sleep(milliseconds);
    }
#else
    #include <unistd.h>

    void sleep(unsigned milliseconds)
    {
        usleep(milliseconds * 1000); // takes microseconds
    }
#endif

But a much simpler method is to use boost::this_thread::sleep.

This code should work on Linux and Windows.

//...
while(1)
{
    tracker->mainloop();
    connection->mainloop();
    sleep(5000);
}
//...
Community
  • 1
  • 1
TermWay
  • 69
  • 1
  • 5