Skip to content

Commit

Permalink
Added socket server, modified SocketClientPlot and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Suyash458 committed Aug 1, 2016
1 parent f428f63 commit a69c6f0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
35 changes: 17 additions & 18 deletions SoftOscilloscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from matplotlib import animation
import numpy as np
import matplotlib
from utils import ClientHandler
matplotlib.use('TkAgg')

class BasePlot(object):
Expand All @@ -28,7 +29,7 @@ def close_stream(self):
if hasattr(self.stream, 'flushOutput'):
self.stream.flushOutput()
self.stream.close()
print "stream closed"
print "Stream closed"

def handle_close_event(self, event):
self.close_stream()
Expand Down Expand Up @@ -89,28 +90,26 @@ def __init__(self, com_port, baud_rate, **kwargs):
self.serial_port.baud_rate = baud_rate
self.serial_port.port = com_port
super(SerialPlot, self).__init__(self.serial_port, **kwargs)


class SocketPlot(BasePlot):
def __init__(self, address, port, **kwargs):
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.settimeout(5)
self.socket_params = (address, port)
self.socket.connect((address, port))
super(SocketPlot, self).__init__(self.socket.makefile(), **kwargs)

class SocketClientPlot(BasePlot):
def __init__(self, address, port, socket=None, **kwargs):
if not socket:
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.settimeout(5)
self.socket_params = (address, port)
self.socket.connect((address, port))
else:
self.socket_params = address
self.socket = socket
self.stream = self.socket.makefile()
super(SocketPlot, self).__init__(self.stream, **kwargs)

def open_stream(self):
try:
self.socket.connect(self.socket_params)
self.socket.settimeout(5)
self.stream = self.socket.makefile()
except:
pass
pass

def close_stream(self):
self.socket.close()
self.stream.close()
return

class GenericPlot(BasePlot):
def __init__(self, stream, **kwargs):
Expand All @@ -119,4 +118,4 @@ def __init__(self, stream, **kwargs):
and hasattr(stream, 'readline'):
super(GenericPlot, self).__init__(stream, **kwargs)
else:
raise BadAttributeError("One of the open/close/readline attributes is missing")
raise BadAttributeError("One of the open/close/readline attributes is missing")
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ to work with the package.
Uses the SocketPlot-Test example to plot a sine wave.
Run SocketPlot-Test.py on a different console window
'''
>>>from SoftOscilloscope import SocketPlot
>>>from SoftOscilloscope import SocketClientPlot
>>>plot = SocketPlot('localhost', 5000)
>>>plot.start()

Expand All @@ -41,8 +41,8 @@ Example for serial plots
'''
Takes a generic stream and sets custom parameters
'''
>>>from SoftOscilloscope import GenericStreamPlot
>>>plot = GenericStreamPlot(
>>>from SoftOscilloscope import GenericPlot
>>>plot = GenericPlot(
myStream,
xlim=(-100,100),
ylim=(-50, 50),
Expand Down
27 changes: 27 additions & 0 deletions utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from SoftOscilloscope import SocketClientPlot

class SocketServer(asyncore.dispatcher):
def __init__(self, address, num_clients=5, **kwargs):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.bind(address)
self.listen(num_clients)

def handle_accept(self):
client, addr = self.accept()
print "Connected to " + str(addr)
SocketClientPlot(socket=client, address=addr).start()

def handle_close(self):
self.close()
print "Closing server."

def run_server():
address = ('0.0.0.0',5000)
server = SocketServer(address)
try:
print "Server listening on " + str(address)
asyncore.loop(0.2, use_poll = True)
except KeyboardInterrupt:
print "Closing server."
server.close()

0 comments on commit a69c6f0

Please sign in to comment.