summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2002-02-14 01:33:33 +0000
committercliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a>2002-02-14 01:33:33 +0000
commite8f75f91b5b96039c496215005731c50f356690d (patch)
tree76421618dfaa2700dba492b012141efd979bb67f
parentd0b8b2733d027e2a68a87df92697e2cb7aa3c79d (diff)
downloadpyserial-git-e8f75f91b5b96039c496215005731c50f356690d.tar.gz
FileLike base class is now used for readline etc.
-rw-r--r--pyserial/serial/__init__.py6
-rw-r--r--pyserial/serial/serialjava.py9
-rw-r--r--pyserial/serial/serialposix.py38
3 files changed, 38 insertions, 15 deletions
diff --git a/pyserial/serial/__init__.py b/pyserial/serial/__init__.py
index b5d9c91..75bb07f 100644
--- a/pyserial/serial/__init__.py
+++ b/pyserial/serial/__init__.py
@@ -1,12 +1,12 @@
-#!/usr/bin/env python
+#!/usr/bin/env python
#portable serial port access with python
#this is a wrapper module for different platform implementations
#
-# (C)2001 Chris Liechti <cliechti@gmx.net>
+# (C)2001-2002 Chris Liechti <cliechti@gmx.net>
# this is distributed under a free software license, see license.txt
import sys, os, string
-VERSION = string.split("$Revision: 1.1.1.1 $")[1] #extract CVS version
+VERSION = string.split("$Revision: 1.2 $")[1] #extract CVS version
#chose an implementation, depending on os
if os.name == 'nt': #sys.platform == 'win32':
diff --git a/pyserial/serial/serialjava.py b/pyserial/serial/serialjava.py
index f7601ec..9049731 100644
--- a/pyserial/serial/serialjava.py
+++ b/pyserial/serial/serialjava.py
@@ -1,13 +1,14 @@
-#!/usr/bin/env python
+#!jython
#module for serial IO for Jython and JavaComm
-#see serial.py
+#see __init__.py
#
#(C) 2002 Chris Liechti <cliechti@gmx.net>
# this is distributed under a free software license, see license.txt
import sys, os, string, javax.comm
+import serialutil
-VERSION = string.split("$Revision: 1.1.1.1 $")[1] #extract CVS version
+VERSION = string.split("$Revision: 1.2 $")[1] #extract CVS version
PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK, PARITY_SPACE = (0,1,2,3,4)
STOPBITS_ONE, STOPBITS_TWO, STOPBITS_ONE_HALVE = (1, 2, 3)
@@ -25,7 +26,7 @@ def device(portnumber):
ports.append(el)
return ports[portnumber]
-class Serial:
+class Serial(serialutil.FileLike):
def __init__(self,
port, #number of device, numbering starts at
#zero. if everything fails, the user
diff --git a/pyserial/serial/serialposix.py b/pyserial/serial/serialposix.py
index 8d58542..cd7b98e 100644
--- a/pyserial/serial/serialposix.py
+++ b/pyserial/serial/serialposix.py
@@ -1,6 +1,6 @@
#!/usr/bin/env python
#module for serial IO for POSIX compatible systems, like Linux
-#see serial.py
+#see __init__.py
#
#(C) 2001 Chris Liechti <cliechti@gmx.net>
# this is distributed under a free software license, see license.txt
@@ -10,13 +10,15 @@
# references: http://www.easysw.com/~mike/serial/serial.html
import sys, os, fcntl, termios, struct, string, select
+import serialutil
-VERSION = string.split("$Revision: 1.1.1.1 $")[1] #extract CVS version
+VERSION = string.split("$Revision: 1.2 $")[1] #extract CVS version
PARITY_NONE, PARITY_EVEN, PARITY_ODD = range(3)
STOPBITS_ONE, STOPBITS_TWO = (1, 2)
FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS = (5,6,7,8)
+#Do check the Python version as some constants have moved.
if (sys.hexversion < 0x020100f0):
import TERMIOS
else:
@@ -95,7 +97,8 @@ for rate in (0,50,75,110,134,150,200,300,600,1200,1800,2400,4800,9600,
except:
pass
-if hasattr(TERMIOS, 'TIOCMGET'):
+#load some constants for late use
+if hasattr(TERMIOS, 'TIOCMGET'): #if this const is here the others will be to (hopefully)
TIOCMGET = TERMIOS.TIOCMGET
TIOCMBIS = TERMIOS.TIOCMBIS
TIOCMBIC = TERMIOS.TIOCMBIC
@@ -115,10 +118,10 @@ if hasattr(TERMIOS, 'TIOCMGET'):
TIOCM_OUT1 = TERMIOS.TIOCM_OUT1
TIOCM_OUT2 = TERMIOS.TIOCM_OUT2
else: #workaround for older python versions
- TIOCMGET = 0x5415
- TIOCMBIS = 0x5416
- TIOCMBIC = 0x5417
- TIOCMSET = 0x5418
+ TIOCMGET = 0x5415
+ TIOCMBIS = 0x5416
+ TIOCMBIC = 0x5417
+ TIOCMSET = 0x5418
TIOCM_LE = 0x001
TIOCM_DTR = 0x002
@@ -140,7 +143,7 @@ TIOCM_DTR_str = struct.pack('I', TIOCM_DTR)
portNotOpenError = ValueError('port not open')
-class Serial:
+class Serial(serialutil.FileLike):
def __init__(self,
port, #number of device, numbering starts at
#zero. if everything fails, the user
@@ -154,6 +157,7 @@ class Serial:
xonxoff=0, #enable software flow control
rtscts=0, #enable RTS/CTS flow control
):
+ """init comm port"""
self.fd = None
self.timeout = timeout
vmin = vtime = 0 #timeout is done via select
@@ -240,21 +244,26 @@ class Serial:
self.__tcsetattr()
def __tcsetattr(self):
+ """internal function to set port attributes"""
termios.tcsetattr(self.fd, TERMIOS.TCSANOW, [self.iflag,self.oflag,self.cflag,self.lflag,self.ispeed,self.ospeed,self.cc])
def __tcgetattr(self):
+ """internal function to get port attributes"""
self.iflag,self.oflag,self.cflag,self.lflag,self.ispeed,self.ospeed,self.cc = termios.tcgetattr(self.fd)
def close(self):
+ """close port"""
if self.fd:
os.close(self.fd)
self.fd = None
def inWaiting(self):
+ """how many character are in the input queue"""
s = fcntl.ioctl(self.fd, TERMIOS.FIONREAD, TIOCM_zero_str)
return struct.unpack('I',s)[0]
def write(self, data):
+ """write a string to the port"""
if not self.fd: raise portNotOpenError
t = len(data)
d = data
@@ -264,6 +273,8 @@ class Serial:
t = t - n
def read(self, size=1):
+ """read a number of bytes from the port.
+ the default is one (unlike files)"""
if not self.fd: raise portNotOpenError
read = ''
imp = None
@@ -284,50 +295,60 @@ class Serial:
return read
def flushInput(self):
+ """clear input queue"""
if not self.fd:
raise portNotOpenError
termios.tcflush(self.fd, TERMIOS.TCIFLUSH)
def flushOutput(self):
+ """flush output"""
if not self.fd:
raise portNotOpenError
termios.tcflush(self.fd, TERMIOS.TCOFLUSH)
def sendBreak(self):
+ """send break signal"""
if not self.fd:
raise portNotOpenError
termios.tcsendbreak(self.fd, 0)
def drainOutput(self):
+ """internal - not portable!"""
if not self.fd: raise portNotOpenError
termios.tcdrain(self.fd)
def nonblocking(self):
+ """internal - not portable!"""
if not self.fd:
raise portNotOpenError
fcntl.fcntl(self.fd, FCNTL.F_SETFL, FCNTL.O_NONBLOCK)
def getDSR(self):
+ """read terminal status line"""
if not self.fd: raise portNotOpenError
s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str)
return struct.unpack('I',s)[0] & TIOCM_DSR
def getCD(self):
+ """read terminal status line"""
if not self.fd: raise portNotOpenError
s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str)
return struct.unpack('I',s)[0] & TIOCM_CD
def getRI(self):
+ """read terminal status line"""
if not self.fd: raise portNotOpenError
s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str)
return struct.unpack('I',s)[0] & TIOCM_RI
def getCTS(self):
+ """read terminal status line"""
if not self.fd: raise portNotOpenError
s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str)
return struct.unpack('I',s)[0] & TIOCM_CTS
def setDTR(self,on=1):
+ """set terminal status line"""
if not self.fd: raise portNotOpenError
if on:
fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_DTR_str)
@@ -335,6 +356,7 @@ class Serial:
fcntl.ioctl(self.fd, TIOCMBIC, TIOCM_DTR_str)
def setRTS(self,on=1):
+ """set terminal status line"""
if not self.fd: raise portNotOpenError
if on:
fcntl.ioctl(self.fd, TIOCMBIS, TIOCM_RTS_str)