o +a_Serialz5Serial port implementation for Win32 based on ctypes.)2Kni,iXiii` ii%iKiiics,d|_d|_d|_tt|j|i|dSN) _port_handle_overlapped_read_overlapped_writesuperr __init__)selfargskwargs __class__4/usr/lib/python3/dist-packages/serial/serialwin32.pyrszSerial.__init__c Cs|jdur td|jrtd|j}z|dr)t|dddkr)d|}Wn ty3Ynwt |tj tj Bddtj tj tjBd|_|jtjkr]d|_td |jtzMt|_tdd dd|j_t|_tdddd|j_t|jd d t|_t|jt|j|t |jtj!tj"Btj#Btj$BWnz |%Wd|_Yd|_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.COMz\\.\rzcould not open port {!r}: {!r}T)&_portris_opennameupper startswithint ValueErrorr CreateFile GENERIC_READ GENERIC_WRITE OPEN_EXISTINGFILE_ATTRIBUTE_NORMALFILE_FLAG_OVERLAPPEDrINVALID_HANDLE_VALUEformatportstrctypesWinError OVERLAPPEDr CreateEventhEventr SetupComm COMMTIMEOUTS _orgTimeoutsGetCommTimeoutsbyref_reconfigure_port PurgeComm PURGE_TXCLEAR PURGE_TXABORT PURGE_RXCLEAR PURGE_RXABORT_close)rportrrropen#sf "          z Serial.opencCs|jstdt}|jdurn|jdkrtj|_n tt|jdd|_ |jdkr;|j dur;tt|j dd|_|j durAn|j dkrKtj|_ n tt|j dd|_ t |jt|t|jtjt}t|jt||j|_|jtjkrd|_n&|jtjkrd|_n|jtjkrd|_n|jtjkrd |_ntd |j|jtj krtj!|_"d|_#n@|jtj$krtj%|_"d|_#n2|jtj&krtj'|_"d|_#n$|jtj(krtj)|_"d|_#n|jtj*krtj+|_"d|_#ntd |j|j,tj-krtj.|_/n |j,tj0krtj1|_/n|j,tj2krtj3|_/ntd |j,d|_4|j5durF|j6r6tj7|_8n |j9r=tj:ntj;|_8|j6|_rbtd|j5j>|j5j?durrtd|j5j?|j5j@durtd|j5j@|j5jArtd|j5jAtjB|_8d|_<|jCrtjD|_En |jFrtjGntjH|_E|jC|_I|jJ|_K|jJ|_Ld|_Md|_Nd|_OtjP|_QtjR|_StT|jt|stdtUdS)z,Set communication parameters on opened port.z'Can only operate on a valid port handleNrir rz%Unsupported number of data bits: {!r}zUnsupported parity mode: {!r}z%Unsupported number of stop bits: {!r}zQUnsupported value for RS485Settings.rts_level_for_tx: {!r} (only True is allowed)zRUnsupported value for RS485Settings.rts_level_for_rx: {!r} (only False is allowed)zPUnsupported value for RS485Settings.delay_before_tx: {!r} (only None is allowed)zPUnsupported value for RS485Settings.delay_before_rx: {!r} (only None is allowed)zJUnsupported value for RS485Settings.loopback: {!r} (only False is allowed)zCCannot configure port, something went wrong. Original message: {!r})Vrrrr8_timeoutMAXDWORDReadIntervalTimeoutmaxr'ReadTotalTimeoutConstant_inter_byte_timeout_write_timeoutWriteTotalTimeoutConstantSetCommTimeoutsr2r; SetCommMaskEV_ERRDCB GetCommState _baudrateBaudRate _bytesizeserialFIVEBITSByteSizeSIXBITS SEVENBITS EIGHTBITSr(r0_parity PARITY_NONENOPARITYParityfParity PARITY_EVEN EVENPARITY PARITY_ODD ODDPARITY PARITY_MARK MARKPARITY PARITY_SPACE SPACEPARITY _stopbits STOPBITS_ONE ONESTOPBITStopBitsSTOPBITS_ONE_POINT_FIVE ONE5STOPBITS STOPBITS_TWO TWOSTOPBITSfBinary _rs485_mode_rtsctsRTS_CONTROL_HANDSHAKE fRtsControl _rts_stateRTS_CONTROL_ENABLERTS_CONTROL_DISABLE fOutxCtsFlowrts_level_for_txrts_level_for_rxdelay_before_txdelay_before_rxloopbackRTS_CONTROL_TOGGLE_dsrdtrDTR_CONTROL_HANDSHAKE fDtrControl _dtr_stateDTR_CONTROL_ENABLEDTR_CONTROL_DISABLE fOutxDsrFlow_xonxofffOutXfInXfNull fErrorChar fAbortOnErrorXONXonCharXOFFXoffChar SetCommStater3)rtimeoutscomDCBrrrr<ds                          zSerial._reconfigure_portcCs|jdur>t|j|j|jdur |t|jjd|_|jdur3| t|jjd|_t|jd|_dSdS)zinternal close port helperN) rrrPr9r cancel_read CloseHandler6r cancel_writerrrrrBs     z Serial._closecCs|jr |d|_dSdS)z Close portFN)r#rBrrrrcloses z Serial.closecCDt}t}t|jt|t|stdt |j S)z9Return the number of bytes currently in the input buffer.ClearCommError failed ({!r})) rDWORDCOMSTATClearCommErrorrr2r;rr0r3cbInQuerflagscomstatrrr in_waiting zSerial.in_waitingr c CsX|jst|dkrt|jjt}t}t|j t |t |s0t d t |jdkr;t|j|n|}|dkrt |}t}t|j ||t |t |j}|spttjtjfvrpt d t t|j t |jt |d}|sttjkrt d t |jd|j} t| St} t| St} t| S)z Read size bytes from the serial port. If a timeout is set it may return less characters as requested. With no timeout it will block until the requested number of bytes is read. rrzReadFile failed ({!r})Tz!GetOverlappedResult failed ({!r})N)r#rr ResetEventrr6rrrrr2r;rr0r3timeoutminrcreate_string_bufferReadFile GetLastError ERROR_SUCCESSERROR_IO_PENDINGGetOverlappedResultERROR_OPERATION_ABORTEDrawvaluebytes) rsizerrnbufrcread_ok result_okreadrrrrsH   z Serial.readcCs|jstt|}|rt}t|j|t|t ||j }|j dkr_|s:t tj tjfvr:tdtt|j|j t |dt tjkrQ|jS|jt|kr\td|jS|rdtj nt }|tjtjtjfvrtdS|tj tjfvrt|StdtdS)z2Output the given byte string over the serial port.rzWriteFile failed ({!r})Tz Write timeout)r#rrrr WriteFilerlenr2r;rrNrrrrr0r3rrrrERROR_INVALID_USER_BUFFERERROR_NOT_ENOUGH_MEMORY)rdatarsuccess errorcoderrrwrite/s0   z Serial.writecCs|jr td|jsdSdS)zb Flush of file like objects. In this case, wait until all data is written. g?N) out_waitingtimesleeprrrrflushTs z Serial.flushcC&|jstt|jtjtjBdS)z9Clear input buffer, discarding all that is in the buffer.N)r#rrr=rr@rArrrrreset_input_buffer_szSerial.reset_input_buffercCr)zs Clear output buffer, aborting the current output and discarding all that is in the buffer. N)r#rrr=rr>r?rrrrreset_output_buffereszSerial.reset_output_buffercCs2|jst|jrt|jdSt|jdS)zBSet break: Controls TXD. When active, to transmitting is possible.N)r#r _break_stater SetCommBreakrClearCommBreakrrrr_update_break_statens zSerial._update_break_statecC.|jr t|jtjdSt|jtjdS)z)Set terminal status line: Request To SendN)rxrEscapeCommFunctionrSETRTSCLRRTSrrrr_update_rts_statewzSerial._update_rts_statecCr)z-Set terminal status line: Data Terminal ReadyN)rrrrSETDTRCLRDTRrrrr_update_dtr_state~rzSerial._update_dtr_statecCs.|jstt}t|jt||jSr) r#rrrGetCommModemStatusrr2r;r)rstatrrr_GetCommModemStatuss zSerial._GetCommModemStatuscCtj|@dkS)z(Read terminal status line: Clear To Sendr)r MS_CTS_ONrrrrrctsz Serial.ctscCr)z)Read terminal status line: Data Set Readyr)r MS_DSR_ONrrrrrdsrrz Serial.dsrcCr)z)Read terminal status line: Ring Indicatorr)r MS_RING_ONrrrrrrirz Serial.ricCr)z)Read terminal status line: Carrier Detectr)r MS_RLSD_ONrrrrrcdrz Serial.cdr!NcCs |dur|}t|j||dS)z Recommend a buffer size to the driver (device driver can ignore this value). Must be called after the port is opened. N)rr7r)rrx_sizetx_sizerrrset_buffer_sizeszSerial.set_buffer_sizeTcCs8|jst|rt|jtjdSt|jtjdS)a* Manually control flow - when software flow control is enabled. This will do the same as if XON (true) or XOFF (false) are received from the other device and control the transmission accordingly. WARNING: this function is not portable to different platforms! N)r#rrrrSETXONSETXOFF)renablerrrset_output_flow_controls zSerial.set_output_flow_controlcCr)z0Return how many bytes the in the outgoing bufferr) rrrrrr2r;rr0r3cbOutQuerrrrrrzSerial.out_waitingcCsXt}t|jt|t|d}|s(ttjtjfvr*t |j|dSdSdS)ACancel a blocking read operation, may be called from other threadFN) rrrrr2r;rrERROR_IO_INCOMPLETE CancelIoEx)r overlappedrerrrrr_cancel_overlapped_ioszSerial._cancel_overlapped_iocC||jdS)rN)rrrrrrrzSerial.cancel_readcCr)zBCancel a blocking write operation, may be called from other threadN)rrrrrrrrzSerial.cancel_writecCs.|dur |s td|tjj||dS)z$Change the exclusive access setting.Nz.win32 only supports exclusive access (not: {}))r(r0rXr exclusive__set__)rrrrrrs zSerial.exclusive)r )r!N)T)#__name__ __module__ __qualname____doc__ BAUDRATESrrDr<rBrpropertyrrrrrrrrrrrrrrrrrrrrrrsetter __classcell__rrrrr sJ A  )%           r ) __future__rr2rrXrserial.serialutilrrrrrr rrrrs