Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you send a message using pyOSC?

Tags:

python

I'm very new to python and even less familiar with pyOSC. Can someone post a simple example of how to send a string message from my computer to another computer? I viewed this pyOSC link which has given me some guidance but I am uncertain why the addressSet() takes "/startup". Is this the function that receives the message on the other end or is it something else?

I greatly appreciate any guidance you can provide!

like image 369
hlupico Avatar asked Dec 15 '22 11:12

hlupico


1 Answers

OSC has the concept of an message address that is distinct from the network address of the device to which you're connecting. The idea is that the message you are sending could be routed to one of many different handlers at the other end of the network connection. Each handler has its own address which is usually designated with a '/' prefix.

Using the same client code from the question you referenced:

import OSC

c = OSC.OSCClient()
c.connect(('127.0.0.1', 57120))   # localhost, port 57120
oscmsg = OSC.OSCMessage()
oscmsg.setAddress("/startup")
oscmsg.append('HELLO')
c.send(oscmsg)

First, run this server code:

import OSC

def handler(addr, tags, data, client_address):
    txt = "OSCMessage '%s' from %s: " % (addr, client_address)
    txt += str(data)
    print(txt)

if __name__ == "__main__":
    s = OSC.OSCServer(('127.0.0.1', 57120))  # listen on localhost, port 57120
    s.addMsgHandler('/startup', handler)     # call handler() for OSC messages received with the /startup address
    s.serve_forever()

Then run the client from a different terminal. On the server side you should get something like:

OSCMessage '/startup' from ('127.0.0.1', 55018): ['HELLO']

The best way to get more insight into how to use pyOSC is to just read the source. It's all in OSC.py and not too long. There is a test method at the bottom of the file that gives a fairly detailed examples of how to use most, if not all, of the protocol functionality.

like image 133
labradore Avatar answered Jan 02 '23 09:01

labradore