-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a new module EstimSocket, that uses simple TCP sockets to do re…
…mote controlling of the E-stim 2B. Bumped to v0.2, and minor bugfixes too.
- Loading branch information
Showing
7 changed files
with
96 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
from estim2b import Estim | ||
from estimsocket import EstimSocket |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
15
examples/server_client_passthru_example/start_estim2b_server.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters