U
    ;V                      @   s@   d dl T dd Zdd ZeddgZdd	 ZG d
d deZdS )    )*c                 C   s2   t | }| d}|dd  D ]}t||}q|S )N.   )
__import__splitgetattr)namemodZ
componentscomp r   3/usr/lib/python3/dist-packages/serial/serialjava.py	my_import   s
    
r   c              
   C   sH   | D ]6}zt |}|j |W   S  ttfk
r8   Y qX qtddS )z1try given list of modules and return that importsz/No Java Communications API implementation foundN)r   
SerialPortImportErrorAttributeError)namesr   r	   r   r   r   detect_java_comm   s    
r   z
javax.commzgnu.ioc                 C   sF   t j }g }| r:| }| t jjkr|| q||   S )z%Turn a port number into a device name)	commCommPortIdentifierZgetPortIdentifiersZhasMoreElementsZnextElementZgetPortTypeZPORT_SERIALappendZgetName)Z
portnumberenumZportsZelr   r   r   device*   s    
r   c                   @   s   e Zd ZdZdd Zdd Zdd Zedd	 Zd%ddZ	dd Z
dd Zdd Zd&ddZdd Zdd Zdd Zedd Zedd Zed d! Zed"d# Zd$S )'Serialz    Serial port class, implemented with Java Communications API and
    thus usable with jython and the appropriate java extension.
    c              
   C   s   | j dkrtd| jr tdt| j tdkrBtj| j }ntjt| j }z|dd| _	W n4 t
k
r } zd| _	td| W 5 d}~X Y nX |   | j	 | _| j	 | _d| _dS )	zx        Open port with current settings. This may throw a SerialException
        if the port cannot be opened.
        Nz.Port must be configured before it can be used.zPort is already open. zpython serial module
   zCould not open port: %sT)Z_portSerialExceptionis_opentyper   r   ZgetPortIdentifierr   opensPort	Exception_reconfigurePortZgetInputStream	_instreamZgetOutputStream
_outstream)selfZportIdmsgr   r   r   r   ;   s     
zSerial.openc                 C   s  | j std| j d | jtkr.tjj}nJ| jtkrBtjj	}n6| jt
krVtjj}n"| jtkrjtjj}ntd| j | jtkrtjj}n6| jtkrtjj}n"| jtkrtjj}ntd| j | jtkrtjj}nb| jtkrtjj}nN| jtkrtjj}n:| jtkrtjj}n$| jtkr*tjj}ntd| j d }}| jr`|tjjO }|tjjO }| jr|tjj O }|tjj!O }| j "| j#||| | j $||B  | j%dkr| j t&| j%d  n
| j '  dS )	z,Set communication parameters on opened port.z'Can only operate on a valid port handle   zunsupported bytesize: %rz"unsupported number of stopbits: %rzunsupported parity type: %rr   i  N)(r   r   ZenableReceiveTimeoutZ	_bytesizeZFIVEBITSr   r   Z
DATABITS_5ZSIXBITSZ
DATABITS_6Z	SEVENBITSZ
DATABITS_7Z	EIGHTBITSZ
DATABITS_8
ValueErrorZ	_stopbitsZSTOPBITS_ONEZ
STOPBITS_1ZSTOPBITS_ONE_POINT_FIVEZSTOPBITS_1_5ZSTOPBITS_TWOZ
STOPBITS_2Z_parityZPARITY_NONEZPARITY_EVENZ
PARITY_ODDZPARITY_MARKZPARITY_SPACEZ_rtsctsZFLOWCONTROL_RTSCTS_INZFLOWCONTROL_RTSCTS_OUTZ_xonxoffZFLOWCONTROL_XONXOFF_INZFLOWCONTROL_XONXOFF_OUTZsetSerialPortParamsZ	_baudrateZsetFlowControlModeZ_timeoutintZdisableReceiveTimeout)r$   Z	jdatabitsZ	jstopbitsZjparityZjflowinZjflowoutr   r   r   r!   R   sT    





















zSerial._reconfigurePortc                 C   s:   | j r6| jr0| j  | j  | j  d| _d| _ dS )z
Close portNF)r   r   r"   closer#   r$   r   r   r   r)      s    


zSerial.closec                 C   s   | j s
t| j S )z>Return the number of characters currently in the input buffer.)r   portNotOpenErrorr"   	availabler*   r   r   r   
in_waiting   s    zSerial.in_waitingr   c                 C   sX   | j s
tt }|dkrPt||k rP| j }|dkrD| jdkrNqPq|| qt|S )z        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.
        r   )	r   r+   	bytearraylenr"   readZtimeoutr   bytes)r$   sizer1   xr   r   r   r1      s    

zSerial.readc                 C   s@   | j s
tt|ttfs,tdtt|f | j| t	|S )z-Output the given string over the serial port.z expected %s or bytearray, got %s)
r   r+   
isinstancer2   r/   	TypeErrorr   r#   writer0   )r$   datar   r   r   r7      s    zSerial.writec                 C   s    | j s
t| j| j  dS )z9Clear input buffer, discarding all that is in the buffer.N)r   r+   r"   skipr,   r*   r   r   r   reset_input_buffer   s    zSerial.reset_input_bufferc                 C   s   | j s
t| j  dS )zs        Clear output buffer, aborting the current output and
        discarding all that is in the buffer.
        N)r   r+   r#   flushr*   r   r   r   reset_output_buffer   s    zSerial.reset_output_buffer      ?c                 C   s   | j s
t| j |d  dS )zHSend break condition. Timed, returns to idle state after given duration.g     @@N)r   r+   Z	sendBreak)r$   Zdurationr   r   r   
send_break   s    zSerial.send_breakc                 C   s   | j dkrttddS )zBSet break: Controls TXD. When active, to transmitting is possible.Nz<The _update_break_state function is not implemented in java.)fdr+   r   r*   r   r   r   _update_break_state   s    
zSerial._update_break_statec                 C   s   | j s
t| j | j dS )z)Set terminal status line: Request To SendN)r   r+   ZsetRTSZ
_rts_stater*   r   r   r   _update_rts_state   s    zSerial._update_rts_statec                 C   s   | j s
t| j | j dS )z-Set terminal status line: Data Terminal ReadyN)r   r+   ZsetDTRZ
_dtr_stater*   r   r   r   _update_dtr_state   s    zSerial._update_dtr_statec                 C   s   | j s
t| j   dS )z(Read terminal status line: Clear To SendN)r   r+   ZisCTSr*   r   r   r   cts   s    z
Serial.ctsc                 C   s   | j s
t| j   dS )z)Read terminal status line: Data Set ReadyN)r   r+   ZisDSRr*   r   r   r   dsr   s    z
Serial.dsrc                 C   s   | j s
t| j   dS )z)Read terminal status line: Ring IndicatorN)r   r+   ZisRIr*   r   r   r   ri   s    z	Serial.ric                 C   s   | j s
t| j   dS )z)Read terminal status line: Carrier DetectN)r   r+   ZisCDr*   r   r   r   cd   s    z	Serial.cdN)r   )r=   )__name__
__module____qualname____doc__r   r!   r)   propertyr-   r1   r7   r:   r<   r>   r@   rA   rB   rC   rD   rE   rF   r   r   r   r   r   5   s,   7

		



r   N)Zserial.serialutilr   r   r   r   Z
SerialBaser   r   r   r   r   <module>
   s   