brix5 / applications / sliposc / serialtoosc.py @ edc6c2e7
History | View | Annotate | Download (2.019 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 | |||
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 | edc6c2e7 | Jan | import sliplib |
19 | 5ef3477f | Jan | |
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 | edc6c2e7 | Jan | DEBUG = False
|
28 | |||
29 | if(len(sys.argv) > 1 and sys.argv[1] == '-v'): |
||
30 | DEBUG = True
|
||
31 | |||
32 | 5ef3477f | Jan | UDP_HOST="127.0.0.1"
|
33 | edc6c2e7 | Jan | UDP_PORTS=[1234, 57120, 57121, 57122]; |
34 | |||
35 | 5ef3477f | Jan | #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 | edc6c2e7 | Jan | drv = sliplib.Driver() |
43 | 5ef3477f | Jan | |
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 | edc6c2e7 | Jan | #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 | 5ef3477f | Jan | while True: |
62 | #TODO: handle drv errors on improper packet
|
||
63 | messages = drv.receive(ser.read(ser.inWaiting())) |
||
64 | edc6c2e7 | Jan | |
65 | if DEBUG:
|
||
66 | print(len(messages), " messages in read") |
||
67 | |||
68 | 5ef3477f | Jan | for msg in messages: |
69 | edc6c2e7 | Jan | 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 | 5ef3477f | Jan | #server.shutdown()
|
84 | #server.server_close()
|
||
85 | sock.close() |