dbfdg 3 M]WZJ@sTddlZddlZddlmZddlZddlmZmZmZmZm Z GdddeZ dS)N)win32) SerialBaseSerialExceptionto_bytesportNotOpenErrorwriteTimeoutErrorcseZdZdZdIZfddZddZddZddZddZ e ddZ dJd d!Z d"d#Z d$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Ze d2d3Ze d4d5Ze d6d7Ze d8d9ZdKdrtdj|j5j>|j5j?dk rtdj|j5j?|j5j@dk rtdj|j5j@|j5jAr2tdj|j5jAtjB|_8d|_<|jCrRtjD|_En|jFr`tjGntjH|_E|jC|_I|jJ|_K|jJ|_Ld|_Md|_Nd|_OtjP|_QtjR|_StjT|jtj|stdjtjUdS)z,Set communication parameters on opened port.z'Can only operate on a valid port handleNrir'r&z%Unsupported number of data bits: {!r}zUnsupported parity mode: {!r}z%Unsupported number of stop bits: {!r}z:Unsupported value for RS485Settings.rts_level_for_tx: {!r}z:Unsupported value for RS485Settings.rts_level_for_rx: {!r}z9Unsupported value for RS485Settings.delay_before_tx: {!r}z9Unsupported value for RS485Settings.delay_before_rx: {!r}z2Unsupported value for RS485Settings.loopback: {!r}zCCannot configure port, something went wrong. Original message: {!r})Vrrrr4Z_timeoutZMAXDWORDZReadIntervalTimeoutmaxr,ZReadTotalTimeoutConstantZ_inter_byte_timeout_write_timeoutZWriteTotalTimeoutConstantSetCommTimeoutsr0r6Z SetCommMaskZEV_ERRZDCBZ GetCommStateZ _baudrateZBaudRateZ _bytesizeserialZFIVEBITSZByteSizeZSIXBITSZ SEVENBITSZ EIGHTBITSr-r/Z_parityZ PARITY_NONEZNOPARITYZParityZfParityZ PARITY_EVENZ EVENPARITYZ PARITY_ODDZ ODDPARITYZ PARITY_MARKZ MARKPARITYZ PARITY_SPACEZ SPACEPARITYZ _stopbitsZ STOPBITS_ONEZ ONESTOPBITZStopBitsZSTOPBITS_ONE_POINT_FIVEZ ONE5STOPBITSZ STOPBITS_TWOZ TWOSTOPBITSZfBinaryZ _rs485_modeZ_rtsctsZRTS_CONTROL_HANDSHAKEZ fRtsControl _rts_stateZRTS_CONTROL_ENABLEZRTS_CONTROL_DISABLEZ fOutxCtsFlowZrts_level_for_txZrts_level_for_rxZdelay_before_txZdelay_before_rxZloopbackZRTS_CONTROL_TOGGLEZ_dsrdtrZDTR_CONTROL_HANDSHAKEZ fDtrControl _dtr_stateZDTR_CONTROL_ENABLEZDTR_CONTROL_DISABLEZ fOutxDsrFlowZ_xonxoffZfOutXZfInXZfNullZ fErrorCharZ fAbortOnErrorZXONZXonCharZXOFFZXoffCharZ SetCommStater1)rZtimeoutsZcomDCBr#r#r$r7bs                     zSerial._reconfigure_portcCs||jdk rxtj|j|j|jdk r@|jtj|jjd|_|jdk rf|j tj|jjd|_tj|jd|_dS)zinternal close port helperN) rrrDr5r cancel_readZ CloseHandler2r cancel_write)rr#r#r$r=s    z Serial._closecCs|jr|jd|_dS)z Close portFN)r(r=)rr#r#r$closesz Serial.closecCs:tj}tj}tj|jtj|tj|s4td|jS)z9Return the number of bytes currently in the input buffer.zcall to ClearCommError failed) rDWORDCOMSTATClearCommErrorrr0r6rcbInQue)rflagscomstatr#r#r$ in_waitings zSerial.in_waitingr'c CsT|js t|dkrFtj|jjtj}tj}tj|j t j |t j |s`t dj t j|jdkrvt|j|n|}|dkr>t j|}tj}tj|j ||t j |t j |j}| rtjtjtjfkrt dj t jtj|j t j |jt j |d}|s,tjtjkr,t dj t j|jd|j} nt} nt} 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. rzClearCommError failed ({!r})zReadFile failed ({!r})Tz!GetOverlappedResult failed ({!r})N)r(rrZ ResetEventrr2rKrLrMrr0r6rr/r1ZtimeoutminrNZcreate_string_bufferZReadFile GetLastErrorZ ERROR_SUCCESSERROR_IO_PENDINGGetOverlappedResultERROR_OPERATION_ABORTEDrawvaluebytes) rsizerOrPnZbufrcZread_okZ result_okreadr#r#r$r]s@    z Serial.readcCs|js tt|}|rtj}tj|j|t|tj ||j }| rdtj tj krdt djtj|jdkrtj|j|j tj |d}tj tjkr|jS|jt|krt|jSdSdS)z2Output the given byte string over the serial port.zWriteFile failed ({!r})rTN)r(rrrrKZ WriteFilerlenr0r6rrSrTrr/r1rCrUrVrXr)rdatar[errr#r#r$write-s   z Serial.writecCsx|jrtjdqWdS)zb Flush of file like objects. In this case, wait until all data is written. g?N) out_waitingtimeZsleep)rr#r#r$flushGsz Serial.flushcCs$|js ttj|jtjtjBdS)z9Clear input buffer, discarding all that is in the buffer.N)r(rrr8rr;r<)rr#r#r$reset_input_bufferRszSerial.reset_input_buffercCs$|js ttj|jtjtjBdS)zs Clear output buffer, aborting the current output and discarding all that is in the buffer. N)r(rrr8rr9r:)rr#r#r$reset_output_bufferXszSerial.reset_output_buffercCs.|js t|jrtj|jn tj|jdS)zBSet break: Controls TXD. When active, to transmitting is possible.N)r(rZ _break_staterZ SetCommBreakrZClearCommBreak)rr#r#r$_update_break_stateas zSerial._update_break_statecCs,|jrtj|jtjntj|jtjdS)z)Set terminal status line: Request To SendN)rFrEscapeCommFunctionrZSETRTSZCLRRTS)rr#r#r$_update_rts_statejszSerial._update_rts_statecCs,|jrtj|jtjntj|jtjdS)z-Set terminal status line: Data Terminal ReadyN)rGrrhrZSETDTRZCLRDTR)rr#r#r$_update_dtr_stateqszSerial._update_dtr_statecCs,|js ttj}tj|jtj||jS)N) r(rrrKZGetCommModemStatusrr0r6rX)rstatr#r#r$_GetCommModemStatusxs zSerial._GetCommModemStatuscCstj|j@dkS)z(Read terminal status line: Clear To Sendr)rZ MS_CTS_ONrl)rr#r#r$ctssz Serial.ctscCstj|j@dkS)z)Read terminal status line: Data Set Readyr)rZ MS_DSR_ONrl)rr#r#r$dsrsz Serial.dsrcCstj|j@dkS)z)Read terminal status line: Ring Indicatorr)rZ MS_RING_ONrl)rr#r#r$risz Serial.ricCstj|j@dkS)z)Read terminal status line: Carrier Detectr)rZ MS_RLSD_ONrl)rr#r#r$cdsz Serial.cdNcCs |dkr |}tj|j||dS)z Recommend a buffer size to the driver (device driver can ignore this value). Must be called before the port is opended. N)rr3r)rZrx_sizeZtx_sizer#r#r$set_buffer_sizeszSerial.set_buffer_sizeTcCs4|js t|r tj|jtjntj|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(rrrhrZSETXONZSETXOFF)renabler#r#r$set_output_flow_controls zSerial.set_output_flow_controlcCs:tj}tj}tj|jtj|tj|s4td|jS)z0Return how many bytes the in the outgoing bufferzcall to ClearCommError failed) rrKrLrMrr0r6rZcbOutQue)rrOrPr#r#r$rbs zSerial.out_waitingcCsRtj}tj|jtj|tj|d}| rNtjtjtjfkrNtj |j|dS)zACancel a blocking read operation, may be called from other threadFN) rrKrUrr0r6rSrTZERROR_IO_INCOMPLETEZ CancelIoEx)rZ overlappedr\r`r#r#r$_cancel_overlapped_ioszSerial._cancel_overlapped_iocCs|j|jdS)zACancel a blocking read operation, may be called from other threadN)rur)rr#r#r$rHszSerial.cancel_readcCs|j|jdS)zBCancel a blocking write operation, may be called from other threadN)rur)rr#r#r$rIszSerial.cancel_write)r r r r r rrrrrrrrrrrr)r')rqN)T) __name__ __module__ __qualname____doc__Z BAUDRATESrr>r7r=rJpropertyrQr]rardrerfrgrirjrlrmrnrorprrrtrbrurHrI __classcell__r#r#)r"r$rs: A )          r) r0rcrErZserial.serialutilrrrrrrr#r#r#r$ s