Statistics
| Branch: | Revision:

brix5 / applications / sliposc / serialtoosc.py @ d83f65fc

History | View | Annotate | Download (1.758 KB)

1 5ef3477f Jan
#!/usr/bin/env python3
2 edc6c2e7 Jan
#vim:ts=2:sw=2:expandtab:autoindent
3 5ef3477f Jan
#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 d83f65fc Jan
from IPython import embed
10 5ef3477f Jan
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 d83f65fc Jan
import sliplib
21 5ef3477f Jan
import socket
22
import socketserver
23 d83f65fc Jan
24 5ef3477f Jan
from time import sleep
25 d83f65fc Jan
#import threading
26 5ef3477f Jan
27 edc6c2e7 Jan
DEBUG = False
28
29
if(len(sys.argv) > 1 and sys.argv[1] == '-v'):
30
    DEBUG = True
31
32 d83f65fc Jan
TARGET_HOST="127.0.0.1"
33
TARGET_PORTS=[1234, 57120, 57121, 57122];
34
TARGET_PORT=1234
35 edc6c2e7 Jan
36 5ef3477f Jan
#RECV_PORT=9999
37
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
38 d83f65fc Jan
#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 5ef3477f Jan
42
#TODO:autodetect serialport
43
ser = serial.Serial('/dev/ttyACM0',9600)
44 edc6c2e7 Jan
drv = sliplib.Driver()
45 d83f65fc Jan
messages = []
46 5ef3477f Jan
47 d83f65fc Jan
#embed()
48 5ef3477f Jan
while True:
49 d83f65fc Jan
#  slipsocket.driver
50 5ef3477f Jan
  #TODO: handle drv errors on improper packet
51
  messages = drv.receive(ser.read(ser.inWaiting()))
52 edc6c2e7 Jan
53
  if DEBUG:
54
    print(len(messages), " messages in read")
55
56 d83f65fc Jan
#  embed()
57 5ef3477f Jan
  for msg in messages:
58 d83f65fc Jan
    if DEBUG:
59
      print(len, msg)
60 edc6c2e7 Jan
    try:
61 d83f65fc Jan
      if not sliplib.is_valid(msg):
62
        for port in TARGET_PORTS:
63
          sock.sendto(msg, (TARGET_HOST, port))
64
65 edc6c2e7 Jan
    except sliplib.ProtocolError:
66
      if DEBUG:
67
        print ("Invalid Packet received")
68
69
  print("---");
70 d83f65fc Jan
  sleep(0.1)
71
slipsocket.close()
72 5ef3477f Jan
sock.close()
73 d83f65fc Jan
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)