refactor: use is_open instead of platform specific attributes, closes #83
diff --git a/serial/serialcli.py b/serial/serialcli.py
index db35063..9596f62 100644
--- a/serial/serialcli.py
+++ b/serial/serialcli.py
@@ -145,7 +145,7 @@
@property
def in_waiting(self):
"""Return the number of characters currently in the input buffer."""
- if not self._port_handle:
+ if not self.is_open:
raise portNotOpenError
return self._port_handle.BytesToRead
@@ -155,7 +155,7 @@
return less characters as requested. With no timeout it will block
until the requested number of bytes is read.
"""
- if not self._port_handle:
+ if not self.is_open:
raise portNotOpenError
# must use single byte reads as this is the only way to read
# without applying encodings
@@ -171,7 +171,7 @@
def write(self, data):
"""Output the given string over the serial port."""
- if not self._port_handle:
+ if not self.is_open:
raise portNotOpenError
#~ if not isinstance(data, (bytes, bytearray)):
#~ raise TypeError('expected %s or bytearray, got %s' % (bytes, type(data)))
@@ -185,7 +185,7 @@
def reset_input_buffer(self):
"""Clear input buffer, discarding all that is in the buffer."""
- if not self._port_handle:
+ if not self.is_open:
raise portNotOpenError
self._port_handle.DiscardInBuffer()
@@ -194,7 +194,7 @@
Clear output buffer, aborting the current output and
discarding all that is in the buffer.
"""
- if not self._port_handle:
+ if not self.is_open:
raise portNotOpenError
self._port_handle.DiscardOutBuffer()
@@ -202,40 +202,40 @@
"""
Set break: Controls TXD. When active, to transmitting is possible.
"""
- if not self._port_handle:
+ if not self.is_open:
raise portNotOpenError
self._port_handle.BreakState = bool(self._break_state)
def _update_rts_state(self):
"""Set terminal status line: Request To Send"""
- if not self._port_handle:
+ if not self.is_open:
raise portNotOpenError
self._port_handle.RtsEnable = bool(self._rts_state)
def _update_dtr_state(self):
"""Set terminal status line: Data Terminal Ready"""
- if not self._port_handle:
+ if not self.is_open:
raise portNotOpenError
self._port_handle.DtrEnable = bool(self._dtr_state)
@property
def cts(self):
"""Read terminal status line: Clear To Send"""
- if not self._port_handle:
+ if not self.is_open:
raise portNotOpenError
return self._port_handle.CtsHolding
@property
def dsr(self):
"""Read terminal status line: Data Set Ready"""
- if not self._port_handle:
+ if not self.is_open:
raise portNotOpenError
return self._port_handle.DsrHolding
@property
def ri(self):
"""Read terminal status line: Ring Indicator"""
- if not self._port_handle:
+ if not self.is_open:
raise portNotOpenError
#~ return self._port_handle.XXX
return False # XXX an error would be better
@@ -243,7 +243,7 @@
@property
def cd(self):
"""Read terminal status line: Carrier Detect"""
- if not self._port_handle:
+ if not self.is_open:
raise portNotOpenError
return self._port_handle.CDHolding
diff --git a/serial/serialposix.py b/serial/serialposix.py
index c99dc0e..eff1009 100644
--- a/serial/serialposix.py
+++ b/serial/serialposix.py
@@ -712,7 +712,7 @@
return less characters as requested. With no timeout it will block
until the requested number of bytes is read.
"""
- if self.fd is None:
+ if not self.is_open:
raise portNotOpenError
read = bytearray()
poll = select.poll()
diff --git a/serial/serialwin32.py b/serial/serialwin32.py
index f33bee5..cb2fa0b 100644
--- a/serial/serialwin32.py
+++ b/serial/serialwin32.py
@@ -266,7 +266,7 @@
Read size bytes from the serial port. If a timeout is set it may
return less characters as requested. With no timeout it will block
until the requested number of bytes is read."""
- if not self._port_handle:
+ if not self.is_open:
raise portNotOpenError
if size > 0:
win32.ResetEvent(self._overlapped_read.hEvent)
@@ -300,7 +300,7 @@
def write(self, data):
"""Output the given byte string over the serial port."""
- if not self._port_handle:
+ if not self.is_open:
raise portNotOpenError
#~ if not isinstance(data, (bytes, bytearray)):
#~ raise TypeError('expected %s or bytearray, got %s' % (bytes, type(data)))
@@ -335,7 +335,7 @@
def reset_input_buffer(self):
"""Clear input buffer, discarding all that is in the buffer."""
- if not self._port_handle:
+ if not self.is_open:
raise portNotOpenError
win32.PurgeComm(self._port_handle, win32.PURGE_RXCLEAR | win32.PURGE_RXABORT)
@@ -344,13 +344,13 @@
Clear output buffer, aborting the current output and discarding all
that is in the buffer.
"""
- if not self._port_handle:
+ if not self.is_open:
raise portNotOpenError
win32.PurgeComm(self._port_handle, win32.PURGE_TXCLEAR | win32.PURGE_TXABORT)
def _update_break_state(self):
"""Set break: Controls TXD. When active, to transmitting is possible."""
- if not self._port_handle:
+ if not self.is_open:
raise portNotOpenError
if self._break_state:
win32.SetCommBreak(self._port_handle)
@@ -372,7 +372,7 @@
win32.EscapeCommFunction(self._port_handle, win32.CLRDTR)
def _GetCommModemStatus(self):
- if not self._port_handle:
+ if not self.is_open:
raise portNotOpenError
stat = win32.DWORD()
win32.GetCommModemStatus(self._port_handle, ctypes.byref(stat))
@@ -416,7 +416,7 @@
from the other device and control the transmission accordingly.
WARNING: this function is not portable to different platforms!
"""
- if not self._port_handle:
+ if not self.is_open:
raise portNotOpenError
if enable:
win32.EscapeCommFunction(self._port_handle, win32.SETXON)