I'm trying to receive a message sent over OSC from Pure Data (or Max/MSP) to MATLAB.
- I'm able to send OSC messages from Pure Data to Pure Data
- I can send messages from MATLAB to MATLAB
- I can even send messages from MATLAB to Pure Data
- ...I'm really struggling to get Pure Data to talk back to MATLAB
Here's my code that sends messages from MATLAB (I'm using the oscmex protocol):
host = 'localhost'; % local host UDP address
sendPort = 3333; % UDP port number to send over
receivePort = 3333; % UDP port number to receive from
oscAddress = osc_new_address(host, sendPort); % open send address
oscServer = osc_new_server(receivePort); % open server
dataPacket = struct('path','/foo','tt','f','data',{num2cell([1.0])}); % create packet
osc_send(oscAddress, dataPacket); % write packet to OSC
oscMessage = osc_recv(oscServer, 0.1); % listen for packet on OSC
% check to see if anything is there...
if length(oscMessage) > 0
fprintf('Found something!')
else
fprintf('Failed to find anything')
end
osc_free_address(oscAddress);
osc_free_server(oscServer);
If I send using host 'localhost', everything works fine sending from MATLAB to MATLAB using the code above. If I set it to '127.0.0.1', MATLAB sends to Pure Data, but MATLAB then can't receive its own messages.
Now for the other end of things. Here's my Pure Data patch:
Again, running the above patch alone successfully sends and receives messages through Pure Data.
The problem lies when I try to talk from one program to another. If I set things so that MATLAB is sending on port 3333 and Pure Data is receiving on 3333, and that Pure Data is sending on 2222 and MATLAB is receiving on 2222, I can make Pure Data receive if MATLAB's host is '127.0.0.1'. But, with '127.0.0.1', MATLAB can't send to itself.
In any case, no matter what I try, I'm unable to make Pure Data send to MATLAB, despite being able to get it to send to itself. I suspect it has something to do with the 'host' address.
My actual IPv4 address (found using 'ipconfig' of the MS command prompt) is completely different from 127.0.0.1, and using the value specified here doesn't seem to make things work any better.
I'm aware that I can't have more than one OSC server with the same port open at any one time and so my current attempt at a solution involves sending from MATLAB on one port, and sending from Pure Data on another, with only a single server open at one time on either port.
Note I'm also aware that I use /foo
for messages from MATLAB and /test
from Pure Data. However, my MATLAB code indiscriminately receives everything sent over OSC, so this makes no difference.
Any help getting PD to talk to MATLAB would be appreciated.
Update: Ive solved the 'localhost' issue and that doesn't seem to fix things (i had to add localhost to my Windows 'hosts' file). So, I may have been barking up the wrong tree by worrying about the localhost thing. But, I still can't get PD to talk to MATLAB.
Update #2: Amro has posted an elegant solution below and I still can't get MATLAB to receive messages from Pure Data. I've installed CloseTheDoor to monitor my UDP connections and notice that when MATLAB sets up a server, it uses the 'Interface' [::0]
, whereas PD sets uses 'Interface' 0.0.0.0
. Since PureData is the one that successfully receives messages, perhaps I need to make MATLAB listen on 0.0.0.0
as well?
'localhost'
as the host name? This appears to be the name used withudpsend
in your diagram, and the one that was working for MATLAB -> MATLAB communication. Not that it should make a difference, but worth a try... – Storage'localhost'
is the only host name (in MATLAB) that allows communication with MATLAB. However, only'127.0.0.1'
allows communication from MATLAB to Pure Data. Neither (set in the Pure Data patch) allows communication from Pure Data to MATLAB – Hinchlocalhost thing
is another problem, because localhost just is the usual DNS name for 127.0.0.1. One or another should work (some software might use 127.0.0.1 as a name instead of IP so this could fail as well) - but if you've tried both there's another problem. I developed a software myself that talks to Pure Data via OSC - unfortunately the Pure Data side was not my part of the job so I cannot tell exactly if it behaves any special. But I know it can work ;-) – Grimsleyhosts
file. I've tested my code in R2013a running on WinXP 32-bit, and it works just fine... Note that the "0.0.0.0" address means that the server is listening on all interfaces. The double-colon address is IPv6 notation I think, could this be a IPv4 vs IPv6 issue? Usually::1
is the same as127.0.0.1
, while::0
means0.0.0.0
. – Heartless#ifdef ENABLE_IPV6
macros in the source). As far as I can tell, the OSC-MEX binaries provided were compiled against liblo v0.22 if that makes a difference. btw you never mentioned the bitness of your OS and your MATLAB version that you running (32-bit vs 64-bit), that could be a factor. – Heartlessliblo
page about it. So I would stick with IPv4 for now.. Perhaps you can temporarily disable it: informationweek.com/byte/personal-tech/… – Heartless