o
    ^_#                     @   sL   d dl mZ d dlZd dlZd dlT ejej Zdd ZG dd de	Z
dS )    )absolute_importN)*c                 C   s   t dd | D S )Nc                 S   s   g | ]}t |qS  )ord).0xr   r   2/usr/lib/python3/dist-packages/serial/serialcli.py
<listcomp>   s    z!as_byte_array.<locals>.<listcomp>)sab)stringr   r   r   as_byte_array   s   r   c                   @   s   e Zd 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 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 implementation for .NET/Mono.)2   K   n            i,  iX  i  i  i`	  i  i%  i K  i   i   i  c              
   C   s   | j du r	td| jrtdztjj| j| _W n t	y3 } zd| _td| j|f d}~ww | j
du r<d| _
| jdu rDd| _|   | j  d| _| jsW|   | js^|   |   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could not open port %s: %sT)Z_portSerialExceptionis_openSystemIOPorts
SerialPortZportstr_port_handle	Exception
_rts_state
_dtr_state_reconfigure_portZOpenZ_dsrdtr_update_dtr_state_rtscts_update_rts_statereset_input_buffer)selfmsgr   r   r   open   s.   



zSerial.openc              
   C   sp  | j std| jdu rtjjjj| j _n	t	| jd | j _| j
du r,tjjjj| j _n	t	| j
d | j _z| j| j _W n tyN } ztt|d}~ww | jtkrYd| j _n%| jtkrcd| j _n| jtkrmd| j _n| jtkrwd| j _ntd| j | jtkrttjjjd	| j _n?| jtkrtjjjj| j _n1| jtkrtjjjj| j _n#| jtkrtjjjj | j _n| jt!krtjjjj"| j _ntd
| j | j#t$krtjjj%j&| j _%n#| j#t'krtjjj%j(| j _%n| j#t)krtjjj%j*| j _%ntd| j# | j+r| j,rtjjj-j.| j _-dS | j+rtjjj-j/| j _-dS | j,r,tjjj-j0| j _-dS ttjjj-d	| j _-dS )z,Set communication parameters on opened port.z'Can only operate on a valid port handleNi              z#Unsupported number of data bits: %rNonezUnsupported parity mode: %rz#Unsupported number of stop bits: %r)1r   r   Z_timeoutr   r   r   r   ZInfiniteTimeoutZReadTimeoutintZ_write_timeoutZWriteTimeoutZ	_baudrateZBaudRateIOError
ValueErrorstrZ	_bytesizeZFIVEBITSZDataBitsZSIXBITSZ	SEVENBITSZ	EIGHTBITSZ_parityZPARITY_NONEgetattrZParityZPARITY_EVENZEvenZ
PARITY_ODDZOddZPARITY_MARKZMarkZPARITY_SPACEZSpaceZ	_stopbitsZSTOPBITS_ONEZStopBitsZOneZSTOPBITS_ONE_POINT_FIVEZOnePointFiveZSTOPBITS_TWOZTwor    Z_xonxoffZ	HandshakeZRequestToSendXOnXOffZRequestToSendZXOnXOff)r#   er   r   r   r   =   s`   

















zSerial._reconfigure_portc                 C   sH   | j r"| jrz| j  W n tjjjy   Y nw d| _d| _ dS dS )z
Close portNF)r   r   ZCloser   r   r   ZInvalidOperationExceptionr#   r   r   r   close   s   
zSerial.closec                 C      | j st | jjS )z>Return the number of characters currently in the input buffer.)r   PortNotOpenErrorr   ZBytesToReadr1   r   r   r   
in_waiting      zSerial.in_waiting   c                 C   sZ   | j st t }|r)z
|| j  W n tjy"   Y t|S w |d8 }|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.
        r7   )	r   r4   	bytearrayappendr   ZReadByter   TimeoutExceptionbytes)r#   sizedatar   r   r   read   s   zSerial.readc                 C   sH   | j st z| jt|dt| W t|S  tjy#   tdw )z-Output the given string over the serial port.r   zWrite timeout)	r   r4   r   ZWriter   lenr   r:   ZSerialTimeoutException)r#   r=   r   r   r   write   s   zSerial.writec                 C      | j st | j  dS )z9Clear input buffer, discarding all that is in the buffer.N)r   r4   r   ZDiscardInBufferr1   r   r   r   r"      s   zSerial.reset_input_bufferc                 C   rA   )zs        Clear output buffer, aborting the current output and
        discarding all that is in the buffer.
        N)r   r4   r   ZDiscardOutBufferr1   r   r   r   reset_output_buffer   s   zSerial.reset_output_bufferc                 C      | j st t| j| j_dS )zT
        Set break: Controls TXD. When active, to transmitting is possible.
        N)r   r4   boolZ_break_stater   Z
BreakStater1   r   r   r   _update_break_state   s   zSerial._update_break_statec                 C   rC   )z)Set terminal status line: Request To SendN)r   r4   rD   r   r   Z	RtsEnabler1   r   r   r   r!         zSerial._update_rts_statec                 C   rC   )z-Set terminal status line: Data Terminal ReadyN)r   r4   rD   r   r   Z	DtrEnabler1   r   r   r   r      rF   zSerial._update_dtr_statec                 C   r3   )z(Read terminal status line: Clear To Send)r   r4   r   Z
CtsHoldingr1   r   r   r   cts   r6   z
Serial.ctsc                 C   r3   )z)Read terminal status line: Data Set Ready)r   r4   r   Z
DsrHoldingr1   r   r   r   dsr   r6   z
Serial.dsrc                 C   s   | j st dS )z)Read terminal status line: Ring IndicatorF)r   r4   r1   r   r   r   ri   s   z	Serial.ric                 C   r3   )z)Read terminal status line: Carrier Detect)r   r4   r   Z	CDHoldingr1   r   r   r   cd   r6   z	Serial.cdN)r7   )__name__
__module____qualname____doc__Z	BAUDRATESr%   r   r2   propertyr5   r>   r@   r"   rB   rE   r!   r   rG   rH   rI   rJ   r   r   r   r   r      s.    H

	


r   )Z
__future__r   r   ZSystem.IO.PortsZserial.serialutilZArrayZByter
   r   Z
SerialBaser   r   r   r   r   <module>   s   	