o
    l@xd !                     @   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mod
componentscomp r   9D:\rfid_django\env\Lib\site-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append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setFlowControlMode_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#   r0   	bytearraylenr&   readtimeoutr   bytes)r(   sizer6   xr   r   r   r6      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#   r0   
isinstancer8   r4   	TypeErrorr!   r'   writer5   )r(   datar   r   r   r=      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#   r0   r&   skipr1   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#   r0   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#   r0   Z	sendBreak)r(   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.)fdr0   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#   r0   ZsetRTSZ
_rts_stater/   r   r   r   _update_rts_state      zSerial._update_rts_statec                 C   rH   )z-Set terminal status line: Data Terminal ReadyN)r#   r0   ZsetDTRZ
_dtr_stater/   r   r   r   _update_dtr_state   rJ   zSerial._update_dtr_statec                 C      | j st | j   dS )z(Read terminal status line: Clear To SendN)r#   r0   ZisCTSr/   r   r   r   cts      z
Serial.ctsc                 C   rL   )z)Read terminal status line: Data Set ReadyN)r#   r0   ZisDSRr/   r   r   r   dsr   rN   z
Serial.dsrc                 C   rL   )z)Read terminal status line: Ring IndicatorN)r#   r0   ZisRIr/   r   r   r   ri   rN   z	Serial.ric                 C   rL   )z)Read terminal status line: Carrier DetectN)r#   r0   ZisCDr/   r   r   r   cd   rN   z	Serial.cdN)r   )rC   )__name__
__module____qualname____doc__r"   r%   r.   propertyr2   r6   r=   r@   rB   rE   rG   rI   rK   rM   rO   rP   rQ   r   r   r   r   r   7   s.    7

	
	


r   N)	
__future__r   Zserial.serialutilr   r   r   r   Z
SerialBaser   r   r   r   r   <module>   s   	