U
    �AcjÀ  ã                   @   sd   d Z ddlZddlZddlmZmZmZ ddlmZ ddl	m
Z
 ddlmZ G dd„ dee
jƒZdS )	zB
Serial port support for Windows.

Requires PySerial and pywin32.
é    N)Ú	EIGHTBITSÚPARITY_NONEÚSTOPBITS_ONE)Úto_bytes)Úabstract)ÚBaseSerialPortc                   @   sZ   e Zd ZdZdZdeeeddfdd„Zdd„ Z	d	d
„ Z
dd„ Zdd„ Zdd„ Zdd„ ZdS )Ú
SerialPortz@A serial device, acting as a transport, that uses a win32 event.é   i€%  r   c
           
   
   C   sÒ   | j |||||d ||	d�| _|  ¡  |  ¡  || _|| _g | _d| _d| _d| _	|| _t
 ¡ | _t d ddd ¡| j_t
 ¡ | _t d ddd ¡| j_| j | jj| d¡ | j | jj| d¡ | j | ¡ |  ¡  d S )N)ÚbaudrateÚbytesizeÚparityÚstopbitsÚtimeoutÚxonxoffÚrtsctsr   r	   ÚserialReadEventÚserialWriteEvent)Z_serialFactoryÚ_serialZ
flushInputZflushOutputÚreactorÚprotocolÚoutQueueÚclosedZclosedNotifiesÚwriteInProgressÚ	win32fileZ
OVERLAPPEDÚ_overlappedReadÚ
win32eventZCreateEventÚhEventÚ_overlappedWriteZaddEventZmakeConnectionÚ_finishPortSetup)
Úselfr   ZdeviceNameOrPortNumberr   r
   r   r   r   r   r   © r    úE/tmp/pip-unpacked-wheel-nrvc_22y/twisted/internet/_win32serialport.pyÚ__init__    s6    ø


zSerialPort.__init__c                 C   s0   |   ¡ \}}t | jjt d¡| j¡\}| _dS )zn
        Finish setting up the serial port.

        This is a separate method to facilitate testing.
        r	   N)Ú_clearCommErrorr   ÚReadFiler   Ú_port_handleÚAllocateReadBufferr   Úread_buf)r   ÚflagsÚcomstatÚrcr    r    r!   r   K   s    ýzSerialPort._finishPortSetupc                 C   s   t  | jj¡S )N)r   ZClearCommErrorr   r%   )r   r    r    r!   r#   X   s    zSerialPort._clearCommErrorc                 C   sÖ   t  | jj| jd¡}t| jd |… ƒ}|  ¡ \}}|jr˜t	 
| jj¡ t  | jjt  |j¡| j¡\}}t  | jj| jd¡}| j |t|d |… ƒ ¡ n| j |¡ t	 
| jj¡ t  | jjt  d¡| j¡\}| _d S )Nr   r	   )r   ZGetOverlappedResultr   r%   r   r   r'   r#   ZcbInQuer   Z
ResetEventr   r$   r&   r   ZdataReceived)r   ÚnÚfirstr(   r)   r*   Úbufr    r    r!   r   [   s6      ÿ
ý  ÿýzSerialPort.serialReadEventc                 C   s6   |r2| j r| j |¡ nd| _ t | jj|| j¡ d S )Nr	   )r   r   Úappendr   Ú	WriteFiler   r%   r   )r   Údatar    r    r!   Úwrite{   s      ÿzSerialPort.writec                 C   sF   z| j  d¡}W n tk
r,   d| _Y d S X t | jj|| j¡ d S )Nr   )	r   ÚpopÚ
IndexErrorr   r   r/   r   r%   r   )r   ZdataToWriter    r    r!   r   …   s      ÿzSerialPort.serialWriteEventc                 C   sH   | j  | jj¡ | j  | jj¡ tj | |¡ | j 	¡  | j
 |¡ dS )z™
        Called when the serial port disconnects.

        Will call C{connectionLost} on the protocol that is handling the
        serial data.
        N)r   ZremoveEventr   r   r   r   ÚFileDescriptorÚconnectionLostr   Úcloser   )r   Úreasonr    r    r!   r5   �   s
    
zSerialPort.connectionLostN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	connectedr   r   r   r"   r   r#   r   r1   r   r5   r    r    r    r!   r      s   ö
+ 
r   )r;   r   r   Úserialr   r   r   Zserial.serialutilr   Ztwisted.internetr   Ztwisted.internet.serialportr   r4   r   r    r    r    r!   Ú<module>   s   