2016年5月19日 星期四

Raspberry Pi: OSC for Python3

since: 2016/05/19
update: 2016/05/19
reference:
1. Osc4py3 documentation

A. 安裝 OSC for Python3
    $ sudo pip3 install osc4py3

-----------------------------------------------------------------------------------------------

B. OSC 接收端 (client)
     // osc_client.py

#!/usr/bin/python3

# Import needed modules from osc4py3
from osc4py3.as_eventloop import *
from osc4py3 import oscmethod as osm

print('start osc client....'+'\n')

def osc_receive_function(address, s, x, y):
    # Will receive message address, and message data flattened in s, x, y
    print("osc client receive:")
    print('address = '+address)
    print('s = ' + s)
    print('x = ' + str(x))
    print('y = ' + str(y))

# Start the system.
osc_startup()

# Make server channels to receive packets.
osc_udp_server("192.168.1.17", 2781, "aservername")

# Associate Python functions with message address patterns
osc_method("/test/*", osc_receive_function, argscheme=osm.OSCARG_ADDRESS + osm.OSCARG_DATAUNPACK)

# Periodically call osc4py3 processing method in your event loop.
finished = False
while not finished:
    # …
    osc_process()
    # …

# Properly close the system.
osc_terminate()


-----------------------------------------------------------------------------------------------

C. OSC 發送端 (server)
     // osc_server.py

#!/usr/bin/python3

# Import needed modules from osc4py3
from osc4py3.as_eventloop import *
from osc4py3 import oscbuildparse

print('start osc server....'+ '\n')
# Start the system.
osc_startup()

# Make client channels to send packets.
osc_udp_client("192.168.1.17", 2781, "aclientname")

# Build a simple message and send it.
s = 'text'
x = 672
y = 8.871
address = '/test/me'

msg = oscbuildparse.OSCMessage(address, ",sif", [s, x, y])
osc_send(msg, "aclientname")

print("osc server send:")
print('address = ' + address)
print('s = ' + s)
print('x = ' + str(x))
print('y = ' + str(y))

# Periodically call osc4py3 processing method in your event loop.
finished = False
while not finished:
    # You can send OSC messages from your event loop too…
    # …

    osc_process()
    # …
    finished = True # finish the process

# Properly close the system.
osc_terminate()

-----------------------------------------------------------------------------------------------

D. 測試:
     1. 啟動 OSC client:
         $ ./osc_client.py
         start osc client....

     2. 啟動 OSC server:
         $ ./osc_server.py
         start osc server....

         osc server send:
         address = /test/me
         s = text
         x = 672
         y = 8.871


     3. 查看 OSC client:
         start osc client....

         osc client receive:
         address = /test/me
         s = text
         x = 672
         y = 8.871000289916992

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。