Revision d83f65fc applications/sliposc/serialtoosc.py
applications/sliposc/serialtoosc.py | ||
---|---|---|
6 | 6 |
# |
7 | 7 |
#TODO: OSC client as well |
8 | 8 |
#TODO: OSCQuery everything |
9 |
from IPython import embed |
|
9 | 10 |
|
10 | 11 |
# local packages |
11 | 12 |
import os, sys |
... | ... | |
15 | 16 |
package_path.append('SlipLib/src') |
16 | 17 |
sys.path.append('/'.join(package_path)) |
17 | 18 |
|
18 |
import sliplib |
|
19 |
|
|
20 | 19 |
import serial |
20 |
import sliplib |
|
21 | 21 |
import socket |
22 | 22 |
import socketserver |
23 |
#from pythonosc import udcp_client |
|
23 |
|
|
24 | 24 |
from time import sleep |
25 |
import threading |
|
25 |
#import threading
|
|
26 | 26 |
|
27 | 27 |
DEBUG = False |
28 | 28 |
|
29 | 29 |
if(len(sys.argv) > 1 and sys.argv[1] == '-v'): |
30 | 30 |
DEBUG = True |
31 | 31 |
|
32 |
UDP_HOST="127.0.0.1" |
|
33 |
UDP_PORTS=[1234, 57120, 57121, 57122]; |
|
32 |
TARGET_HOST="127.0.0.1" |
|
33 |
TARGET_PORTS=[1234, 57120, 57121, 57122]; |
|
34 |
TARGET_PORT=1234 |
|
34 | 35 |
|
35 | 36 |
#RECV_PORT=9999 |
36 | 37 |
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) |
37 |
#client = SimpleUDPClient(UDP_HOST,UDP_PORT) |
|
38 |
#sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # default socket |
|
39 |
#slipsocket = sliplib.SlipSocket(sock) |
|
40 |
#slipsocket.create_connection((TARGET_HOST, TARGET_PORT)) |
|
38 | 41 |
|
39 | 42 |
#TODO:autodetect serialport |
40 | 43 |
ser = serial.Serial('/dev/ttyACM0',9600) |
41 |
messages = [] |
|
42 | 44 |
drv = sliplib.Driver() |
45 |
messages = [] |
|
43 | 46 |
|
44 |
#class MessageHandler(socketserver.BaseRequestHandler): |
|
45 |
# def handle(self): |
|
46 |
# data=self.request[0] |
|
47 |
# socket=self.request[1] |
|
48 |
# ser.write(drv.send(data)) |
|
49 |
# print(drv.send(data)) |
|
50 |
# |
|
51 |
#server= socketserver.UDPServer(('',RECV_PORT),MessageHandler) |
|
52 |
#server_thread=threading.Thread(target=server.serve_forever) |
|
53 |
#server_thread.daemon=True |
|
54 |
#server_thread.start() |
|
55 |
|
|
56 |
#import subprocess, shlex |
|
57 |
#sc_port_cmd = "lsof -p `pidof sclang`|grep UDP|awk '{ print $9}'|cut -d: -f2" |
|
58 |
#sp = subprocess.Popen(sc_port_cmd, stdout=subprocess.PIPE, shell=True) |
|
59 |
#print(sp.returncode) |
|
60 |
|
|
47 |
#embed() |
|
61 | 48 |
while True: |
49 |
# slipsocket.driver |
|
62 | 50 |
#TODO: handle drv errors on improper packet |
63 | 51 |
messages = drv.receive(ser.read(ser.inWaiting())) |
64 | 52 |
|
65 | 53 |
if DEBUG: |
66 | 54 |
print(len(messages), " messages in read") |
67 | 55 |
|
56 |
# embed() |
|
68 | 57 |
for msg in messages: |
58 |
if DEBUG: |
|
59 |
print(len, msg) |
|
69 | 60 |
try: |
70 |
dec = sliplib.decode(msg) |
|
71 |
if DEBUG: |
|
72 |
print(len(msg), dec) |
|
73 |
if sliplib.is_valid(msg): |
|
74 |
for port in UDP_PORTS: |
|
75 |
sock.sendto(msg, (UDP_HOST, port)) |
|
61 |
if not sliplib.is_valid(msg): |
|
62 |
for port in TARGET_PORTS: |
|
63 |
sock.sendto(msg, (TARGET_HOST, port)) |
|
64 |
|
|
76 | 65 |
except sliplib.ProtocolError: |
77 | 66 |
if DEBUG: |
78 | 67 |
print ("Invalid Packet received") |
79 | 68 |
|
80 | 69 |
print("---"); |
81 |
sleep(1) |
|
82 |
#sleep(0.005) |
|
83 |
#server.shutdown() |
|
84 |
#server.server_close() |
|
70 |
sleep(0.1) |
|
71 |
slipsocket.close() |
|
85 | 72 |
sock.close() |
73 |
|
|
74 |
#import subprocess, shlex |
|
75 |
#sc_port_cmd = "lsof -p `pidof sclang`|grep UDP|awk '{ print $9}'|cut -d: -f2" |
|
76 |
#sp = subprocess.Popen(sc_port_cmd, stdout=subprocess.PIPE, shell=True) |
|
77 |
#print(sp.returncode) |
|
78 |
|
Also available in: Unified diff