brix5 / applications / sliposc / serialtoosc.py @ 5ef3477f
History | View | Annotate | Download (1.378 KB)
1 |
#!/usr/bin/env python3
|
---|---|
2 |
#v 0.1
|
3 |
#dumb router
|
4 |
#echos serial from serial port to a udp socket
|
5 |
#
|
6 |
#TODO: OSC client as well
|
7 |
#TODO: OSCQuery everything
|
8 |
|
9 |
# local packages
|
10 |
import os, sys |
11 |
path = os.path.realpath(__file__).split('/')
|
12 |
package_path = path[:-1]
|
13 |
#package_path.append('python-packages')
|
14 |
package_path.append('SlipLib/src')
|
15 |
sys.path.append('/'.join(package_path))
|
16 |
|
17 |
from sliplib import Driver |
18 |
|
19 |
import serial |
20 |
import socket |
21 |
import socketserver |
22 |
#from pythonosc import udcp_client
|
23 |
from time import sleep |
24 |
import threading |
25 |
|
26 |
UDP_HOST="127.0.0.1"
|
27 |
UDP_PORT=1234
|
28 |
#RECV_PORT=9999
|
29 |
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) |
30 |
#client = SimpleUDPClient(UDP_HOST,UDP_PORT)
|
31 |
|
32 |
#TODO:autodetect serialport
|
33 |
ser = serial.Serial('/dev/ttyACM0',9600) |
34 |
messages = [] |
35 |
drv = Driver() |
36 |
|
37 |
#class MessageHandler(socketserver.BaseRequestHandler):
|
38 |
# def handle(self):
|
39 |
# data=self.request[0]
|
40 |
# socket=self.request[1]
|
41 |
# ser.write(drv.send(data))
|
42 |
# print(drv.send(data))
|
43 |
#
|
44 |
#server= socketserver.UDPServer(('',RECV_PORT),MessageHandler)
|
45 |
#server_thread=threading.Thread(target=server.serve_forever)
|
46 |
#server_thread.daemon=True
|
47 |
#server_thread.start()
|
48 |
|
49 |
while True: |
50 |
sleep(0.005)
|
51 |
#TODO: handle drv errors on improper packet
|
52 |
messages = drv.receive(ser.read(ser.inWaiting())) |
53 |
for msg in messages: |
54 |
print(msg) |
55 |
sock.sendto(msg, (UDP_HOST,UDP_PORT)) |
56 |
#server.shutdown()
|
57 |
#server.server_close()
|
58 |
sock.close() |