diff options
author | Chris Liechti <cliechti@gmx.net> | 2020-09-14 03:59:52 +0200 |
---|---|---|
committer | Chris Liechti <cliechti@gmx.net> | 2020-09-14 03:59:52 +0200 |
commit | e99bda36b6f93b1a4232a96c9927e75aebd31580 (patch) | |
tree | edfc97b55054a6aa911d923baf229d9f7f86f92d | |
parent | 4573c3165ed130c7bfaa786b263cd27ea72e0529 (diff) | |
download | pyserial-e99bda36b6f93b1a4232a96c9927e75aebd31580.tar.gz |
refactor: raise new instances for PortNotOpenError and SerialTimeoutException
related to #502
fixes #437
-rw-r--r-- | serial/rfc2217.py | 26 | ||||
-rw-r--r-- | serial/serialcli.py | 26 | ||||
-rw-r--r-- | serial/serialjava.py | 26 | ||||
-rw-r--r-- | serial/serialposix.py | 38 | ||||
-rw-r--r-- | serial/serialutil.py | 8 | ||||
-rw-r--r-- | serial/serialwin32.py | 18 | ||||
-rw-r--r-- | serial/urlhandler/protocol_cp2110.py | 12 | ||||
-rw-r--r-- | serial/urlhandler/protocol_loop.py | 20 | ||||
-rw-r--r-- | serial/urlhandler/protocol_socket.py | 28 | ||||
-rw-r--r-- | test/handlers/protocol_test.py | 26 |
10 files changed, 115 insertions, 113 deletions
diff --git a/serial/rfc2217.py b/serial/rfc2217.py index d388038..2ae188e 100644 --- a/serial/rfc2217.py +++ b/serial/rfc2217.py @@ -76,7 +76,7 @@ except ImportError: import serial from serial.serialutil import SerialBase, SerialException, to_bytes, \ - iterbytes, portNotOpenError, Timeout + iterbytes, PortNotOpenError, Timeout # port string is expected to be something like this: # rfc2217://host:port @@ -598,7 +598,7 @@ class Serial(SerialBase): def in_waiting(self): """Return the number of bytes currently in the input buffer.""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() return self._read_buffer.qsize() def read(self, size=1): @@ -608,7 +608,7 @@ class Serial(SerialBase): until the requested number of bytes is read. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() data = bytearray() try: timeout = Timeout(self._timeout) @@ -632,7 +632,7 @@ class Serial(SerialBase): closed. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() with self._write_lock: try: self._socket.sendall(to_bytes(data).replace(IAC, IAC_DOUBLED)) @@ -643,7 +643,7 @@ class Serial(SerialBase): def reset_input_buffer(self): """Clear input buffer, discarding all that is in the buffer.""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() self.rfc2217_send_purge(PURGE_RECEIVE_BUFFER) # empty read buffer while self._read_buffer.qsize(): @@ -655,7 +655,7 @@ class Serial(SerialBase): discarding all that is in the buffer. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() self.rfc2217_send_purge(PURGE_TRANSMIT_BUFFER) def _update_break_state(self): @@ -664,7 +664,7 @@ class Serial(SerialBase): possible. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if self.logger: self.logger.info('set BREAK to {}'.format('active' if self._break_state else 'inactive')) if self._break_state: @@ -675,7 +675,7 @@ class Serial(SerialBase): def _update_rts_state(self): """Set terminal status line: Request To Send.""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if self.logger: self.logger.info('set RTS to {}'.format('active' if self._rts_state else 'inactive')) if self._rts_state: @@ -686,7 +686,7 @@ class Serial(SerialBase): def _update_dtr_state(self): """Set terminal status line: Data Terminal Ready.""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if self.logger: self.logger.info('set DTR to {}'.format('active' if self._dtr_state else 'inactive')) if self._dtr_state: @@ -698,28 +698,28 @@ class Serial(SerialBase): def cts(self): """Read terminal status line: Clear To Send.""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() return bool(self.get_modem_state() & MODEMSTATE_MASK_CTS) @property def dsr(self): """Read terminal status line: Data Set Ready.""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() return bool(self.get_modem_state() & MODEMSTATE_MASK_DSR) @property def ri(self): """Read terminal status line: Ring Indicator.""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() return bool(self.get_modem_state() & MODEMSTATE_MASK_RI) @property def cd(self): """Read terminal status line: Carrier Detect.""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() return bool(self.get_modem_state() & MODEMSTATE_MASK_CD) # - - - platform specific - - - diff --git a/serial/serialcli.py b/serial/serialcli.py index ddd0cdf..4614736 100644 --- a/serial/serialcli.py +++ b/serial/serialcli.py @@ -148,7 +148,7 @@ class Serial(SerialBase): def in_waiting(self): """Return the number of characters currently in the input buffer.""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() return self._port_handle.BytesToRead def read(self, size=1): @@ -158,7 +158,7 @@ class Serial(SerialBase): until the requested number of bytes is read. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() # must use single byte reads as this is the only way to read # without applying encodings data = bytearray() @@ -174,7 +174,7 @@ class Serial(SerialBase): def write(self, data): """Output the given string over the serial port.""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() #~ if not isinstance(data, (bytes, bytearray)): #~ raise TypeError('expected %s or bytearray, got %s' % (bytes, type(data))) try: @@ -182,13 +182,13 @@ class Serial(SerialBase): # as this is the only one not applying encodings self._port_handle.Write(as_byte_array(data), 0, len(data)) except System.TimeoutException: - raise writeTimeoutError + raise SerialTimeoutException('Write timeout') return len(data) def reset_input_buffer(self): """Clear input buffer, discarding all that is in the buffer.""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() self._port_handle.DiscardInBuffer() def reset_output_buffer(self): @@ -197,7 +197,7 @@ class Serial(SerialBase): discarding all that is in the buffer. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() self._port_handle.DiscardOutBuffer() def _update_break_state(self): @@ -205,40 +205,40 @@ class Serial(SerialBase): Set break: Controls TXD. When active, to transmitting is possible. """ if not self.is_open: - raise portNotOpenError + 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.is_open: - raise portNotOpenError + 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.is_open: - raise portNotOpenError + raise PortNotOpenError() self._port_handle.DtrEnable = bool(self._dtr_state) @property def cts(self): """Read terminal status line: Clear To Send""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() return self._port_handle.CtsHolding @property def dsr(self): """Read terminal status line: Data Set Ready""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() return self._port_handle.DsrHolding @property def ri(self): """Read terminal status line: Ring Indicator""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() #~ return self._port_handle.XXX return False # XXX an error would be better @@ -246,7 +246,7 @@ class Serial(SerialBase): def cd(self): """Read terminal status line: Carrier Detect""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() return self._port_handle.CDHolding # - - platform specific - - - - diff --git a/serial/serialjava.py b/serial/serialjava.py index 9c920c5..0789a78 100644 --- a/serial/serialjava.py +++ b/serial/serialjava.py @@ -152,7 +152,7 @@ class Serial(SerialBase): def in_waiting(self): """Return the number of characters currently in the input buffer.""" if not self.sPort: - raise portNotOpenError + raise PortNotOpenError() return self._instream.available() def read(self, size=1): @@ -162,7 +162,7 @@ class Serial(SerialBase): until the requested number of bytes is read. """ if not self.sPort: - raise portNotOpenError + raise PortNotOpenError() read = bytearray() if size > 0: while len(read) < size: @@ -177,7 +177,7 @@ class Serial(SerialBase): def write(self, data): """Output the given string over the serial port.""" if not self.sPort: - raise portNotOpenError + raise PortNotOpenError() if not isinstance(data, (bytes, bytearray)): raise TypeError('expected %s or bytearray, got %s' % (bytes, type(data))) self._outstream.write(data) @@ -186,7 +186,7 @@ class Serial(SerialBase): def reset_input_buffer(self): """Clear input buffer, discarding all that is in the buffer.""" if not self.sPort: - raise portNotOpenError + raise PortNotOpenError() self._instream.skip(self._instream.available()) def reset_output_buffer(self): @@ -195,57 +195,57 @@ class Serial(SerialBase): discarding all that is in the buffer. """ if not self.sPort: - raise portNotOpenError + raise PortNotOpenError() self._outstream.flush() def send_break(self, duration=0.25): """Send break condition. Timed, returns to idle state after given duration.""" if not self.sPort: - raise portNotOpenError + raise PortNotOpenError() self.sPort.sendBreak(duration*1000.0) def _update_break_state(self): """Set break: Controls TXD. When active, to transmitting is possible.""" if self.fd is None: - raise portNotOpenError + raise PortNotOpenError() raise SerialException("The _update_break_state function is not implemented in java.") def _update_rts_state(self): """Set terminal status line: Request To Send""" if not self.sPort: - raise portNotOpenError + raise PortNotOpenError() self.sPort.setRTS(self._rts_state) def _update_dtr_state(self): """Set terminal status line: Data Terminal Ready""" if not self.sPort: - raise portNotOpenError + raise PortNotOpenError() self.sPort.setDTR(self._dtr_state) @property def cts(self): """Read terminal status line: Clear To Send""" if not self.sPort: - raise portNotOpenError + raise PortNotOpenError() self.sPort.isCTS() @property def dsr(self): """Read terminal status line: Data Set Ready""" if not self.sPort: - raise portNotOpenError + raise PortNotOpenError() self.sPort.isDSR() @property def ri(self): """Read terminal status line: Ring Indicator""" if not self.sPort: - raise portNotOpenError + raise PortNotOpenError() self.sPort.isRI() @property def cd(self): """Read terminal status line: Carrier Detect""" if not self.sPort: - raise portNotOpenError + raise PortNotOpenError() self.sPort.isCD() diff --git a/serial/serialposix.py b/serial/serialposix.py index 334ba9f..f31b99d 100644 --- a/serial/serialposix.py +++ b/serial/serialposix.py @@ -39,7 +39,7 @@ import termios import serial from serial.serialutil import SerialBase, SerialException, to_bytes, \ - portNotOpenError, writeTimeoutError, Timeout + PortNotOpenError, SerialTimeoutException, Timeout class PlatformSpecificBase(object): @@ -532,7 +532,7 @@ class Serial(SerialBase, PlatformSpecific): until the requested number of bytes is read. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() read = bytearray() timeout = Timeout(self._timeout) while len(read) < size: @@ -585,7 +585,7 @@ class Serial(SerialBase, PlatformSpecific): def write(self, data): """Output the given byte string over the serial port.""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() d = to_bytes(data) tx_len = length = len(d) timeout = Timeout(self._write_timeout) @@ -600,13 +600,13 @@ class Serial(SerialBase, PlatformSpecific): # when timeout is set, use select to wait for being ready # with the time left as timeout if timeout.expired(): - raise writeTimeoutError + raise SerialTimeoutException('Write timeout') abort, ready, _ = select.select([self.pipe_abort_write_r], [self.fd], [], timeout.time_left()) if abort: os.read(self.pipe_abort_write_r, 1000) break if not ready: - raise writeTimeoutError + raise SerialTimeoutException('Write timeout') else: assert timeout.time_left() is None # wait for write operation @@ -633,7 +633,7 @@ class Serial(SerialBase, PlatformSpecific): if e[0] not in (errno.EAGAIN, errno.EALREADY, errno.EWOULDBLOCK, errno.EINPROGRESS, errno.EINTR): raise SerialException('write failed: {}'.format(e)) if not timeout.is_non_blocking and timeout.expired(): - raise writeTimeoutError + raise SerialTimeoutException('Write timeout') return length - len(d) def flush(self): @@ -642,13 +642,13 @@ class Serial(SerialBase, PlatformSpecific): is written. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() termios.tcdrain(self.fd) def reset_input_buffer(self): """Clear input buffer, discarding all that is in the buffer.""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() termios.tcflush(self.fd, termios.TCIFLUSH) def reset_output_buffer(self): @@ -657,7 +657,7 @@ class Serial(SerialBase, PlatformSpecific): that is in the buffer. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() termios.tcflush(self.fd, termios.TCOFLUSH) def send_break(self, duration=0.25): @@ -666,7 +666,7 @@ class Serial(SerialBase, PlatformSpecific): duration. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() termios.tcsendbreak(self.fd, int(duration / 0.25)) def _update_rts_state(self): @@ -687,7 +687,7 @@ class Serial(SerialBase, PlatformSpecific): def cts(self): """Read terminal status line: Clear To Send""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str) return struct.unpack('I', s)[0] & TIOCM_CTS != 0 @@ -695,7 +695,7 @@ class Serial(SerialBase, PlatformSpecific): def dsr(self): """Read terminal status line: Data Set Ready""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str) return struct.unpack('I', s)[0] & TIOCM_DSR != 0 @@ -703,7 +703,7 @@ class Serial(SerialBase, PlatformSpecific): def ri(self): """Read terminal status line: Ring Indicator""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str) return struct.unpack('I', s)[0] & TIOCM_RI != 0 @@ -711,7 +711,7 @@ class Serial(SerialBase, PlatformSpecific): def cd(self): """Read terminal status line: Carrier Detect""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() s = fcntl.ioctl(self.fd, TIOCMGET, TIOCM_zero_str) return struct.unpack('I', s)[0] & TIOCM_CD != 0 @@ -730,7 +730,7 @@ class Serial(SerialBase, PlatformSpecific): WARNING: this function is not portable to different platforms! """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() return self.fd def set_input_flow_control(self, enable=True): @@ -740,7 +740,7 @@ class Serial(SerialBase, PlatformSpecific): WARNING: this function is not portable to different platforms! """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if enable: termios.tcflow(self.fd, termios.TCION) else: @@ -753,7 +753,7 @@ class Serial(SerialBase, PlatformSpecific): WARNING: this function is not portable to different platforms! """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if enable: termios.tcflow(self.fd, termios.TCOON) else: @@ -779,7 +779,7 @@ class PosixPollSerial(Serial): until the requested number of bytes is read. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() read = bytearray() timeout = Timeout(self._timeout) poll = select.poll() @@ -856,7 +856,7 @@ class VTIMESerial(Serial): until the requested number of bytes is read. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() read = bytearray() while len(read) < size: buf = os.read(self.fd, size - len(read)) diff --git a/serial/serialutil.py b/serial/serialutil.py index 2785b6f..8c5ccfe 100644 --- a/serial/serialutil.py +++ b/serial/serialutil.py @@ -97,8 +97,10 @@ class SerialTimeoutException(SerialException): """Write timeouts give an exception""" -writeTimeoutError = SerialTimeoutException('Write timeout') -portNotOpenError = SerialException('Attempting to use a port that is not open') +class PortNotOpenError(SerialException): + """Port is not open""" + def __init__(self): + super(PortNotOpenError, self).__init__('Attempting to use a port that is not open') class Timeout(object): @@ -574,7 +576,7 @@ class SerialBase(io.RawIOBase): duration. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() self.break_condition = True time.sleep(duration) self.break_condition = False diff --git a/serial/serialwin32.py b/serial/serialwin32.py index bd1944c..54d3e12 100644 --- a/serial/serialwin32.py +++ b/serial/serialwin32.py @@ -17,7 +17,7 @@ import time from serial import win32 import serial -from serial.serialutil import SerialBase, SerialException, to_bytes, portNotOpenError, writeTimeoutError +from serial.serialutil import SerialBase, SerialException, to_bytes, PortNotOpenError, SerialTimeoutException class Serial(SerialBase): @@ -266,7 +266,7 @@ class Serial(SerialBase): until the requested number of bytes is read. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if size > 0: win32.ResetEvent(self._overlapped_read.hEvent) flags = win32.DWORD() @@ -303,7 +303,7 @@ class Serial(SerialBase): def write(self, data): """Output the given byte string over the serial port.""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() #~ if not isinstance(data, (bytes, bytearray)): #~ raise TypeError('expected %s or bytearray, got %s' % (bytes, type(data))) # convert data (needed in case of memoryview instance: Py 3.1 io lib), ctypes doesn't like memoryview @@ -322,7 +322,7 @@ class Serial(SerialBase): if win32.GetLastError() == win32.ERROR_OPERATION_ABORTED: return n.value # canceled IO is no error if n.value != len(data): - raise writeTimeoutError + raise SerialTimeoutException('Write timeout') return n.value else: errorcode = win32.ERROR_SUCCESS if success else win32.GetLastError() @@ -351,7 +351,7 @@ class Serial(SerialBase): def reset_input_buffer(self): """Clear input buffer, discarding all that is in the buffer.""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() win32.PurgeComm(self._port_handle, win32.PURGE_RXCLEAR | win32.PURGE_RXABORT) def reset_output_buffer(self): @@ -360,13 +360,13 @@ class Serial(SerialBase): that is in the buffer. """ if not self.is_open: - raise portNotOpenError + 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.is_open: - raise portNotOpenError + raise PortNotOpenError() if self._break_state: win32.SetCommBreak(self._port_handle) else: @@ -388,7 +388,7 @@ class Serial(SerialBase): def _GetCommModemStatus(self): if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() stat = win32.DWORD() win32.GetCommModemStatus(self._port_handle, ctypes.byref(stat)) return stat.value @@ -432,7 +432,7 @@ class Serial(SerialBase): WARNING: this function is not portable to different platforms! """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if enable: win32.EscapeCommFunction(self._port_handle, win32.SETXON) else: diff --git a/serial/urlhandler/protocol_cp2110.py b/serial/urlhandler/protocol_cp2110.py index 04ba03e..44ad4eb 100644 --- a/serial/urlhandler/protocol_cp2110.py +++ b/serial/urlhandler/protocol_cp2110.py @@ -40,7 +40,7 @@ except ImportError: import hid # hidapi import serial -from serial.serialutil import SerialBase, SerialException, portNotOpenError, to_bytes, Timeout +from serial.serialutil import SerialBase, SerialException, PortNotOpenError, to_bytes, Timeout # Report IDs and related constant @@ -185,7 +185,7 @@ class Serial(SerialBase): def reset_input_buffer(self): if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() self._hid_handle.send_feature_report( bytes((_REPORT_SET_PURGE_FIFOS, _PURGE_RX_FIFO))) # empty read buffer @@ -194,13 +194,13 @@ class Serial(SerialBase): def reset_output_buffer(self): if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() self._hid_handle.send_feature_report( bytes((_REPORT_SET_PURGE_FIFOS, _PURGE_TX_FIFO))) def _update_break_state(self): if not self._hid_handle: - raise portNotOpenError + raise PortNotOpenError() if self._break_state: self._hid_handle.send_feature_report( @@ -214,7 +214,7 @@ class Serial(SerialBase): def read(self, size=1): if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() data = bytearray() try: @@ -234,7 +234,7 @@ class Serial(SerialBase): def write(self, data): if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() data = to_bytes(data) tx_len = len(data) while tx_len > 0: diff --git a/serial/urlhandler/protocol_loop.py b/serial/urlhandler/protocol_loop.py index 985e7a7..526583f 100644 --- a/serial/urlhandler/protocol_loop.py +++ b/serial/urlhandler/protocol_loop.py @@ -27,7 +27,7 @@ try: except ImportError: import Queue as queue -from serial.serialutil import SerialBase, SerialException, to_bytes, iterbytes, writeTimeoutError, portNotOpenError +from serial.serialutil import SerialBase, SerialException, to_bytes, iterbytes, SerialTimeoutException, PortNotOpenError # map log level names to constants. used in from_url() LOGGER_LEVELS = { @@ -127,7 +127,7 @@ class Serial(SerialBase): def in_waiting(self): """Return the number of bytes currently in the input buffer.""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if self.logger: # attention the logged value can differ from return value in # threaded environments... @@ -141,7 +141,7 @@ class Serial(SerialBase): until the requested number of bytes is read. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if self._timeout is not None and self._timeout != 0: timeout = time.time() + self._timeout else: @@ -181,7 +181,7 @@ class Serial(SerialBase): """ self._cancel_write = False if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() data = to_bytes(data) # calculate aprox time that would be used to send the data time_used_to_send = 10.0 * len(data) / self._baudrate @@ -195,7 +195,7 @@ class Serial(SerialBase): time_left -= 0.5 if self._cancel_write: return 0 # XXX - raise writeTimeoutError + raise SerialTimeoutException('Write timeout') for byte in iterbytes(data): self.queue.put(byte, timeout=self._write_timeout) return len(data) @@ -203,7 +203,7 @@ class Serial(SerialBase): def reset_input_buffer(self): """Clear input buffer, discarding all that is in the buffer.""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if self.logger: self.logger.info('reset_input_buffer()') try: @@ -218,7 +218,7 @@ class Serial(SerialBase): discarding all that is in the buffer. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if self.logger: self.logger.info('reset_output_buffer()') try: @@ -249,7 +249,7 @@ class Serial(SerialBase): def cts(self): """Read terminal status line: Clear To Send""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if self.logger: self.logger.info('CTS -> state of RTS ({!r})'.format(self._rts_state)) return self._rts_state @@ -265,7 +265,7 @@ class Serial(SerialBase): def ri(self): """Read terminal status line: Ring Indicator""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if self.logger: self.logger.info('returning dummy for RI') return False @@ -274,7 +274,7 @@ class Serial(SerialBase): def cd(self): """Read terminal status line: Carrier Detect""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if self.logger: self.logger.info('returning dummy for CD') return True diff --git a/serial/urlhandler/protocol_socket.py b/serial/urlhandler/protocol_socket.py index 11f6a05..2888467 100644 --- a/serial/urlhandler/protocol_socket.py +++ b/serial/urlhandler/protocol_socket.py @@ -29,7 +29,7 @@ except ImportError: import urllib.parse as urlparse from serial.serialutil import SerialBase, SerialException, to_bytes, \ - portNotOpenError, writeTimeoutError, Timeout + PortNotOpenError, SerialTimeoutException, Timeout # map log level names to constants. used in from_url() LOGGER_LEVELS = { @@ -136,7 +136,7 @@ class Serial(SerialBase): def in_waiting(self): """Return the number of bytes currently in the input buffer.""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() # Poll the socket to see if it is ready for reading. # If ready, at least one byte will be to read. lr, lw, lx = select.select([self._socket], [], [], 0) @@ -152,7 +152,7 @@ class Serial(SerialBase): until the requested number of bytes is read. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() read = bytearray() timeout = Timeout(self._timeout) while len(read) < size: @@ -193,7 +193,7 @@ class Serial(SerialBase): closed. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() d = to_bytes(data) tx_len = length = len(d) @@ -209,10 +209,10 @@ class Serial(SerialBase): # when timeout is set, use select to wait for being ready # with the time left as timeout if timeout.expired(): - raise writeTimeoutError + raise SerialTimeoutException('Write timeout') _, ready, _ = select.select([], [self._socket], [], timeout.time_left()) if not ready: - raise writeTimeoutError + raise SerialTimeoutException('Write timeout') else: assert timeout.time_left() is None # wait for write operation @@ -236,13 +236,13 @@ class Serial(SerialBase): if e[0] not in (errno.EAGAIN, errno.EALREADY, errno.EWOULDBLOCK, errno.EINPROGRESS, errno.EINTR): raise SerialException('write failed: {}'.format(e)) if not timeout.is_non_blocking and timeout.expired(): - raise writeTimeoutError + raise SerialTimeoutException('Write timeout') return length - len(d) def reset_input_buffer(self): """Clear input buffer, discarding all that is in the buffer.""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() # just use recv to remove input, while there is some ready = True @@ -270,7 +270,7 @@ class Serial(SerialBase): discarding all that is in the buffer. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if self.logger: self.logger.info('ignored reset_output_buffer') @@ -280,7 +280,7 @@ class Serial(SerialBase): duration. """ if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if self.logger: self.logger.info('ignored send_break({!r})'.format(duration)) @@ -304,7 +304,7 @@ class Serial(SerialBase): def cts(self): """Read terminal status line: Clear To Send""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if self.logger: self.logger.info('returning dummy for cts') return True @@ -313,7 +313,7 @@ class Serial(SerialBase): def dsr(self): """Read terminal status line: Data Set Ready""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if self.logger: self.logger.info('returning dummy for dsr') return True @@ -322,7 +322,7 @@ class Serial(SerialBase): def ri(self): """Read terminal status line: Ring Indicator""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if self.logger: self.logger.info('returning dummy for ri') return False @@ -331,7 +331,7 @@ class Serial(SerialBase): def cd(self): """Read terminal status line: Carrier Detect""" if not self.is_open: - raise portNotOpenError + raise PortNotOpenError() if self.logger: self.logger.info('returning dummy for cd)') return True diff --git a/test/handlers/protocol_test.py b/test/handlers/protocol_test.py index f2e572f..c0cffa6 100644 --- a/test/handlers/protocol_test.py +++ b/test/handlers/protocol_test.py @@ -80,7 +80,7 @@ class DummySerial(SerialBase): def inWaiting(self): """Return the number of characters currently in the input buffer.""" - if not self._isOpen: raise portNotOpenError + if not self._isOpen: raise PortNotOpenError() if self.logger: # set this one to debug as the function could be called often... self.logger.debug('WARNING: inWaiting returns dummy value') @@ -90,7 +90,7 @@ class DummySerial(SerialBase): """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._isOpen: raise portNotOpenError + if not self._isOpen: raise PortNotOpenError() data = '123' # dummy data return bytes(data) @@ -98,73 +98,73 @@ class DummySerial(SerialBase): """Output the given string over the serial port. Can block if the connection is blocked. May raise SerialException if the connection is closed.""" - if not self._isOpen: raise portNotOpenError + if not self._isOpen: raise PortNotOpenError() # nothing done return len(data) def flushInput(self): """Clear input buffer, discarding all that is in the buffer.""" - if not self._isOpen: raise portNotOpenError + if not self._isOpen: raise PortNotOpenError() if self.logger: self.logger.info('ignored flushInput') def flushOutput(self): """Clear output buffer, aborting the current output and discarding all that is in the buffer.""" - if not self._isOpen: raise portNotOpenError + if not self._isOpen: raise PortNotOpenError() if self.logger: self.logger.info('ignored flushOutput') def sendBreak(self, duration=0.25): """Send break condition. Timed, returns to idle state after given duration.""" - if not self._isOpen: raise portNotOpenError + if not self._isOpen: raise PortNotOpenError() if self.logger: self.logger.info('ignored sendBreak({!r})'.format(duration)) def setBreak(self, level=True): """Set break: Controls TXD. When active, to transmitting is possible.""" - if not self._isOpen: raise portNotOpenError + if not self._isOpen: raise PortNotOpenError() if self.logger: self.logger.info('ignored setBreak({!r})'.format(level)) def setRTS(self, level=True): """Set terminal status line: Request To Send""" - if not self._isOpen: raise portNotOpenError + if not self._isOpen: raise PortNotOpenError() if self.logger: self.logger.info('ignored setRTS({!r})'.format(level)) def setDTR(self, level=True): """Set terminal status line: Data Terminal Ready""" - if not self._isOpen: raise portNotOpenError + if not self._isOpen: raise PortNotOpenError() if self.logger: self.logger.info('ignored setDTR({!r})'.format(level)) def getCTS(self): """Read terminal status line: Clear To Send""" - if not self._isOpen: raise portNotOpenError + if not self._isOpen: raise PortNotOpenError() if self.logger: self.logger.info('returning dummy for getCTS()') return True def getDSR(self): """Read terminal status line: Data Set Ready""" - if not self._isOpen: raise portNotOpenError + if not self._isOpen: raise PortNotOpenError() if self.logger: self.logger.info('returning dummy for getDSR()') return True def getRI(self): """Read terminal status line: Ring Indicator""" - if not self._isOpen: raise portNotOpenError + if not self._isOpen: raise PortNotOpenError() if self.logger: self.logger.info('returning dummy for getRI()') return False def getCD(self): """Read terminal status line: Carrier Detect""" - if not self._isOpen: raise portNotOpenError + if not self._isOpen: raise PortNotOpenError() if self.logger: self.logger.info('returning dummy for getCD()') return True |