sending udp packets in format osc (Open sound control) using pnet in Matlab R2012a

StackOverflow https://stackoverflow.com/questions/15274533

  •  18-03-2022
  •  | 
  •  

Frage

I have a question concerning udp packets in MATLAB. I've been using the oscsend.m script to send udp packets to other applications. http://www.mathworks.fr/matlabcentral/fileexchange/31400-send-open-sound-control-osc-messages/content/oscsend.m It works normaly. however, now I need to make use of the pnet function from the TCP/UDP/IP Toolbox 2.0.6 http://www.mathworks.de/matlabcentral/fileexchange/345-tcpudpip-toolbox-2-0-6 because I no longer have access to the instrument control toolbox in Matlab, which oscsend makes use of.

I looked up online for answers and even contacted the authors with no luck so far. Even though the problem seems trivial I couldn't make it work.

normally the usage with the Instrument Control toolbox would be:

u = udp('127.0.0.1', 12345) %12345 being the port
fopen(u)
oscsend(u, /test, 'f', 1.05) %"f" indicating a floating number 

but now without udp or fopen I tried to use pnet like this

%write data to UDP
data = [oscstr(path) types data];
sock=pnet('udpsocket',12345) %it returns 0, a sign that the socket is working
%however when I try to send the oscsend signal through this socket nothing happens
pnet(sock, 'writepacket' data) %data being the output of oscsend 

I also tried with no luck integrating the pnet function to oscsend but I couldn't recieve a signal, (I am working with another instance of MATLAB which is recieving the data sent from the previoulsy mentioned port). This is what I did inside oscsend and none of them worked

%write data to UDP
data = [oscstr(path) types data];
%pnet(u, 'writepacket', data, '127.0.0.1', 12345 );%
%pnet u 'write' data %
%pnet(u,'write',data, '127.0.0.1', 12345 )
%pnet(u,'write',data, '127.0.0.1', 12345 ) %returned value?
%pnet(0,'write',data, '127.0.0.1', 12345 ) %
%sock = pnet('udpsocket',12345 ); %
%pnet(sock,'write', data, '127.0.0.1', 12345 )

Thanks to all of you in advance who took the time to read this. Best. Mario.

War es hilfreich?

Lösung 3

Thanks to my supervisor here is the code if you ever need to send udps via oscsend.m using pnet http://www.mathworks.fr/matlabcentral/fileexchange/31400-send-open-sound-control-osc-messages/content/oscsend.m just add the following code at the end of oscsend.m

%write data to UDP
data = [oscstr(path) types data];
pnet(u, 'write', data)
pnet(u, 'writepacket', '127.0.0.1', 12345); %127.0.0.1 being the IP and 12345 the port 

Andere Tipps

It's not clear to me what UDP port you want to send your data to, and what sort of process you're using to monitor and test your code. There are two ports involved... One is the local port that you're binding to (that's the argument to 'udpsocket'), and the other port is the destination port of the packet, given along with the destination host.

It appears to me that the Instrument Control Toolbox syntax requires the destination hostname and port during creation. It allows you to specify the local port as an optional argument if you want to. In constrast, pnet('udpsocket') takes the local port. You should notice in your first set of code that nothing specifies the destination host, which should make you suspicious... If you read on in the UDP docs of pnet.m, you'll see that there's another function: pnet(sock, 'udpconnect', 'hostname', port), which "connects" the UDP socket to the host/port pair, so that you can leave out the hostname/port when doing writepacket.

So here is what I think the equivalent is of your original Instrument Control Toolbox code:

sock=pnet('udpsocket',1237);  % Does local port matter?  You haven't said...
pnet(sock, 'udpconnect', '127.0.0.1', 12345); % Destination port
pnet(sock, 'writepacket', data);

You should ALWAYS use 'writepacket' for UDP, never 'write'. And note that you can either do 'udpconnect' as I've listed above, OR you can supply hostname/port with every writepacket, as you were trying to do.

What I can't figure out from your question is how 6351 enters in, as that never showed up in your reference code.

btw, there is also oscmex, a library based on liblo that allows you to send/receive OSC messages directly from/within matlab.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top