| |
| Author |
Message |
< beginners ~ UDP receiving with ofxNetwork |
|
stephanschulz
|
Posted: Tue Nov 10, 2009 12:43 am |
|
|
| Joined: Fri Jul 10, 2009 5:33 pmPosts: 67Location: Montreal |
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.
|
|
Top
|
|
|
arturo
|
Posted: Tue Nov 10, 2009 12:44 pm |
|
|
| Site AdminJoined: Wed Apr 11, 2007 1:02 amPosts: 906Location: barcelona |
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: Code: char udpMessage[100000]; udpConnection.Receive(udpMessage,100000); string message=udpMessage; cout << message << endl;
|
|
Top
|
|
|
stephanschulz
|
Posted: Tue Nov 10, 2009 2:21 pm |
|
|
| Joined: Fri Jul 10, 2009 5:33 pmPosts: 67Location: Montreal |
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. Code: ret= recvfrom(m_hSocket, pBuff, iSize, 0, (sockaddr *)&saClient, &nLen); do you think this is still due to the wrong message format? stephan.
|
|
Top
|
|
|
arturo
|
Posted: Tue Nov 10, 2009 8:01 pm |
|
|
| Site AdminJoined: Wed Apr 11, 2007 1:02 amPosts: 906Location: barcelona |
mmh, no that means there's an error.
don't know what can be the problem. can you post some code.
|
|
Top
|
|
|
stephanschulz
|
Posted: Tue Nov 10, 2009 8:35 pm |
|
|
| Joined: Fri Jul 10, 2009 5:33 pmPosts: 67Location: Montreal |
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 Code: #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 Code: //-------------------------------------------------------------- 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); }
|
|
Top
|
|
|
arturo
|
Posted: Fri Nov 13, 2009 7:53 pm |
|
|
| Site AdminJoined: Wed Apr 11, 2007 1:02 amPosts: 906Location: barcelona |
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: Code: udpConnectionRx.SetNonBlocking(false);
....
int bytesReceived = udpConnectionRx.Receive(udpMessage,10); if(bytesReceived>0){ rxMessage[bytesReceived] = '\0'; rxMessage = udpMessage; }
|
|
Top
|
|
|
|
All times are UTC
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot post attachments in this forum
|
|
|