Revision edc6c2e7
applications/sliposc/serialtoosc.py | ||
---|---|---|
1 | 1 |
#!/usr/bin/env python3 |
2 |
#vim:ts=2:sw=2:expandtab:autoindent |
|
2 | 3 |
#v 0.1 |
3 | 4 |
#dumb router |
4 | 5 |
#echos serial from serial port to a udp socket |
... | ... | |
14 | 15 |
package_path.append('SlipLib/src') |
15 | 16 |
sys.path.append('/'.join(package_path)) |
16 | 17 |
|
17 |
from sliplib import Driver
|
|
18 |
import sliplib
|
|
18 | 19 |
|
19 | 20 |
import serial |
20 | 21 |
import socket |
... | ... | |
23 | 24 |
from time import sleep |
24 | 25 |
import threading |
25 | 26 |
|
27 |
DEBUG = False |
|
28 |
|
|
29 |
if(len(sys.argv) > 1 and sys.argv[1] == '-v'): |
|
30 |
DEBUG = True |
|
31 |
|
|
26 | 32 |
UDP_HOST="127.0.0.1" |
27 |
UDP_PORT=1234 |
|
33 |
UDP_PORTS=[1234, 57120, 57121, 57122]; |
|
34 |
|
|
28 | 35 |
#RECV_PORT=9999 |
29 | 36 |
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) |
30 | 37 |
#client = SimpleUDPClient(UDP_HOST,UDP_PORT) |
... | ... | |
32 | 39 |
#TODO:autodetect serialport |
33 | 40 |
ser = serial.Serial('/dev/ttyACM0',9600) |
34 | 41 |
messages = [] |
35 |
drv = Driver() |
|
42 |
drv = sliplib.Driver()
|
|
36 | 43 |
|
37 | 44 |
#class MessageHandler(socketserver.BaseRequestHandler): |
38 | 45 |
# def handle(self): |
... | ... | |
46 | 53 |
#server_thread.daemon=True |
47 | 54 |
#server_thread.start() |
48 | 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 |
|
|
49 | 61 |
while True: |
50 |
sleep(0.005) |
|
51 | 62 |
#TODO: handle drv errors on improper packet |
52 | 63 |
messages = drv.receive(ser.read(ser.inWaiting())) |
64 |
|
|
65 |
if DEBUG: |
|
66 |
print(len(messages), " messages in read") |
|
67 |
|
|
53 | 68 |
for msg in messages: |
54 |
print(msg) |
|
55 |
sock.sendto(msg, (UDP_HOST,UDP_PORT)) |
|
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) |
|
56 | 83 |
#server.shutdown() |
57 | 84 |
#server.server_close() |
58 | 85 |
sock.close() |
Also available in: Unified diff