# -*- coding: utf-8 -*- # ***************************************************************************** # # 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. # # You should have received a copy of the GNU General Public License along with # this program; if not, write to the Free Software Foundation, Inc., # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # Module authors: # Markus Zolliker # # ***************************************************************************** """asynchronous connections generic class for byte oriented communication includes implementation for TCP and Serial connections support for asynchronous communication, but may be used also for synchronous IO (see secop.stringio.StringIO) """ import socket import select import time import ast try: from serial import Serial except ImportError: Serial = None from secop.lib import parseHostPort, tcpSocket, closeSocket from secop.errors import ConfigError, CommunicationFailedError class ConnectionClosed(ConnectionError): pass class AsynConn: timeout = 1 # inter byte timeout SCHEME_MAP = {} connection = None # is not None, if connected defaultport = None def __new__(cls, uri, end_of_line=b'\n'): scheme = uri.split('://')[0] iocls = cls.SCHEME_MAP.get(scheme, None) if not iocls: # try tcp, if scheme not given try: host_port = parseHostPort(uri, cls.defaultport) except (ValueError, TypeError, AssertionError): if 'COM' in uri: raise ValueError("the correct uri for a COM port is: " "'serial://COM[?