mirror of
https://github.com/daniviga/django-ram.git
synced 2025-08-04 13:17:50 +02:00
Improve net-to-serial daemon
This commit is contained in:
@@ -4,7 +4,10 @@ ListeningIP = 0.0.0.0
|
|||||||
ListeningPort = 2560
|
ListeningPort = 2560
|
||||||
|
|
||||||
[Serial]
|
[Serial]
|
||||||
|
# UNO
|
||||||
Port = /dev/ttyACM0
|
Port = /dev/ttyACM0
|
||||||
|
# Mega WiFi
|
||||||
|
# Port = /dev/ttyUSB0
|
||||||
Baudrate = 115200
|
Baudrate = 115200
|
||||||
# Timeout in milliseconds
|
# Timeout in milliseconds
|
||||||
Timeout = 50
|
Timeout = 50
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
import time
|
||||||
import logging
|
import logging
|
||||||
import serial
|
import serial
|
||||||
import asyncio
|
import asyncio
|
||||||
@@ -20,7 +21,27 @@ class SerialDaemon:
|
|||||||
except AttributeError:
|
except AttributeError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
def __read_serial(self):
|
||||||
|
"""Serial reader wrapper"""
|
||||||
|
response = b""
|
||||||
|
while True:
|
||||||
|
line = self.ser.read_until()
|
||||||
|
if not line.strip(): # empty line
|
||||||
|
break
|
||||||
|
if line.decode().startswith("<*"):
|
||||||
|
logging.debug("Serial debug: {}".format(line))
|
||||||
|
else:
|
||||||
|
response += line
|
||||||
|
logging.debug("Serial read: {}".format(response))
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
|
def __write_serial(self, data):
|
||||||
|
"""Serial writer wrapper"""
|
||||||
|
self.ser.write(data)
|
||||||
|
|
||||||
async def handle_echo(self, reader, writer):
|
async def handle_echo(self, reader, writer):
|
||||||
|
"""Process a request from socket and return the response"""
|
||||||
while 1: # keep connection to client open
|
while 1: # keep connection to client open
|
||||||
data = await reader.read(100)
|
data = await reader.read(100)
|
||||||
if not data: # client has disconnected
|
if not data: # client has disconnected
|
||||||
@@ -29,25 +50,23 @@ class SerialDaemon:
|
|||||||
addr = writer.get_extra_info('peername')
|
addr = writer.get_extra_info('peername')
|
||||||
logging.info("Received {} from {}".format(data, addr[0]))
|
logging.info("Received {} from {}".format(data, addr[0]))
|
||||||
|
|
||||||
self.ser.write(data)
|
self.__write_serial(data)
|
||||||
response = line = self.ser.read()
|
response = self.__read_serial()
|
||||||
while line.strip():
|
|
||||||
line = self.ser.read_until()
|
|
||||||
if line.decode().startswith("<*"):
|
|
||||||
logging.debug("Serial debug: {}".format(line))
|
|
||||||
else:
|
|
||||||
response += line
|
|
||||||
logging.info("Send: {}".format(response))
|
|
||||||
writer.write(response)
|
writer.write(response)
|
||||||
await writer.drain()
|
await writer.drain()
|
||||||
|
logging.info("Sent: {}".format(response))
|
||||||
|
|
||||||
writer.close()
|
writer.close()
|
||||||
await writer.wait_closed()
|
await writer.wait_closed()
|
||||||
|
|
||||||
async def return_board(self):
|
async def return_board(self):
|
||||||
self.ser.write(b'<s>')
|
"""Return the board signature"""
|
||||||
self.ser.read_until() # we need the second line
|
self.__read_serial() # drain the serial buffer on startup
|
||||||
return(self.ser.read_until().decode().strip('\n'))
|
self.__write_serial(b"<s>")
|
||||||
|
time.sleep(0.5)
|
||||||
|
board = self.__read_serial().decode().split("\n")[1] # get second line
|
||||||
|
|
||||||
|
return(board)
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
@@ -68,6 +87,7 @@ async def main():
|
|||||||
config["Serial"]["Port"],
|
config["Serial"]["Port"],
|
||||||
config["Serial"]["Baudrate"],
|
config["Serial"]["Baudrate"],
|
||||||
config["Serial"]["Timeout"]))
|
config["Serial"]["Timeout"]))
|
||||||
|
logging.warning("Initializing board")
|
||||||
logging.warning(
|
logging.warning(
|
||||||
await sd.return_board())
|
await sd.return_board())
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user