aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzonedabone2011-05-08 12:37:56 +0000
committerzonedabone2011-05-08 12:37:56 +0000
commit922f8ac40b67645e1fd3194b39d9f2b7dd26afc7 (patch)
treeffc3f4a3632f48e73f4443aaf998bfb4344bbb2d
parenta5ac168ea59dda460fa2c44bbf69af2641a3c8c3 (diff)
Added experimental suport for NXT communication over server connection. (Not tested yet!)
-rw-r--r--nxt/locator.py4
-rw-r--r--nxt/serversock.py61
-rw-r--r--scripts/nxt_server.py47
-rw-r--r--setup.py2
4 files changed, 113 insertions, 1 deletions
diff --git a/nxt/locator.py b/nxt/locator.py
index 693c3b8..c7487bd 100644
--- a/nxt/locator.py
+++ b/nxt/locator.py
@@ -92,3 +92,7 @@ only look for devices which match the args provided."""
raise BrickNotFoundError
+def server_brick(host, port = 2727):
+ import serversock
+ sock = serversock.ServerSock(host, port)
+ return sock.connect() \ No newline at end of file
diff --git a/nxt/serversock.py b/nxt/serversock.py
new file mode 100644
index 0000000..61d5087
--- /dev/null
+++ b/nxt/serversock.py
@@ -0,0 +1,61 @@
+# nxt.bluesock module -- Server socket communication with LEGO Minstorms NXT
+# Copyright (C) 2006 2007 Douglas P Lau
+# Copyright (C) 2009 Marcus Wanner
+# Copyright (C) 2011 zonedaobne
+#
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+import socket
+import os
+from .brick import Brick
+
+class ServerSock(object):
+
+
+ def __init__(self, host, port):
+ self.host = host
+ self.port = port
+ self.sock = None
+ self.debug = False
+
+ def __str__(self):
+ return 'Server (%s)' % self.host
+
+ def connect(self):
+ if self.debug:
+ print 'Connecting via Server...'
+ sock = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
+ sock.connect(self.host, self.port)
+ self.sock = sock
+ if self.debug:
+ print 'Connected.'
+ return Brick(self)
+
+ def close(self):
+ if self.debug:
+ print 'Closing Server connection...'
+ sock.send('\x99')
+ self.sock.close()
+ if self.debug:
+ print 'Server connection closed.'
+
+ def send(self, data):
+ if self.debug:
+ print 'Send:',
+ print ':'.join('%02x' % ord(c) for c in data)
+ self.sock.send(data)
+
+ def recv(self):
+ data = self.sock.recv(1024)
+ if self.debug:
+ print 'Recv:',
+ print ':'.join('%02x' % ord(c) for c in data)
+ return data
diff --git a/scripts/nxt_server.py b/scripts/nxt_server.py
new file mode 100644
index 0000000..479ac9c
--- /dev/null
+++ b/scripts/nxt_server.py
@@ -0,0 +1,47 @@
+#!/usr/bin/env python
+#
+# nxt_push program -- Serve an interface to the NXT brick
+# Copyright (C) 2006 Douglas P Lau
+# Copyright (C) 2010 rhn
+# Copyright (C) 2011 zonedabone
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+
+import nxt
+
+def serve(channel, details):
+ 'handles serving the client'
+ run = True
+ while run:
+ data = channel.recv(1024)
+ code = data[0]
+ if code == '\x00' or code == '\x01' or code == '\x02':
+ brick.sock.send(data)
+ reply = brick.sock.recv()
+ channel.send(reply)
+ elif code == '\x80' or code == '\x81':
+ brick.sock.send(data)
+ elif code == '\x99':
+ run = False
+ channel.slose()
+
+if __name__ == "__main__":
+ print "Connecting to NXT..."
+ brick = nxt.find_one_brick()
+ print "Brick found."
+ print "Starting server..."
+ server = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
+ server.bind ( ( '', 2727 ) )
+ server.listen ( 5 )
+ # Have the server serve "forever":
+ while True:
+ channel, details = server.accept()
+ serve(channel, details)
diff --git a/setup.py b/setup.py
index 21512fe..23c30eb 100644
--- a/setup.py
+++ b/setup.py
@@ -18,6 +18,6 @@ setup(
url='http://code.google.com/p/nxt-python/',
license='Gnu GPL v3',
packages=['nxt', 'nxt.sensor'],
- scripts=['scripts/nxt_push', 'scripts/nxt_test', 'scripts/nxt_filer'],
+ scripts=['scripts/nxt_push', 'scripts/nxt_test', 'scripts/nxt_filer', 'scripts/nxt_server.py'],
long_description=ldesc
)