Skip to content

Commit

Permalink
Added a new module EstimSocket, that uses simple TCP sockets to do re…
Browse files Browse the repository at this point in the history
…mote controlling of the E-stim 2B.

Bumped to v0.2, and minor bugfixes too.
  • Loading branch information
fredhatt committed Mar 6, 2018
1 parent 23e00e7 commit 36b6f97
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 3 deletions.
1 change: 1 addition & 0 deletions estim2b/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from estim2b import Estim
from estimsocket import EstimSocket
4 changes: 2 additions & 2 deletions estim2b/estim2b.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#! /usr/bin/python
#! env python


import serial
Expand Down Expand Up @@ -147,7 +147,7 @@ def setOutputs(self, levelA=None, levelB=None, kill_after=0):
if levelB is not None:
self.setOutput("B", levelB)

if kill_after > 0:
if kill_after > 0:
time.sleep(kill_after)
self.kill()

Expand Down
65 changes: 65 additions & 0 deletions estim2b/estimsocket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#! env python

import socket
import time

class EstimSocket:

def __init__(self, address="127.0.0.1", port=8089, verbose=True):
self._address = address
self._port = port
self._verbose = verbose

def start_server(self, max_incoming=1, callbacks=[], on_close=None):
self.serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.serversocket.bind(('', self._port))
self.serversocket.listen(max_incoming) # become a server socket

if self._verbose:
print 'Server started... waiting for client to connect.'

conn, addr = self.serversocket.accept()

if self._verbose:
print 'New client {} connected.'.format(addr[0])
print 'Running loop.'

while True:

buf = conn.recv(1024)

if len(buf) > 0:

if self._verbose:
print 'Received {} from {}.'.format(buf, addr[0])

for i, callback in enumerate(callbacks):
# callbacks must accept two arguments: the buffer
# that was sent, and the address of the device that
# sent it.
if self._verbose:
print ' callback {} of {}...'.format(i, len(callbacks))
callback(buf, addr[0])

else: # len(buf) <= 0
print 'Client disconnected, will perform clean exit.'
if on_close is not None:
print 'running cleanup...'
on_close()
break

def client_connect(self):
self.clientsocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
self.clientsocket.connect( (self._address, self._port) )

def client_send(self, buf):
if self._verbose:
print 'Sending {} to {}'.format(buf, self._address)
self.clientsocket.send(buf)


if __name__ == "__main__":

server = EstimSocket()
server.start_server()

Binary file not shown.
12 changes: 12 additions & 0 deletions examples/server_client_passthru_example/client.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import time
from estim2b import EstimSocket

client = EstimSocket()
client.client_connect()

while True:
time.sleep(0.1)

command = raw_input('Enter E-stim 2B compatible command: ')
client.client_send('{}'.format(command))

15 changes: 15 additions & 0 deletions examples/server_client_passthru_example/start_estim2b_server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from optparse import OptionParser
from estim2b import EstimSocket
from estim2b import Estim

e2b = Estim('/dev/ttyUSB0')

def callback_command_passthru(buf, address):
e2b.send(buf)

def set_outputs_to_zero():
e2b.kill()

server = EstimSocket()
server.start_server(callbacks=[callback_command_passthru], on_close=e2b.kill)

2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


setup(name='estim2b',
version='0.1',
version='0.2',
description='Unofficial Python API for E-stim 2B',
author='Fred Hatt',
author_email='fred.r.hatt@gmail.com',
Expand Down

0 comments on commit 36b6f97

Please sign in to comment.