3 M�]WZJ � @ sT d dl Z d dlZd dlmZ d dlZd dlmZmZmZmZm Z G dd� de�Z dS )� N)�win32)� SerialBase�SerialException�to_bytes�portNotOpenError�writeTimeoutErrorc s e Zd ZdZdIZ� fdd�Zdd� Zdd� Zdd� Zdd� Z e dd� �ZdJd d!�Zd"d#� Z d$d%� Zd&d'� Zd(d)� Zd*d+� Zd,d-� Zd.d/� Zd0d1� Ze d2d3� �Ze d4d5� �Ze d6d7� �Ze d8d9� �ZdKd<d=�ZdLd?d@�Ze dAdB� �ZdCdD� ZdEdF� ZdGdH� Z� ZS )M�Serialz5Serial port implementation for Win32 based on ctypes.�2 �K �n � � �� �, �X � � �` � �% � K � � � � � � c s( d | _ d | _d | _tt| �j||� d S )N)�_port_handle�_overlapped_read�_overlapped_write�superr �__init__)�self�args�kwargs)� __class__� �!/usr/lib/python3.6/serialwin32.pyr s zSerial.__init__c C s� | j dkrtd��| jr td��| j}y.|j� jd�rRt|dd� �dkrRd| }W n tk rh Y nX tj |tj tjB ddtjtj tjB d�| _| jtjkr�d| _td j| jtj� ���y�tj� | _tjdd dd�| j_tj� | _tjdddd�| j_tj| jdd� tj� | _tj| jtj| j�� | j� tj | jtj!tj"B tj#B tj$B � W n. y| j%� W n Y nX d| _� Y nX d| _dS ) 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.zPort is already open.ZCOM� � z\\.\r zcould not open port {!r}: {!r}� i T)&Z_portr �is_open�name�upper� startswith�int� ValueErrorr Z CreateFileZGENERIC_READZ GENERIC_WRITEZ OPEN_EXISTING�FILE_ATTRIBUTE_NORMALZFILE_FLAG_OVERLAPPEDr ZINVALID_HANDLE_VALUE�formatZportstr�ctypes�WinErrorZ OVERLAPPEDr ZCreateEvent�hEventr � SetupComm�COMMTIMEOUTS�_orgTimeoutsZGetCommTimeouts�byref�_reconfigure_port� PurgeComm� PURGE_TXCLEAR� PURGE_TXABORT� PURGE_RXCLEAR� PURGE_RXABORT�_close)r Zportr# r# r$ �open! sR " zSerial.openc C s� | j std��tj� }| jdkr"n*| jdkr6tj|_ntt| jd �d�|_ | jdkrv| j dk rvtt| j d �d�|_| jdkr�n*| jdkr�tj|_ntt| jd �d�|_tj | j tj|�� tj| j tj� tj� }tj| j tj|�� | j|_| jtjk�r d|_nR| jtjk�r d|_n<| jtjk�r6d|_n&| jtjk�rLd |_ntd j| j���| jtj k�rztj!|_"d|_#n�| jtj$k�r�tj%|_"d|_#nj| jtj&k�r�tj'|_"d|_#nL| jtj(k�r�tj)|_"d|_#n.| jtj*k�r�tj+|_"d|_#ntdj| j���| j,tj-k�rtj.|_/n@| j,tj0k�r2tj1|_/n(| j,tj2k�rJtj3|_/ntdj| j,���d|_4| j5dk�r�| j6�r~tj7|_8n| j9�r�tj:ntj;|_8| j6|_<n�| j5j=�s�td j| j5j=���| j5j>�r�tdj| j5j>���| j5j?dk �r�tdj| j5j?���| j5j@dk �rtdj| j5j@���| j5jA�r2tdj| j5jA���tjB|_8d|_<| jC�rRtjD|_En| jF�r`tjGntjH|_E| jC|_I| jJ|_K| jJ|_Ld|_Md|_Nd|_OtjP|_QtjR|_StjT| j tj|���s�tdjtjU� ���dS )z,Set communication parameters on opened port.z'Can only operate on a valid port handleNr i� r'