U
    zYW#                     @   s@   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 )    N)*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krtd| jr tdztjj| j| _W n: t	k
rp } zd| _td| j|f W 5 d}~X Y nX | j
dkrd| _
| jdkrd| _|   | j  d| _| js|   | 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   s  | j std| jdkr*tjjjj| j _nt	| jd | j _| j
dkrXtjjjj| j _nt	| j
d | j _z| j| j _W n. tk
r } ztt|W 5 d}~X Y nX | jtkrd| j _nJ| jtkrd| j _n6| jtkrd| j _n"| jtkrd| j _ntd| j | jtkr(ttjjjd	| j _n| jtkrFtjjjj| j _nh| jtkrdtjjjj| j _nJ| 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 _%nJ| j#t'krtjjj%j(| j _%n,| j#t)krtjjj%j*| j _%ntd| j# | j+r8| j,r8tjjj-j.| j _-nH| j+rRtjjj-j/| j _-n.| j,rltjjj-j0| j _-n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   sF   | j rB| jr<z| j  W n tjjjk
r4   Y nX d| _d| _ dS )z
Close portNF)r   r   ZCloser   r   r   ZInvalidOperationExceptionr"   r   r   r   close   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   ZBytesToReadr0   r   r   r   
in_waiting   s    zSerial.in_waiting   c                 C   sV   | j s
tt }|rNz|| j  W n tjk
rB   Y qNY qX |d8 }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.
        r4   )	r   r2   	bytearrayappendr   ZReadByter   TimeoutExceptionbytes)r"   sizedatar   r   r   read   s    

zSerial.readc                 C   sJ   | j s
tz| jt|dt| W n tjk
r@   tY nX t|S )z-Output the given string over the serial port.r   )	r   r2   r   ZWriter   lenr   r7   ZwriteTimeoutError)r"   r:   r   r   r   write   s    
zSerial.writec                 C   s   | j s
t| j  dS )z9Clear input buffer, discarding all that is in the buffer.N)r   r2   r   ZDiscardInBufferr0   r   r   r   r!      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   r2   r   ZDiscardOutBufferr0   r   r   r   reset_output_buffer   s    zSerial.reset_output_bufferc                 C   s   | j s
tt| j| j_dS )zT
        Set break: Controls TXD. When active, to transmitting is possible.
        N)r   r2   boolZ_break_stater   Z
BreakStater0   r   r   r   _update_break_state   s    zSerial._update_break_statec                 C   s   | j s
tt| j| j_dS )z)Set terminal status line: Request To SendN)r   r2   r?   r   r   Z	RtsEnabler0   r   r   r   r       s    zSerial._update_rts_statec                 C   s   | j s
tt| j| j_dS )z-Set terminal status line: Data Terminal ReadyN)r   r2   r?   r   r   Z	DtrEnabler0   r   r   r   r      s    zSerial._update_dtr_statec                 C   s   | j s
t| jjS )z(Read terminal status line: Clear To Send)r   r2   r   Z
CtsHoldingr0   r   r   r   cts   s    z
Serial.ctsc                 C   s   | j s
t| jjS )z)Read terminal status line: Data Set Ready)r   r2   r   Z
DsrHoldingr0   r   r   r   dsr   s    z
Serial.dsrc                 C   s   | j s
tdS )z)Read terminal status line: Ring IndicatorF)r   r2   r0   r   r   r   ri   s    z	Serial.ric                 C   s   | j s
t| jjS )z)Read terminal status line: Carrier Detect)r   r2   r   Z	CDHoldingr0   r   r   r   cd   s    z	Serial.cdN)r4   )__name__
__module____qualname____doc__Z	BAUDRATESr$   r   r1   propertyr3   r;   r=   r!   r>   r@   r    r   rA   rB   rC   rD   r   r   r   r   r      s,   H

	


r   )	r   ZSystem.IO.PortsZserial.serialutilZArrayZByter	   r   Z
SerialBaser   r   r   r   r   <module>
   s
   