
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
3
M]W                  @   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   s6   t | }| jd}x|dd  D ]}t||}q W |S )N.   )
__import__splitgetattr)namemodZ
componentscomp r    /usr/lib/python3.6/serialjava.py	my_import   s
    
r   c             C   sF   x8| D ]0}yt |}|j |S  ttfk
r4   Y qX qW 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   sJ   t jj }g }x.|j r<|j }|j t jjkr|j| qW ||  j 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| j }ntjjt| j }y|jdd| _	W n4 t
k
r } zd| _	td| W Y dd}~X nX | j  | j	j | _| 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 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}nd| jtkrtjj}nP| jtkr tjj}n:| jtkrtjj}n$| jtkr,tjj}ntd| j d }}| jrb|tjjO }|tjjO }| jr|tjj O }|tjj!O }| j j"| j#||| | j j$||B  | j%dkr| j jt&| j%d  n
| j 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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j S )z>Return the number of characters currently in the input buffer.)r   portNotOpenErrorr"   	available)r$   r   r   r   
in_waiting   s    zSerial.in_waitingr   c             C   s\   | j s
tt }|dkrTx:t||k rR| jj }|dkrF| jdkrPP q|j| qW 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   r*   	bytearraylenr"   readZtimeoutr   bytes)r$   sizer0   xr   r   r   r0      s    

zSerial.readc             C   s@   | j s
tt|ttfs,tdtt|f | jj| t	|S )z-Output the given string over the serial port.z expected %s or bytearray, got %s)
r   r*   
isinstancer1   r.   	TypeErrorr   r#   writer/   )r$   datar   r   r   r6      s    zSerial.writec             C   s    | j s
t| jj| 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j  dS )zs        Clear output buffer, aborting the current output and
        discarding all that is in the buffer.
        N)r   r*   r#   flush)r$   r   r   r   reset_output_buffer   s    zSerial.reset_output_buffer      ?c             C   s   | j s
t| j 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| j dS )z)Set terminal status line: Request To SendN)r   r*   ZsetRTSZ
_rts_state)r$   r   r   r   _update_rts_state   s    zSerial._update_rts_statec             C   s   | j s
t| j j| j dS )z-Set terminal status line: Data Terminal ReadyN)r   r*   ZsetDTRZ
_dtr_state)r$   r   r   r   _update_dtr_state   s    zSerial._update_dtr_statec             C   s   | j s
t| j j  dS )z(Read terminal status line: Clear To SendN)r   r*   ZisCTS)r$   r   r   r   cts   s    z
Serial.ctsc             C   s   | j s
t| j j  dS )z)Read terminal status line: Data Set ReadyN)r   r*   ZisDSR)r$   r   r   r   dsr   s    z
Serial.dsrc             C   s   | j s
t| j j  dS )z)Read terminal status line: Ring IndicatorN)r   r*   ZisRI)r$   r   r   r   ri   s    z	Serial.ric             C   s   | j s
t| j j  dS )z)Read terminal status line: Carrier DetectN)r   r*   ZisCD)r$   r   r   r   cd   s    z	Serial.cdN)r   )r<   )__name__
__module____qualname____doc__r   r!   r)   propertyr,   r0   r6   r9   r;   r=   r?   r@   rA   rB   rC   rD   rE   r   r   r   r   r   5   s"   7
		
r   N)Zserial.serialutilr   r   r   r   Z
SerialBaser   r   r   r   r   <module>
   s   