Calling simxStart through a c++ constructor fails

Typically: "How do I... ", "How can I... " questions
Post Reply
Rex
Posts: 15
Joined: 22 Jan 2018, 07:45

Calling simxStart through a c++ constructor fails

Post by Rex » 02 Feb 2018, 08:06

Hi,

I am writing a C++ client that is trying to dynamically setup multiple Remote API connections to multiple VREP instances with successive port numbers (Starting with 10001). I've written a 'Sim' class that contains each remote connection and associated variables within individual objects. These 'Sim' objects are stored within a vector accessed by the main program.

I'm facing an issue where the clientID and connectionID returned are both -1, if I call simxStart through the Sim class constructor. However, if I call multiple simxStart multiple times directly in a for loop, to each unique VREP instance through the main program directly, the clientIDs returned are valid (0, 1, 2... etc) and connectionID are all 0;

main.c:

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "sim.hpp"
#include <vector>

using namespace std;

vector<Sim> SimInstances;
char* serverAddress = (char*) "my_host_ip";

int main(){
	int instancenumber;
	int requiredinstancescount = 5;
	for(instancenumber = 0; instancenumber<requiredinstancescount; instancenumber++){
		SimInstances.push_back(Sim(instancenumber,serverAddress));
		cout << "Client ID of instance " << instancenumber << " is " << SimInstances[instancenumber].GetConnectionId() << "\n";
		cout << "Connection ID of instance " << instancenumber << " is " << SimInstances[instancenumber].GetClientId() << "\n";
	}
	return 0;
}
sim.hpp

Code: Select all

#ifndef _SIM_HPP_
#define _SIM_HPP_

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include <string>
#include <sstream>

using namespace std;

class Sim{
private:
	simxInt clientID;
	simxChar* connectionAddress;
	simxInt connectionPort;
	simxUChar waitUntilConnected;
	simxUChar doNotReconnectOnceDisconnected;
	simxInt timeOutInMs;
	simxInt commThreadCycleInMs;
	simxInt connectionID;

public:
	Sim();
	Sim(int instancenumber, char* serverAddress);
	~Sim();	
	int GetConnectionId();
};
#endif
sim.cpp

Code: Select all

#include "sim.hpp"

using namespace std;

#define STARTPORT 10001

Sim::Sim(){}

Sim::Sim(int instancenumber, char* serverAddress){
	
	clientID = -1;
	connectionPort = STARTPORT + instancenumber;
	waitUntilConnected = 1;
	doNotReconnectOnceDisconnected = 0;
	timeOutInMs = 5000;
	commThreadCycleInMs = 5;
	connectionAddress = serverAddress;
	connectionID = -1;

	clientID = simxStart(connectionAddress, connectionPort, waitUntilConnected, doNotReconnectOnceDisconnected, timeOutInMs, commThreadCycleInMs);
}

Sim::~Sim(){
	simxFinish(clientID);
}

int Sim::GetClientId(){
	return clientID;
}

int Sim::GetConnectionId(){
	connectionID =  simxGetConnectionId(clientID);
	return connectionID;
}
here's the code I got working using direct simxStart API call:

Code: Select all

void StartSimDirectAPI(){
int instancenumber;

	for(instancenumber = 0;instancenumber<requiredinstancescount; instancenumber++){

		//variable definitions
		simxInt clientID = -1;
		simxInt connectionPort = STARTPORT + instancenumber;
		simxUChar waitUntilConnected = 1;
		simxUChar doNotReconnectOnceDisconnected = 0;
		simxInt timeOutInMs = 5000;
		simxInt commThreadCycleInMs = 5;
		simxChar* connectionAddress = serverAddress;

		simxInt connectionID = -1;

		clientID =  simxStart(connectionAddress, connectionPort, waitUntilConnected, doNotReconnectOnceDisconnected, timeOutInMs, commThreadCycleInMs);
		
		cout << "Client ID : " << clientID << "\n";
		
		connectionID = simxGetConnectionId(clientID);
		cout << "Connection ID : " << connectionID << "\n";
		}
}


Thanks in advance!

coppelia
Site Admin
Posts: 6769
Joined: 14 Dec 2012, 00:25

Re: Calling simxStart through a c++ constructor fails

Post by coppelia » 04 Feb 2018, 16:39

Hello,

not sure, there must be a problem with how you create the sim objects...

Cheers

Rex
Posts: 15
Joined: 22 Jan 2018, 07:45

Re: Calling simxStart through a c++ constructor fails

Post by Rex » 12 Feb 2018, 05:04

Hi, I've managed to fix the problem. Turns out that if I push the object itself to a vector (as a copied object), the connection to the Sim will break. Instead, it works if I push a pointer of the original object to the vector.

Post Reply