brix5 / applications / sliposc / serialtoosc.py @ d83f65fc
History | View | Annotate | Download (1.758 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 |
from IPython import embed |
| 10 |
|
| 11 |
# local packages
|
| 12 |
import os, sys |
| 13 |
path = os.path.realpath(__file__).split('/')
|
| 14 |
package_path = path[:-1]
|
| 15 |
#package_path.append('python-packages')
|
| 16 |
package_path.append('SlipLib/src')
|
| 17 |
sys.path.append('/'.join(package_path))
|
| 18 |
|
| 19 |
import serial |
| 20 |
import sliplib |
| 21 |
import socket |
| 22 |
import socketserver |
| 23 |
|
| 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 |
TARGET_HOST="127.0.0.1"
|
| 33 |
TARGET_PORTS=[1234, 57120, 57121, 57122]; |
| 34 |
TARGET_PORT=1234
|
| 35 |
|
| 36 |
#RECV_PORT=9999
|
| 37 |
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) |
| 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))
|
| 41 |
|
| 42 |
#TODO:autodetect serialport
|
| 43 |
ser = serial.Serial('/dev/ttyACM0',9600) |
| 44 |
drv = sliplib.Driver() |
| 45 |
messages = [] |
| 46 |
|
| 47 |
#embed()
|
| 48 |
while True: |
| 49 |
# slipsocket.driver
|
| 50 |
#TODO: handle drv errors on improper packet
|
| 51 |
messages = drv.receive(ser.read(ser.inWaiting())) |
| 52 |
|
| 53 |
if DEBUG:
|
| 54 |
print(len(messages), " messages in read") |
| 55 |
|
| 56 |
# embed()
|
| 57 |
for msg in messages: |
| 58 |
if DEBUG:
|
| 59 |
print(len, msg)
|
| 60 |
try:
|
| 61 |
if not sliplib.is_valid(msg): |
| 62 |
for port in TARGET_PORTS: |
| 63 |
sock.sendto(msg, (TARGET_HOST, port)) |
| 64 |
|
| 65 |
except sliplib.ProtocolError:
|
| 66 |
if DEBUG:
|
| 67 |
print ("Invalid Packet received") |
| 68 |
|
| 69 |
print("---");
|
| 70 |
sleep(0.1)
|
| 71 |
slipsocket.close() |
| 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 |
|