o
    ¯b2  ã                   @   sl   d Z ddlZddlZddlmZ ddl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)ÚPARITY_NONE)Ú	EIGHTBITSÚ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)Ú_serialFactoryÚ_serialÚ
flushInputÚflushOutputÚreactorÚprotocolÚoutQueueÚclosedÚclosedNotifiesÚwriteInProgressÚ	win32fileÚ
OVERLAPPEDÚ_overlappedReadÚ
win32eventÚCreateEventÚhEventÚ_overlappedWriteÚaddEventÚmakeConnectionÚ_finishPortSetup)
Úselfr   ÚdeviceNameOrPortNumberr   r
   r   r   r   r   r   © r)   úC/usr/lib/python3/dist-packages/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&   L   s   ýzSerialPort._finishPortSetupc                 C   s   t  | jj¡S )N)r   ÚClearCommErrorr   r.   )r'   r)   r)   r*   r,   Y   s   zSerialPort._clearCommErrorc                 C   sÖ   t  | jj| jd¡}t| jd |… ƒ}|  ¡ \}}|jrLt	 
| 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   ÚGetOverlappedResultr   r.   r   r   r0   r,   ÚcbInQuer    Ú
ResetEventr"   r-   r/   r   ÚdataReceived)r'   ÚnÚfirstr1   r2   r3   Úbufr)   r)   r*   r   \   s.   ÿ
ýÿýzSerialPort.serialReadEventc                 C   s<   |r| j r| j |¡ d S d| _ t | jj|| j¡ d S d S )Nr	   )r   r   Úappendr   Ú	WriteFiler   r.   r#   )r'   Údatar)   r)   r*   Úwrite|   s   ÿûzSerialPort.writec                 C   sD   z| j  d¡}W n ty   d| _Y d S w t | jj|| j¡ d S )Nr   )	r   ÚpopÚ
IndexErrorr   r   r=   r   r.   r#   )r'   Ú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   ÚremoveEventr   r"   r#   r   ÚFileDescriptorÚconnectionLostr   Úcloser   )r'   Úreasonr)   r)   r*   rE   ‘   s
   
zSerialPort.connectionLostN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú	connectedr   r   r   r+   r&   r,   r   r?   r   rE   r)   r)   r)   r*   r      s     
ö+ 
r   )rK   r    r   Úserialr   r   r   Úserial.serialutilr   Útwisted.internetr   Útwisted.internet.serialportr   rD   r   r)   r)   r)   r*   Ú<module>   s   