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   