# -*- 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.io) """ import ast import select import socket import time from secop.errors import CommunicationFailedError, ConfigError from secop.lib import closeSocket, parseHostPort, tcpSocket try: from serial import Serial except ImportError: Serial = None class ConnectionClosed(ConnectionError): pass class AsynConn: timeout = 1 # inter byte timeout scheme = None 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[?