simple question : how to make a pause ?

Hello,

I send command over the network and the protocol specifies that the server answer is available in 2 seconds max.
What is the best way to handle this ?

Wait for 2 seconds before reading the answer ?

Polling the answer ?

In both case, how do you code it ?

thanks

I’m not sure I fully understand the scenario, but you might try using the ofxJSON of similar add-on that can be used in conjunction with an HTTP request, and write something like the following. With this protocol, it shouldn’t matter if it takes up 2 seconds max for the server to return the request…

//in your setup loop

string url ="http://yourseveraddress/yourcustomformattedhttprequest";

parsed = json.open(url); // parsed is a bool you would declare in the project header

//in your update loop, something like this:

if(parsed) {

for (int n = 0; n < json.size(); n++) {
ofxJSONElement created_at = json[n][“created_at”];
ofxJSONElement id = json[n][“id”];
ofxJSONElement tweet = json[n][“tweet”];
ofxJSONElement username = json[n][“username”];
string created_at_String = created_at.getRawString(true);
string id_String = id.getRawString(true);
string tweet_String = tweet.getRawString(true);
string username_String = username.getRawString(true);
}
}

Thanks for the tip !
I don’t think i understand the exact purpose of all this but i will make some research. And i don’t understand why with this protocol i shouldn’t matter anymore…

are you used to ethernet connexions with openframeworks ?

here is some code i wrote :

  
  
void testApp::testConnection()  
{  
	string msgRx;  
	  
	if(!pjClient.isConnected()) {  
		bool connected = pjClient.setup("192.168.5.225", 4352);  
			if(pjClient.isConnected()) {  
				cout << "connection established: 192.168.5.225 on port 4352" << endl;  
				while (msgRx.length() < 8)  
				{  
					msgRx = pjClient.receiveRaw();  
					if(msgRx.length() > 6)  
					{  
						cout<< msgRx << endl;  
					}  
				}  
				cout << "sending command: %1POWR ?" << endl;  
				string command = "%1POWR ?\r";  
				pjClient.sendRaw(command);  
				msgRx = "";  
				for(int i = 0; i<200000000 ; i++){} // Empty loop for pause  
				while (msgRx.length() < 8)  
				{  
					msgRx = pjClient.receiveRaw();  
					cout<< msgRx << endl;  
				}  
			}  
			else {  
				cout << "Problem connecting to 192.168.5.225 on port 4352" << endl;  
			}  
	}  
	  
	pjClient.close();  
}  
  

The code I gave you depends on writing a server-side application to manage communication between an Openframenworks app and a remote or onsite server, so yes, through ethernet you might say. But with that you would need to follow an HTTP request protocol ( http://en.wikipedia.org/wiki/Hypertext-Transfer-Protocol ). That protocol shouldn’t present issues if it takes two seconds for your server side app to return the results–you can just parse the data appropriated whenever it is returned. The code that you have sent it looks like you are doing something similar–connecting to a server, setting up a way to receive data, and then generating a specific request to get what you need. I don’t know what the server-side application you’ve written looks like, nor is that app’s structure in the scope of this forum, but I’m merely suggesting an alternative if the 2 second delay is a problem. Maybe my solution is quicker for you? I don’t know. You can try it or not. That’s about extent to the help that I can provide here. Good luck!

Ok, thanks for the suggestion.
But i don’t think this help for my purpose as i am on the client side and try to connect to embedded TCP server.
The server is a video projector to which i would like to send commands such as Power on/off.

For the record, protocol is PJLINK : http://pjlink.jbmia.or.jp/english/

thanks