summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2002-03-05 01:12:29 +0000
committercliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2002-03-05 01:12:29 +0000
commitfc9eb38aeee0e9505d98d7fea564ca5b7aa0e6d8 (patch)
treeaaca161b4f48434b99f7b75b70bdd8e84ef3fd42
parentd441974732faba5fa7a7f572cf6a7df3b3c6d035 (diff)
downloadpyserial-git-fc9eb38aeee0e9505d98d7fea564ca5b7aa0e6d8.tar.gz
added new example
-rw-r--r--pyserial/examples/miniterm.py8
-rw-r--r--pyserial/examples/tcp_serial_redirect.py119
2 files changed, 124 insertions, 3 deletions
diff --git a/pyserial/examples/miniterm.py b/pyserial/examples/miniterm.py
index 829a454..43b4fa8 100644
--- a/pyserial/examples/miniterm.py
+++ b/pyserial/examples/miniterm.py
@@ -1,8 +1,10 @@
#!/usr/bin/env python
#very simple serial terminal
+#(C)2002 Chris Liechti >cliecht@gmx.net>
+
#input characters are sent directly, received characters are displays as is
-#baudrate and echo configuartion is done through globals:
+#baudrate and echo configuartion is done through globals
import sys, os, serial, threading, getopt
@@ -10,7 +12,7 @@ import sys, os, serial, threading, getopt
EXITCHARCTER = '\x04' #ctrl+d
#first choosea platform dependant way to read single characters from the console
-if os.name == 'nt': #sys.platform == 'win32':
+if os.name == 'nt':
import msvcrt
def getkey():
while 1:
@@ -52,7 +54,7 @@ def reader():
sys.stdout.write(s.read())
def writer():
- """loop forever and copy console->serial"""
+ """loop and copy console->serial until EOF character is found"""
while 1:
c = getkey()
if c == EXITCHARCTER: break #exit on esc
diff --git a/pyserial/examples/tcp_serial_redirect.py b/pyserial/examples/tcp_serial_redirect.py
new file mode 100644
index 0000000..0816ad8
--- /dev/null
+++ b/pyserial/examples/tcp_serial_redirect.py
@@ -0,0 +1,119 @@
+#!/usr/bin/env python
+
+#(C)2002 Chris Liechti >cliecht@gmx.net>
+#redirect data from a TCP/IP connection to a serial port and vice versa
+#requires python 2.2 'cause socket.sendall is used
+
+#this program is a hack - do not use it as an example of clean
+#threading programming! it's only an example for pyserial.
+
+import sys, os, serial, threading, getopt, socket, time
+
+def reader():
+ """loop forever and copy serial->console"""
+ global connection
+ while 1:
+ try:
+ if connection:
+ connection.sendall(s.read(s.inWaiting()))
+ else:
+ time.sleep(0.2) #lower CPU usage...
+ except socket.error, msg:
+ print msg
+ if connection: connection.close()
+ connection = None
+ except:
+ pass
+
+def writer():
+ """loop forever and copy console->serial"""
+ global connection
+ try:
+ while 1:
+ s.write(connection.recv(1024))
+ except socket.error, msg:
+ print msg
+
+
+#print a short help message
+def usage():
+ print >>sys.stderr, """USAGE: %s [options]
+ Simple Terminal Programm for the serial port.
+
+ options:
+ -p, --port=PORT: serial port, a number, defualt = 0 or a device name
+ -b, --baud=BAUD: baudrate, default 9600
+ -r, --rtscts: enable RTS/CTS flow control (default off)
+ -x, --xonxoff: enable software flow control (default off)
+ -P, --localport: TCP/IP port on which to run the server (default 7777)
+ """ % sys.argv[0]
+
+if __name__ == '__main__':
+ connection = None
+
+ #parse command line options
+ try:
+ opts, args = getopt.getopt(sys.argv[1:],
+ "hp:b:rxec",
+ ["help", "port=", "baud=", "rtscts", "xonxoff", "echo", "cr"])
+ except getopt.GetoptError:
+ # print help information and exit:
+ usage()
+ sys.exit(2)
+
+ port = 0
+ baudrate = 9600
+ rtscts = 0
+ xonxoff = 0
+ localport = 7777
+ for o, a in opts:
+ if o in ("-h", "--help"): #help text
+ usage()
+ sys.exit()
+ elif o in ("-p", "--port"): #specified port
+ try:
+ port = int(a)
+ except ValueError:
+ port = a
+ elif o in ("-b", "--baud"): #specified baudrate
+ try:
+ baudrate = int(a)
+ except ValueError:
+ raise ValueError, "Baudrate must be a integer number"
+ elif o in ("-r", "--rtscts"):
+ rtscts = 1
+ elif o in ("-x", "--xonxoff"):
+ xonxoff = 1
+ elif o in ("-P", "--localport"):
+ try:
+ localport = int(a)
+ except ValueError:
+ raise ValueError, "local port must be an integer number"
+
+ print "--- TCP/IP to Serial redirector --- type Ctrl-C / BREAK to quit"
+ #start serial->tcp/ip thread
+ r = threading.Thread(target=reader)
+ r.setDaemon(1)
+ r.start()
+
+ try:
+ s = serial.Serial(port, baudrate, rtscts=rtscts, xonxoff=xonxoff)
+ except:
+ print "could not open port"
+ sys.exit(1)
+
+ srv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
+ srv.bind( ('', localport) )
+ srv.listen(1)
+ while 1:
+ try:
+ connection, addr = srv.accept()
+ print 'Connected by', addr
+ #enter console->serial loop
+ writer()
+ except socket.error, msg:
+ print msg
+ if connection: connection.close()
+ connection = None
+
+ print "\n--- exit ---"