UDP receiving with ofxNetwork

Hello Forum.

I am sending and receiving UDP data from a WiShield connected to an Arduino.

I wrote a processing sketch using the hypermedia.net. library. Rx, Tx all works well.

But if I run the UDP example from oF than I am not able to receive any data from the WiShield. All is the same IP, port bindings and most of the code.

As a test I had the ofxNetwork UDP code running on too mac computers and they communicated fine with each other. this makes we think that there is a difference between how arduino(processing) work and oF (C++);

any idea?

thanks,
Stephan.

hi

the udp example is expecting a message with a particular format: points separated by [/p] and coordinates of the point separated by |, something like:

1|34[/p]45|5[/p]…

anything else will be discarded. the parsing of that message is done in the update method take a look at the code there. apart from that the conection and receiving should work without problem.

you should be able to test it by having in update something like:

  
char udpMessage[100000];  
udpConnection.Receive(udpMessage,100000);  
string message=udpMessage;  
cout << message << endl;  

1 Like

Hi Arturo.

My code looks just like the code you posted.

It is true i have not send messages in the particular formate you mentioned, but i send just a normal string from my udp iphone app to my mac running ofxnetworks and that worked fine.

this line here in the ofxUDPManager.cpp file returns -1 when i try to send stuff from the wishield to the ofx app.

  
ret= recvfrom(m_hSocket, pBuff,	iSize, 0, (sockaddr *)&saClient, &nLen);  

do you think this is still due to the wrong message format?

stephan.

mmh, no that means there’s an error.

don’t know what can be the problem. can you post some code.

here is my code.

i am using two objects one for tx and one for rx, otherwise upon receiving data from the iphone the app on my mac broke and was not able to send anymore.

testApp.h

  
#include <stdlib.h>   
#include <iostream.h>   
#include <string>   
  
#include "ofxNetwork.h"  
#include "ofMain.h"  
  
class testApp : public ofBaseApp{  
  
	public:  
  
		void setup();  
		void update();  
		void draw();  
  
		void keyPressed(int key);  
		void keyReleased(int key);  
		void mouseMoved(int x, int y );  
		void mouseDragged(int x, int y, int button);  
		void mousePressed(int x, int y, int button);  
		void mouseReleased(int x, int y, int button);  
		void windowResized(int w, int h);  
  
		ofxUDPManager udpConnectionRx;  
		ofxUDPManager udpConnectionTx;  
	  
		ofTrueTypeFont  mono;  
		ofTrueTypeFont  monosm;  
  
		vector<ofPoint> stroke;  
	  
	string rxMessage;  
	string txMessage;  
	  
	int myTimer;  
	int pauseIt;  
	int xVal,yVal;  
};  
  

testApp.cpp

  
//--------------------------------------------------------------  
void testApp::setup(){  
  
	// we don't want to be running to fast  
	ofSetVerticalSync(true);  
	ofSetFrameRate(60);  
  
	//load our type  
	mono.loadFont("type/mono.ttf",9);  
	monosm.loadFont("type/mono.ttf",8);  
  
    //create the socket and set to send to 127.0.0.1:11999  
	udpConnectionTx.Create();  
	udpConnectionRx.Create();  
	  
	udpConnectionTx.Connect("10.0.1.222",12344); // send data to ip via remote port # ...  
	udpConnectionTx.SetNonBlocking(true);  
  
	udpConnectionRx.Bind(2009); //incomming data on my port # ...  
	udpConnectionRx.SetNonBlocking(true);  
  
	pauseIt = 1;  
	myTimer = ofGetElapsedTimeMillis();  
}  
  
//--------------------------------------------------------------  
void testApp::update(){  
  
	char udpMessage[100000];  
	udpConnectionRx.Receive(udpMessage,100000);  
	  
	cout<<"udpMessage-"<<udpMessage<<"-"<<endl;  
  
	rxMessage = udpMessage;  
	//if(temp_message!=""){  
	if(rxMessage!=""){  
		cout<<"udpMessage-"<<udpMessage<<"-"<<endl;  
		rxMessage = udpMessage;  
		string myValAsString;  
  
		yVal = atoi(myValAsString.c_str());  
		printf("message received:                 %s, %i \n", rxMessage.c_str(),yVal);  
	}  
  
	if(ofGetElapsedTimeMillis() - myTimer > 450 && pauseIt == 0){  
		myTimer = ofGetElapsedTimeMillis();  
		  
		txMessage= "v"+ofToString(10000000 + mouseX);  
		//int sent = udpConnection.Send(message.c_str(),message.length());  
		if(pauseIt == 0){  
			udpConnectionTx.Send(txMessage.c_str(),txMessage.length());  
			printf("send: %s \n" , txMessage.c_str());  
		}  
	}  
	  
	xVal = xVal + 1;  
	if (xVal > ofGetWidth()){  
		stroke.clear();  
		xVal = 0;  
	}  
	stroke.push_back(ofPoint(xVal,yVal));  
}  
  
//--------------------------------------------------------------  
void testApp::draw(){  
  
	ofSetColor(20, 20, 20);  
	mono.drawString("openFrameworks UDP Send Example ", 15, 30);  
    monosm.drawString("drag to draw", 15, 50);  
	for(int i=1;i<stroke.size();i++){  
		ofLine(stroke[i-1].x,stroke[i-1].y,stroke[i].x,stroke[i].y);  
	}  
  
	char udpStr[255];  
	sprintf(udpStr, "sending = %s", txMessage.c_str());  
	monosm.drawString(udpStr, 15, ofGetHeight()-70);  
	  
	sprintf(udpStr, "receiving = %s", rxMessage.c_str());  
	monosm.drawString(udpStr, 15, ofGetHeight()-40);  
	  
	sprintf(udpStr, "pauseIt = %i", pauseIt);  
	monosm.drawString(udpStr, 15, ofGetHeight()-10);  
}  

hi sorry for late reply…

can you try with setting nonblocking to false and lowering the number of bytes to receive. also add a \0 after the last byte received:

  
udpConnectionRx.SetNonBlocking(false);  
  
....  
  
int bytesReceived = udpConnectionRx.Receive(udpMessage,10);  
if(bytesReceived>0){  
    rxMessage[bytesReceived] = '\0';  
    rxMessage = udpMessage;  
}  

was there ever a solution found to this? I’m having a similar issue receiving messages from the arduino wifi shield looks like the same return value of -1 from
ret= recvfrom(m_hSocket, pBuff, iSize, 0, (sockaddr *)&saClient, &nLen);

@arturo I tried setting nonblocking to false but that caused my app to not even run. it would crash while the dock icon was still bouncing even before the window would appear.

I got around it by waiting 3 seconds before starting to call receive so this is what my update looks like

void testApp::update(){
    
    if(ofGetElapsedTimeMillis() > 3000){
        char inMessage[20];
        
        udpClient.Receive(inMessage, 20);
        string message = inMessage;
        if(message != ""){
            printf("%s\n", message.c_str());
        }
    }

    
}

Still no luck or errors that i’m seeing.
Any Ideas?

1 Like