o
    ^_ !                     @   sL   d dl mZ 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 )    )absolute_import)*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   s@   | D ]}zt |}|j |W   S  ttfy   Y qw td)z1try given list of modules and return that importsz/No Java Communications API implementation found)r   
SerialPortImportErrorAttributeError)namesr	   r
   r   r   r   detect_java_comm   s   
r   z
javax.commzgnu.ioc                 C   sL   t j }g }| r | }| t jjkr|| | s||   S )z%Turn a port number into a device name)	commCommPortIdentifierZgetPortIdentifiersZhasMoreElementsZnextElementZgetPortTypeZPORT_SERIALappendZgetName)Z
portnumberenumZports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u r	td| jrtdt| j tdkr!tj| j }n	tjt| j }z	|dd| _	W n t
yH } z	d| _	td| d}~ww |   | 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}n%| jtkr!tjj	}n| jt
kr+tjj}n| jtkr5tjj}ntd| j | jtkrFtjj}n| jtkrPtjj}n| jtkrZtjj}ntd| j | jtkrktjj}n/| jtkrutjj}n%| jtkrtjj}n| jtkrtjj}n| jtkrt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  dS | 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#   T   sT   
























zSerial._reconfigurePortc                 C   s>   | j r| jr| j  | j  | j  d| _d| _ dS 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   sl   | j st t }|dkr2t||k r2| j }|dkr'| jdkr&	 t|S n|| t||k s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&   sizer3   xr   r   r   r3      s   


zSerial.readc                 C   sB   | 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-   
isinstancer4   r1   	TypeErrorr   r%   writer2   )r&   datar   r   r   r9      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u rt td)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      | 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      zSerial._update_rts_statec                 C   rC   )z-Set terminal status line: Data Terminal ReadyN)r!   r-   ZsetDTRZ
_dtr_stater,   r   r   r   _update_dtr_state   rE   zSerial._update_dtr_statec                 C      | j st | j   dS )z(Read terminal status line: Clear To SendN)r!   r-   ZisCTSr,   r   r   r   cts      z
Serial.ctsc                 C   rG   )z)Read terminal status line: Data Set ReadyN)r!   r-   ZisDSRr,   r   r   r   dsr   rI   z
Serial.dsrc                 C   rG   )z)Read terminal status line: Ring IndicatorN)r!   r-   ZisRIr,   r   r   r   ri   rI   z	Serial.ric                 C   rG   )z)Read terminal status line: Carrier DetectN)r!   r-   ZisCDr,   r   r   r   cd   rI   z	Serial.cdN)r   )r?   )__name__
__module____qualname____doc__r    r#   r+   propertyr/   r3   r9   r<   r>   r@   rB   rD   rF   rH   rJ   rK   rL   r   r   r   r   r   7   s.    7

	
	


r   N)	Z
__future__r   Zserial.serialutilr   r   r   r   Z
SerialBaser   r   r   r   r   <module>   s   	