brix5 / applications / sliposc / serialtoosc.py @ edc6c2e7
History | View | Annotate | Download (2.019 KB)
1 |
#!/usr/bin/env python3
|
---|---|
2 |
#vim:ts=2:sw=2:expandtab:autoindent
|
3 |
#v 0.1
|
4 |
#dumb router
|
5 |
#echos serial from serial port to a udp socket
|
6 |
#
|
7 |
#TODO: OSC client as well
|
8 |
#TODO: OSCQuery everything
|
9 |
|
10 |
# local packages
|
11 |
import os, sys |
12 |
path = os.path.realpath(__file__).split('/')
|
13 |
package_path = path[:-1]
|
14 |
#package_path.append('python-packages')
|
15 |
package_path.append('SlipLib/src')
|
16 |
sys.path.append('/'.join(package_path))
|
17 |
|
18 |
import sliplib |
19 |
|
20 |
import serial |
21 |
import socket |
22 |
import socketserver |
23 |
#from pythonosc import udcp_client
|
24 |
from time import sleep |
25 |
import threading |
26 |
|
27 |
DEBUG = False
|
28 |
|
29 |
if(len(sys.argv) > 1 and sys.argv[1] == '-v'): |
30 |
DEBUG = True
|
31 |
|
32 |
UDP_HOST="127.0.0.1"
|
33 |
UDP_PORTS=[1234, 57120, 57121, 57122]; |
34 |
|
35 |
#RECV_PORT=9999
|
36 |
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) |
37 |
#client = SimpleUDPClient(UDP_HOST,UDP_PORT)
|
38 |
|
39 |
#TODO:autodetect serialport
|
40 |
ser = serial.Serial('/dev/ttyACM0',9600) |
41 |
messages = [] |
42 |
drv = sliplib.Driver() |
43 |
|
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 |
|
61 |
while True: |
62 |
#TODO: handle drv errors on improper packet
|
63 |
messages = drv.receive(ser.read(ser.inWaiting())) |
64 |
|
65 |
if DEBUG:
|
66 |
print(len(messages), " messages in read") |
67 |
|
68 |
for msg in messages: |
69 |
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)) |
76 |
except sliplib.ProtocolError:
|
77 |
if DEBUG:
|
78 |
print ("Invalid Packet received") |
79 |
|
80 |
print("---");
|
81 |
sleep(1)
|
82 |
#sleep(0.005)
|
83 |
#server.shutdown()
|
84 |
#server.server_close()
|
85 |
sock.close() |