
    BPc                     ~    d Z ddlZddlZddlZddlmZmZ 	 ddlZddlm	Z	 n# e
$ r dZY nw xY w G d d          ZdS )z3
Common access to serial and other computer ports.
    N   )DependencyErrorAbortExtension)
list_portsc                   Z    e Zd ZdZddZddZd	 Zd
 Zed             Z	ed             Z
dS )Seriala  
    Attempt to get access to the computer's serial port.

    with Serial(port_name, ...) as com:
        com.write(...)

    Provides access to the debug/testing ports which are pretend ports
    able to accept the same input but allow for debugging.
    %  皙?c                 z   |dk    | _         | j         r>dd l}|                                \  | _        | _        t          j        | j                  }|                                  t          j	                    | _
        || j
        _        t          |          | j
        _        || j
        _         | j        di | d S )Nz[test]r    )testptyopenpty
controller
peripheralosttyname
has_serialserialr   comportintbaudratetimeoutset_options)selfr   baudr   optionsr   s         -/usr/share/inkscape/extensions/inkex/ports.py__init__zSerial.__init__.   s    H$	9 	/JJJ/2{{}},DOT_:do..D=??II"##7#####    r      Nc                    ddddd                     ||          }ddi                     |                                |          }dd	d
d                     ||          }t          t          t	          t	          |                                                    dz             | j        _        t          t          dt	          |                                          z             | j        _        t          t          dt	          |                                          z             | j        _	        |dk    | j        _
        |dv | j        _        |dk    | j        _        dS )z&Set further options on the serial portfivesixseveneight)         r"   onepointfive      ?oneone_point_fivetwo)r   r,      BITS	STOPBITS_PARITY_xonxoff)rtsctsdsrdtrrtsctsr6   N)getlowergetattrr   strupperr   bytesizestopbitsparityr4   r5   dsrdtr)r   stopsizeflowr>   s        r   r   zSerial.set_options=   s   eG<<@@tLL$((t<</E::>>tTJJ#FCD		0A0A,B,BV,KLL#FK#d))//:K:K,KLL!&)c&kk6G6G6I6I*IJJ9,"<<.0r!   c                     	 | j                                          n'# t          j        $ r}t	          d          |d }~ww xY w| j         S )NzgCould not open serial port. Please check your device is running, connected and the settings are correct)r   openr   SerialExceptionr   )r   errors     r   	__enter__zSerial.__enter__J   s`    	HMMOOOO% 	 	 	 F  	
 xs    A ;A c                 Z   |s| j         rd}t          |          dk    rst          j        d           t	          j        | j        d          }t          j        	                    |
                    d                     t          |          dk    s| j                                         d S )Na                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   i   g{Gz?utf8)r   lentimesleepr   readr   sysstderrwritedecoder   close)r   excvalue	tracebackoutputs        r   __exit__zSerial.__exit__U   s     	8TY 	8Ff++%%
4   $77
  v!6!6777 f++%%
 	r!   c                  2    t           t          d          dS )z!Late importing of pySerial moduleNz*pySerial is required to open serial ports.)r   r   r   r!   r   r   zSerial.has_serial_   s     >!"NOOO >r!   c                  p    t                                            d t          j        d          D             S )z'Return a list of available serial portsc                     g | ]	}|j         
S r   )name).0hws     r   
<listcomp>z%Serial.list_ports.<locals>.<listcomp>i   s    <<<B<<<r!   T)r   r   r   comportsr   r!   r   r   zSerial.list_portse   s5     	<<*"5d";";<<<<r!   )r	   r
   )r   r"   NN)__name__
__module____qualname____doc__r    r   rG   rW   staticmethodr   r   r   r!   r   r   r   #   s         $ $ $ $1 1 1 1	 	 	   P P \P
 = = \= = =r!   r   )rc   r   rN   rK   utilsr   r   r   serial.toolsr   ImportErrorr   r   r!   r   <module>rh      s   &  
			 



  2 2 2 2 2 2 2 2MMM'''''''   FFFF= F= F= F= F= F= F= F= F= F=s   
# --