cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 1 | #! python |
Chris Liechti | 3e02f70 | 2015-12-16 23:06:04 +0100 | [diff] [blame] | 2 | # |
| 3 | # backend for Windows ("win32" incl. 32/64 bit support) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 4 | # |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 5 | # (C) 2001-2015 Chris Liechti <cliechti@gmx.net> |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 6 | # |
Chris Liechti | 3e02f70 | 2015-12-16 23:06:04 +0100 | [diff] [blame] | 7 | # This file is part of pySerial. https://github.com/pyserial/pyserial |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 8 | # SPDX-License-Identifier: BSD-3-Clause |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 9 | # |
| 10 | # Initial patch to use ctypes by Giovanni Bajo <rasky@develer.com> |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 11 | |
Kurt McKee | 057387c | 2018-02-07 22:10:38 -0600 | [diff] [blame] | 12 | from __future__ import absolute_import |
| 13 | |
Chris Liechti | 409e10b | 2016-02-10 22:40:34 +0100 | [diff] [blame] | 14 | # pylint: disable=invalid-name,too-few-public-methods |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 15 | import ctypes |
Chris Liechti | d240cf5 | 2015-08-05 02:57:03 +0200 | [diff] [blame] | 16 | import time |
cliechti | 39cfb7b | 2011-08-22 00:30:07 +0000 | [diff] [blame] | 17 | from serial import win32 |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 18 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 19 | import serial |
Chris Liechti | e99bda3 | 2020-09-14 03:59:52 +0200 | [diff] [blame^] | 20 | from serial.serialutil import SerialBase, SerialException, to_bytes, PortNotOpenError, SerialTimeoutException |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 21 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 22 | |
Chris Liechti | ef6b7b4 | 2015-08-06 22:19:26 +0200 | [diff] [blame] | 23 | class Serial(SerialBase): |
cliechti | 1e2a6df | 2009-07-24 02:00:50 +0000 | [diff] [blame] | 24 | """Serial port implementation for Win32 based on ctypes.""" |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 25 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 26 | BAUDRATES = (50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, |
| 27 | 9600, 19200, 38400, 57600, 115200) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 28 | |
cliechti | 5735a07 | 2009-10-26 23:57:48 +0000 | [diff] [blame] | 29 | def __init__(self, *args, **kwargs): |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 30 | self._port_handle = None |
| 31 | self._overlapped_read = None |
| 32 | self._overlapped_write = None |
Chris Liechti | e509cf2 | 2016-01-22 02:20:57 +0100 | [diff] [blame] | 33 | super(Serial, self).__init__(*args, **kwargs) |
cliechti | 5735a07 | 2009-10-26 23:57:48 +0000 | [diff] [blame] | 34 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 35 | def open(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 36 | """\ |
| 37 | Open port with current settings. This may throw a SerialException |
| 38 | if the port cannot be opened. |
| 39 | """ |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 40 | if self._port is None: |
| 41 | raise SerialException("Port must be configured before it can be used.") |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 42 | if self.is_open: |
cliechti | 8f69e70 | 2011-03-19 00:22:32 +0000 | [diff] [blame] | 43 | raise SerialException("Port is already open.") |
cliechti | 8b7cff0 | 2008-06-24 12:11:57 +0000 | [diff] [blame] | 44 | # the "\\.\COMx" format is required for devices other than COM1-COM8 |
| 45 | # not all versions of windows seem to support this properly |
| 46 | # so that the first few ports are used with the DOS device name |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 47 | port = self.name |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 48 | try: |
| 49 | if port.upper().startswith('COM') and int(port[3:]) > 8: |
| 50 | port = '\\\\.\\' + port |
| 51 | except ValueError: |
| 52 | # for like COMnotanumber |
| 53 | pass |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 54 | self._port_handle = win32.CreateFile( |
Chris Liechti | 9eaa40c | 2016-02-12 23:32:59 +0100 | [diff] [blame] | 55 | port, |
| 56 | win32.GENERIC_READ | win32.GENERIC_WRITE, |
| 57 | 0, # exclusive access |
| 58 | None, # no security |
| 59 | win32.OPEN_EXISTING, |
| 60 | win32.FILE_ATTRIBUTE_NORMAL | win32.FILE_FLAG_OVERLAPPED, |
| 61 | 0) |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 62 | if self._port_handle == win32.INVALID_HANDLE_VALUE: |
| 63 | self._port_handle = None # 'cause __del__ is called anyway |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 64 | raise SerialException("could not open port {!r}: {!r}".format(self.portstr, ctypes.WinError())) |
cliechti | e37b6a8 | 2009-07-24 12:19:50 +0000 | [diff] [blame] | 65 | |
cliechti | 1ae5ab0 | 2013-10-11 01:08:02 +0000 | [diff] [blame] | 66 | try: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 67 | self._overlapped_read = win32.OVERLAPPED() |
| 68 | self._overlapped_read.hEvent = win32.CreateEvent(None, 1, 0, None) |
| 69 | self._overlapped_write = win32.OVERLAPPED() |
| 70 | #~ self._overlapped_write.hEvent = win32.CreateEvent(None, 1, 0, None) |
| 71 | self._overlapped_write.hEvent = win32.CreateEvent(None, 0, 0, None) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 72 | |
cliechti | 1ae5ab0 | 2013-10-11 01:08:02 +0000 | [diff] [blame] | 73 | # Setup a 4k buffer |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 74 | win32.SetupComm(self._port_handle, 4096, 4096) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 75 | |
cliechti | 1ae5ab0 | 2013-10-11 01:08:02 +0000 | [diff] [blame] | 76 | # Save original timeout values: |
| 77 | self._orgTimeouts = win32.COMMTIMEOUTS() |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 78 | win32.GetCommTimeouts(self._port_handle, ctypes.byref(self._orgTimeouts)) |
cliechti | edfba4e | 2009-02-07 00:29:47 +0000 | [diff] [blame] | 79 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 80 | self._reconfigure_port() |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 81 | |
cliechti | 1ae5ab0 | 2013-10-11 01:08:02 +0000 | [diff] [blame] | 82 | # Clear buffers: |
| 83 | # Remove anything that was there |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 84 | win32.PurgeComm( |
Chris Liechti | 9eaa40c | 2016-02-12 23:32:59 +0100 | [diff] [blame] | 85 | self._port_handle, |
| 86 | win32.PURGE_TXCLEAR | win32.PURGE_TXABORT | |
| 87 | win32.PURGE_RXCLEAR | win32.PURGE_RXABORT) |
cliechti | 1ae5ab0 | 2013-10-11 01:08:02 +0000 | [diff] [blame] | 88 | except: |
| 89 | try: |
| 90 | self._close() |
| 91 | except: |
| 92 | # ignore any exception when closing the port |
| 93 | # also to keep original exception that happened when setting up |
| 94 | pass |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 95 | self._port_handle = None |
cliechti | 1ae5ab0 | 2013-10-11 01:08:02 +0000 | [diff] [blame] | 96 | raise |
| 97 | else: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 98 | self.is_open = True |
cliechti | 1ae5ab0 | 2013-10-11 01:08:02 +0000 | [diff] [blame] | 99 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 100 | def _reconfigure_port(self): |
cliechti | cc8d9d2 | 2008-07-06 22:42:44 +0000 | [diff] [blame] | 101 | """Set communication parameters on opened port.""" |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 102 | if not self._port_handle: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 103 | raise SerialException("Can only operate on a valid port handle") |
cliechti | edfba4e | 2009-02-07 00:29:47 +0000 | [diff] [blame] | 104 | |
| 105 | # Set Windows timeout values |
| 106 | # timeouts is a tuple with the following items: |
| 107 | # (ReadIntervalTimeout,ReadTotalTimeoutMultiplier, |
| 108 | # ReadTotalTimeoutConstant,WriteTotalTimeoutMultiplier, |
| 109 | # WriteTotalTimeoutConstant) |
Chris Liechti | cc12801 | 2015-11-06 02:34:16 +0100 | [diff] [blame] | 110 | timeouts = win32.COMMTIMEOUTS() |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 111 | if self._timeout is None: |
Chris Liechti | e192724 | 2015-11-03 00:38:43 +0100 | [diff] [blame] | 112 | pass # default of all zeros is OK |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 113 | elif self._timeout == 0: |
Chris Liechti | e192724 | 2015-11-03 00:38:43 +0100 | [diff] [blame] | 114 | timeouts.ReadIntervalTimeout = win32.MAXDWORD |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 115 | else: |
Chris Liechti | e192724 | 2015-11-03 00:38:43 +0100 | [diff] [blame] | 116 | timeouts.ReadTotalTimeoutConstant = max(int(self._timeout * 1000), 1) |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 117 | if self._timeout != 0 and self._inter_byte_timeout is not None: |
Chris Liechti | e192724 | 2015-11-03 00:38:43 +0100 | [diff] [blame] | 118 | timeouts.ReadIntervalTimeout = max(int(self._inter_byte_timeout * 1000), 1) |
cliechti | edfba4e | 2009-02-07 00:29:47 +0000 | [diff] [blame] | 119 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 120 | if self._write_timeout is None: |
cliechti | 6261161 | 2004-04-20 01:55:43 +0000 | [diff] [blame] | 121 | pass |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 122 | elif self._write_timeout == 0: |
Chris Liechti | e192724 | 2015-11-03 00:38:43 +0100 | [diff] [blame] | 123 | timeouts.WriteTotalTimeoutConstant = win32.MAXDWORD |
cliechti | 6261161 | 2004-04-20 01:55:43 +0000 | [diff] [blame] | 124 | else: |
Chris Liechti | e192724 | 2015-11-03 00:38:43 +0100 | [diff] [blame] | 125 | timeouts.WriteTotalTimeoutConstant = max(int(self._write_timeout * 1000), 1) |
| 126 | win32.SetCommTimeouts(self._port_handle, ctypes.byref(timeouts)) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 127 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 128 | win32.SetCommMask(self._port_handle, win32.EV_ERR) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 129 | |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 130 | # Setup the connection info. |
| 131 | # Get state and modify it: |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 132 | comDCB = win32.DCB() |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 133 | win32.GetCommState(self._port_handle, ctypes.byref(comDCB)) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 134 | comDCB.BaudRate = self._baudrate |
| 135 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 136 | if self._bytesize == serial.FIVEBITS: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 137 | comDCB.ByteSize = 5 |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 138 | elif self._bytesize == serial.SIXBITS: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 139 | comDCB.ByteSize = 6 |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 140 | elif self._bytesize == serial.SEVENBITS: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 141 | comDCB.ByteSize = 7 |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 142 | elif self._bytesize == serial.EIGHTBITS: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 143 | comDCB.ByteSize = 8 |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 144 | else: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 145 | raise ValueError("Unsupported number of data bits: {!r}".format(self._bytesize)) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 146 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 147 | if self._parity == serial.PARITY_NONE: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 148 | comDCB.Parity = win32.NOPARITY |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 149 | comDCB.fParity = 0 # Disable Parity Check |
| 150 | elif self._parity == serial.PARITY_EVEN: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 151 | comDCB.Parity = win32.EVENPARITY |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 152 | comDCB.fParity = 1 # Enable Parity Check |
| 153 | elif self._parity == serial.PARITY_ODD: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 154 | comDCB.Parity = win32.ODDPARITY |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 155 | comDCB.fParity = 1 # Enable Parity Check |
| 156 | elif self._parity == serial.PARITY_MARK: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 157 | comDCB.Parity = win32.MARKPARITY |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 158 | comDCB.fParity = 1 # Enable Parity Check |
| 159 | elif self._parity == serial.PARITY_SPACE: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 160 | comDCB.Parity = win32.SPACEPARITY |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 161 | comDCB.fParity = 1 # Enable Parity Check |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 162 | else: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 163 | raise ValueError("Unsupported parity mode: {!r}".format(self._parity)) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 164 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 165 | if self._stopbits == serial.STOPBITS_ONE: |
| 166 | comDCB.StopBits = win32.ONESTOPBIT |
| 167 | elif self._stopbits == serial.STOPBITS_ONE_POINT_FIVE: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 168 | comDCB.StopBits = win32.ONE5STOPBITS |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 169 | elif self._stopbits == serial.STOPBITS_TWO: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 170 | comDCB.StopBits = win32.TWOSTOPBITS |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 171 | else: |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 172 | raise ValueError("Unsupported number of stop bits: {!r}".format(self._stopbits)) |
cliechti | edfba4e | 2009-02-07 00:29:47 +0000 | [diff] [blame] | 173 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 174 | comDCB.fBinary = 1 # Enable Binary Transmission |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 175 | # Char. w/ Parity-Err are replaced with 0xff (if fErrorChar is set to TRUE) |
Chris Liechti | 33f0ec5 | 2015-08-06 16:37:21 +0200 | [diff] [blame] | 176 | if self._rs485_mode is None: |
| 177 | if self._rtscts: |
| 178 | comDCB.fRtsControl = win32.RTS_CONTROL_HANDSHAKE |
| 179 | else: |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 180 | comDCB.fRtsControl = win32.RTS_CONTROL_ENABLE if self._rts_state else win32.RTS_CONTROL_DISABLE |
Chris Liechti | 33f0ec5 | 2015-08-06 16:37:21 +0200 | [diff] [blame] | 181 | comDCB.fOutxCtsFlow = self._rtscts |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 182 | else: |
Chris Liechti | 33f0ec5 | 2015-08-06 16:37:21 +0200 | [diff] [blame] | 183 | # checks for unsupported settings |
| 184 | # XXX verify if platform really does not have a setting for those |
| 185 | if not self._rs485_mode.rts_level_for_tx: |
| 186 | raise ValueError( |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 187 | 'Unsupported value for RS485Settings.rts_level_for_tx: {!r}'.format( |
| 188 | self._rs485_mode.rts_level_for_tx,)) |
Chris Liechti | 33f0ec5 | 2015-08-06 16:37:21 +0200 | [diff] [blame] | 189 | if self._rs485_mode.rts_level_for_rx: |
| 190 | raise ValueError( |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 191 | 'Unsupported value for RS485Settings.rts_level_for_rx: {!r}'.format( |
| 192 | self._rs485_mode.rts_level_for_rx,)) |
Chris Liechti | 33f0ec5 | 2015-08-06 16:37:21 +0200 | [diff] [blame] | 193 | if self._rs485_mode.delay_before_tx is not None: |
| 194 | raise ValueError( |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 195 | 'Unsupported value for RS485Settings.delay_before_tx: {!r}'.format( |
| 196 | self._rs485_mode.delay_before_tx,)) |
Chris Liechti | 33f0ec5 | 2015-08-06 16:37:21 +0200 | [diff] [blame] | 197 | if self._rs485_mode.delay_before_rx is not None: |
| 198 | raise ValueError( |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 199 | 'Unsupported value for RS485Settings.delay_before_rx: {!r}'.format( |
| 200 | self._rs485_mode.delay_before_rx,)) |
Chris Liechti | 33f0ec5 | 2015-08-06 16:37:21 +0200 | [diff] [blame] | 201 | if self._rs485_mode.loopback: |
| 202 | raise ValueError( |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 203 | 'Unsupported value for RS485Settings.loopback: {!r}'.format( |
| 204 | self._rs485_mode.loopback,)) |
Chris Liechti | 33f0ec5 | 2015-08-06 16:37:21 +0200 | [diff] [blame] | 205 | comDCB.fRtsControl = win32.RTS_CONTROL_TOGGLE |
| 206 | comDCB.fOutxCtsFlow = 0 |
| 207 | |
cliechti | f46e0a8 | 2005-05-19 15:24:57 +0000 | [diff] [blame] | 208 | if self._dsrdtr: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 209 | comDCB.fDtrControl = win32.DTR_CONTROL_HANDSHAKE |
cliechti | f46e0a8 | 2005-05-19 15:24:57 +0000 | [diff] [blame] | 210 | else: |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 211 | comDCB.fDtrControl = win32.DTR_CONTROL_ENABLE if self._dtr_state else win32.DTR_CONTROL_DISABLE |
| 212 | comDCB.fOutxDsrFlow = self._dsrdtr |
| 213 | comDCB.fOutX = self._xonxoff |
| 214 | comDCB.fInX = self._xonxoff |
| 215 | comDCB.fNull = 0 |
| 216 | comDCB.fErrorChar = 0 |
| 217 | comDCB.fAbortOnError = 0 |
| 218 | comDCB.XonChar = serial.XON |
| 219 | comDCB.XoffChar = serial.XOFF |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 220 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 221 | if not win32.SetCommState(self._port_handle, ctypes.byref(comDCB)): |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 222 | raise SerialException( |
| 223 | 'Cannot configure port, something went wrong. ' |
| 224 | 'Original message: {!r}'.format(ctypes.WinError())) |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 225 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 226 | #~ def __del__(self): |
| 227 | #~ self.close() |
| 228 | |
cliechti | 1ae5ab0 | 2013-10-11 01:08:02 +0000 | [diff] [blame] | 229 | def _close(self): |
| 230 | """internal close port helper""" |
Chris Liechti | 3401840 | 2016-05-25 01:51:42 +0200 | [diff] [blame] | 231 | if self._port_handle is not None: |
cliechti | 1ae5ab0 | 2013-10-11 01:08:02 +0000 | [diff] [blame] | 232 | # Restore original timeout values: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 233 | win32.SetCommTimeouts(self._port_handle, self._orgTimeouts) |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 234 | if self._overlapped_read is not None: |
Chris Liechti | 5a39b88 | 2016-05-03 22:10:39 +0200 | [diff] [blame] | 235 | self.cancel_read() |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 236 | win32.CloseHandle(self._overlapped_read.hEvent) |
| 237 | self._overlapped_read = None |
| 238 | if self._overlapped_write is not None: |
Chris Liechti | 5a39b88 | 2016-05-03 22:10:39 +0200 | [diff] [blame] | 239 | self.cancel_write() |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 240 | win32.CloseHandle(self._overlapped_write.hEvent) |
| 241 | self._overlapped_write = None |
Chris Liechti | 3401840 | 2016-05-25 01:51:42 +0200 | [diff] [blame] | 242 | win32.CloseHandle(self._port_handle) |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 243 | self._port_handle = None |
cliechti | 1ae5ab0 | 2013-10-11 01:08:02 +0000 | [diff] [blame] | 244 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 245 | def close(self): |
| 246 | """Close port""" |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 247 | if self.is_open: |
cliechti | 1ae5ab0 | 2013-10-11 01:08:02 +0000 | [diff] [blame] | 248 | self._close() |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 249 | self.is_open = False |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 250 | |
| 251 | # - - - - - - - - - - - - - - - - - - - - - - - - |
cliechti | edfba4e | 2009-02-07 00:29:47 +0000 | [diff] [blame] | 252 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 253 | @property |
| 254 | def in_waiting(self): |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 255 | """Return the number of bytes currently in the input buffer.""" |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 256 | flags = win32.DWORD() |
| 257 | comstat = win32.COMSTAT() |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 258 | if not win32.ClearCommError(self._port_handle, ctypes.byref(flags), ctypes.byref(comstat)): |
Chris Liechti | f956057 | 2017-02-15 02:47:54 +0100 | [diff] [blame] | 259 | raise SerialException("ClearCommError failed ({!r})".format(ctypes.WinError())) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 260 | return comstat.cbInQue |
| 261 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 262 | def read(self, size=1): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 263 | """\ |
| 264 | Read size bytes from the serial port. If a timeout is set it may |
| 265 | return less characters as requested. With no timeout it will block |
Chris Liechti | 5a39b88 | 2016-05-03 22:10:39 +0200 | [diff] [blame] | 266 | until the requested number of bytes is read. |
| 267 | """ |
Chris Liechti | acac236 | 2016-03-29 22:37:48 +0200 | [diff] [blame] | 268 | if not self.is_open: |
Chris Liechti | e99bda3 | 2020-09-14 03:59:52 +0200 | [diff] [blame^] | 269 | raise PortNotOpenError() |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 270 | if size > 0: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 271 | win32.ResetEvent(self._overlapped_read.hEvent) |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 272 | flags = win32.DWORD() |
| 273 | comstat = win32.COMSTAT() |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 274 | if not win32.ClearCommError(self._port_handle, ctypes.byref(flags), ctypes.byref(comstat)): |
Chris Liechti | 229604e | 2016-06-01 01:58:05 +0200 | [diff] [blame] | 275 | raise SerialException("ClearCommError failed ({!r})".format(ctypes.WinError())) |
denglj | 25f1deb | 2016-01-25 12:34:44 +0800 | [diff] [blame] | 276 | n = min(comstat.cbInQue, size) if self.timeout == 0 else size |
| 277 | if n > 0: |
| 278 | buf = ctypes.create_string_buffer(n) |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 279 | rc = win32.DWORD() |
Chris Liechti | 70ca49c | 2016-02-07 23:54:03 +0100 | [diff] [blame] | 280 | read_ok = win32.ReadFile( |
Chris Liechti | 9eaa40c | 2016-02-12 23:32:59 +0100 | [diff] [blame] | 281 | self._port_handle, |
| 282 | buf, |
| 283 | n, |
| 284 | ctypes.byref(rc), |
| 285 | ctypes.byref(self._overlapped_read)) |
Chris Liechti | 7731e98 | 2015-11-02 23:46:07 +0100 | [diff] [blame] | 286 | if not read_ok and win32.GetLastError() not in (win32.ERROR_SUCCESS, win32.ERROR_IO_PENDING): |
Chris Liechti | 1f6643d | 2016-02-16 21:06:52 +0100 | [diff] [blame] | 287 | raise SerialException("ReadFile failed ({!r})".format(ctypes.WinError())) |
Chris Liechti | 91f63fd | 2016-05-31 23:43:19 +0200 | [diff] [blame] | 288 | result_ok = win32.GetOverlappedResult( |
Chris Liechti | 9eaa40c | 2016-02-12 23:32:59 +0100 | [diff] [blame] | 289 | self._port_handle, |
| 290 | ctypes.byref(self._overlapped_read), |
| 291 | ctypes.byref(rc), |
| 292 | True) |
Chris Liechti | 91f63fd | 2016-05-31 23:43:19 +0200 | [diff] [blame] | 293 | if not result_ok: |
| 294 | if win32.GetLastError() != win32.ERROR_OPERATION_ABORTED: |
Chris Liechti | 229604e | 2016-06-01 01:58:05 +0200 | [diff] [blame] | 295 | raise SerialException("GetOverlappedResult failed ({!r})".format(ctypes.WinError())) |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 296 | read = buf.raw[:rc.value] |
denglj | 25f1deb | 2016-01-25 12:34:44 +0800 | [diff] [blame] | 297 | else: |
| 298 | read = bytes() |
cliechti | f622faf | 2003-07-12 23:41:43 +0000 | [diff] [blame] | 299 | else: |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 300 | read = bytes() |
| 301 | return bytes(read) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 302 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 303 | def write(self, data): |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 304 | """Output the given byte string over the serial port.""" |
Chris Liechti | acac236 | 2016-03-29 22:37:48 +0200 | [diff] [blame] | 305 | if not self.is_open: |
Chris Liechti | e99bda3 | 2020-09-14 03:59:52 +0200 | [diff] [blame^] | 306 | raise PortNotOpenError() |
cliechti | 23dc2a0 | 2009-07-30 17:25:09 +0000 | [diff] [blame] | 307 | #~ if not isinstance(data, (bytes, bytearray)): |
| 308 | #~ raise TypeError('expected %s or bytearray, got %s' % (bytes, type(data))) |
| 309 | # convert data (needed in case of memoryview instance: Py 3.1 io lib), ctypes doesn't like memoryview |
cliechti | 3807712 | 2013-10-16 02:57:27 +0000 | [diff] [blame] | 310 | data = to_bytes(data) |
cliechti | b2f5fc8 | 2006-10-20 00:09:07 +0000 | [diff] [blame] | 311 | if data: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 312 | #~ win32event.ResetEvent(self._overlapped_write.hEvent) |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 313 | n = win32.DWORD() |
Chris Liechti | 2a7ed53 | 2016-09-17 16:42:27 +0200 | [diff] [blame] | 314 | success = win32.WriteFile(self._port_handle, data, len(data), ctypes.byref(n), self._overlapped_write) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 315 | if self._write_timeout != 0: # if blocking (None) or w/ write timeout (>0) |
Chris Liechti | dc56ac9 | 2017-01-10 22:22:44 +0100 | [diff] [blame] | 316 | if not success and win32.GetLastError() not in (win32.ERROR_SUCCESS, win32.ERROR_IO_PENDING): |
Chris Liechti | 2a7ed53 | 2016-09-17 16:42:27 +0200 | [diff] [blame] | 317 | raise SerialException("WriteFile failed ({!r})".format(ctypes.WinError())) |
| 318 | |
cliechti | 31000fc | 2011-08-05 01:47:26 +0000 | [diff] [blame] | 319 | # Wait for the write to complete. |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 320 | #~ win32.WaitForSingleObject(self._overlapped_write.hEvent, win32.INFINITE) |
Chris Liechti | 2a7ed53 | 2016-09-17 16:42:27 +0200 | [diff] [blame] | 321 | win32.GetOverlappedResult(self._port_handle, self._overlapped_write, ctypes.byref(n), True) |
Chris Liechti | 3401840 | 2016-05-25 01:51:42 +0200 | [diff] [blame] | 322 | if win32.GetLastError() == win32.ERROR_OPERATION_ABORTED: |
Chris Liechti | 77d922b | 2016-05-26 17:14:58 +0200 | [diff] [blame] | 323 | return n.value # canceled IO is no error |
cliechti | 31000fc | 2011-08-05 01:47:26 +0000 | [diff] [blame] | 324 | if n.value != len(data): |
Chris Liechti | e99bda3 | 2020-09-14 03:59:52 +0200 | [diff] [blame^] | 325 | raise SerialTimeoutException('Write timeout') |
Chris Liechti | ab1ff48 | 2016-09-16 23:53:21 +0200 | [diff] [blame] | 326 | return n.value |
| 327 | else: |
Chris Liechti | 2a7ed53 | 2016-09-17 16:42:27 +0200 | [diff] [blame] | 328 | errorcode = win32.ERROR_SUCCESS if success else win32.GetLastError() |
| 329 | if errorcode in (win32.ERROR_INVALID_USER_BUFFER, win32.ERROR_NOT_ENOUGH_MEMORY, |
| 330 | win32.ERROR_OPERATION_ABORTED): |
| 331 | return 0 |
Chris Liechti | c759d29 | 2016-09-29 23:58:26 +0200 | [diff] [blame] | 332 | elif errorcode in (win32.ERROR_SUCCESS, win32.ERROR_IO_PENDING): |
Chris Liechti | 2a7ed53 | 2016-09-17 16:42:27 +0200 | [diff] [blame] | 333 | # no info on true length provided by OS function in async mode |
| 334 | return len(data) |
| 335 | else: |
| 336 | raise SerialException("WriteFile failed ({!r})".format(ctypes.WinError())) |
cliechti | 23dc2a0 | 2009-07-30 17:25:09 +0000 | [diff] [blame] | 337 | else: |
| 338 | return 0 |
cliechti | edfba4e | 2009-02-07 00:29:47 +0000 | [diff] [blame] | 339 | |
cliechti | 2640978 | 2013-05-31 00:47:16 +0000 | [diff] [blame] | 340 | def flush(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 341 | """\ |
| 342 | Flush of file like objects. In this case, wait until all data |
| 343 | is written. |
| 344 | """ |
Chris Liechti | bc119fd | 2015-09-09 17:08:25 +0200 | [diff] [blame] | 345 | while self.out_waiting: |
cliechti | 2640978 | 2013-05-31 00:47:16 +0000 | [diff] [blame] | 346 | time.sleep(0.05) |
| 347 | # XXX could also use WaitCommEvent with mask EV_TXEMPTY, but it would |
Chris Liechti | 2a7ed53 | 2016-09-17 16:42:27 +0200 | [diff] [blame] | 348 | # require overlapped IO and it's also only possible to set a single mask |
cliechti | 2640978 | 2013-05-31 00:47:16 +0000 | [diff] [blame] | 349 | # on the port--- |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 350 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 351 | def reset_input_buffer(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 352 | """Clear input buffer, discarding all that is in the buffer.""" |
Chris Liechti | acac236 | 2016-03-29 22:37:48 +0200 | [diff] [blame] | 353 | if not self.is_open: |
Chris Liechti | e99bda3 | 2020-09-14 03:59:52 +0200 | [diff] [blame^] | 354 | raise PortNotOpenError() |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 355 | win32.PurgeComm(self._port_handle, win32.PURGE_RXCLEAR | win32.PURGE_RXABORT) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 356 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 357 | def reset_output_buffer(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 358 | """\ |
| 359 | Clear output buffer, aborting the current output and discarding all |
| 360 | that is in the buffer. |
| 361 | """ |
Chris Liechti | acac236 | 2016-03-29 22:37:48 +0200 | [diff] [blame] | 362 | if not self.is_open: |
Chris Liechti | e99bda3 | 2020-09-14 03:59:52 +0200 | [diff] [blame^] | 363 | raise PortNotOpenError() |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 364 | win32.PurgeComm(self._port_handle, win32.PURGE_TXCLEAR | win32.PURGE_TXABORT) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 365 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 366 | def _update_break_state(self): |
cliechti | 997b63c | 2008-06-21 00:09:31 +0000 | [diff] [blame] | 367 | """Set break: Controls TXD. When active, to transmitting is possible.""" |
Chris Liechti | acac236 | 2016-03-29 22:37:48 +0200 | [diff] [blame] | 368 | if not self.is_open: |
Chris Liechti | e99bda3 | 2020-09-14 03:59:52 +0200 | [diff] [blame^] | 369 | raise PortNotOpenError() |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 370 | if self._break_state: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 371 | win32.SetCommBreak(self._port_handle) |
cliechti | 997b63c | 2008-06-21 00:09:31 +0000 | [diff] [blame] | 372 | else: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 373 | win32.ClearCommBreak(self._port_handle) |
cliechti | 997b63c | 2008-06-21 00:09:31 +0000 | [diff] [blame] | 374 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 375 | def _update_rts_state(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 376 | """Set terminal status line: Request To Send""" |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 377 | if self._rts_state: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 378 | win32.EscapeCommFunction(self._port_handle, win32.SETRTS) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 379 | else: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 380 | win32.EscapeCommFunction(self._port_handle, win32.CLRRTS) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 381 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 382 | def _update_dtr_state(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 383 | """Set terminal status line: Data Terminal Ready""" |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 384 | if self._dtr_state: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 385 | win32.EscapeCommFunction(self._port_handle, win32.SETDTR) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 386 | else: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 387 | win32.EscapeCommFunction(self._port_handle, win32.CLRDTR) |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 388 | |
| 389 | def _GetCommModemStatus(self): |
Chris Liechti | acac236 | 2016-03-29 22:37:48 +0200 | [diff] [blame] | 390 | if not self.is_open: |
Chris Liechti | e99bda3 | 2020-09-14 03:59:52 +0200 | [diff] [blame^] | 391 | raise PortNotOpenError() |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 392 | stat = win32.DWORD() |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 393 | win32.GetCommModemStatus(self._port_handle, ctypes.byref(stat)) |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 394 | return stat.value |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 395 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 396 | @property |
| 397 | def cts(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 398 | """Read terminal status line: Clear To Send""" |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 399 | return win32.MS_CTS_ON & self._GetCommModemStatus() != 0 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 400 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 401 | @property |
| 402 | def dsr(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 403 | """Read terminal status line: Data Set Ready""" |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 404 | return win32.MS_DSR_ON & self._GetCommModemStatus() != 0 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 405 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 406 | @property |
| 407 | def ri(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 408 | """Read terminal status line: Ring Indicator""" |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 409 | return win32.MS_RING_ON & self._GetCommModemStatus() != 0 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 410 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 411 | @property |
| 412 | def cd(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 413 | """Read terminal status line: Carrier Detect""" |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 414 | return win32.MS_RLSD_ON & self._GetCommModemStatus() != 0 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 415 | |
cliechti | a30a8a0 | 2003-10-05 12:28:13 +0000 | [diff] [blame] | 416 | # - - platform specific - - - - |
| 417 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 418 | def set_buffer_size(self, rx_size=4096, tx_size=None): |
cliechti | 5919964 | 2011-12-28 20:54:30 +0000 | [diff] [blame] | 419 | """\ |
| 420 | Recommend a buffer size to the driver (device driver can ignore this |
Mohammad Ghasemiahmadi | 98b8c1a | 2018-05-15 10:21:35 -0700 | [diff] [blame] | 421 | value). Must be called after the port is opened. |
cliechti | 5919964 | 2011-12-28 20:54:30 +0000 | [diff] [blame] | 422 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 423 | if tx_size is None: |
| 424 | tx_size = rx_size |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 425 | win32.SetupComm(self._port_handle, rx_size, tx_size) |
cliechti | 5919964 | 2011-12-28 20:54:30 +0000 | [diff] [blame] | 426 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 427 | def set_output_flow_control(self, enable=True): |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 428 | """\ |
| 429 | Manually control flow - when software flow control is enabled. |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 430 | This will do the same as if XON (true) or XOFF (false) are received |
| 431 | from the other device and control the transmission accordingly. |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 432 | WARNING: this function is not portable to different platforms! |
| 433 | """ |
Chris Liechti | acac236 | 2016-03-29 22:37:48 +0200 | [diff] [blame] | 434 | if not self.is_open: |
Chris Liechti | e99bda3 | 2020-09-14 03:59:52 +0200 | [diff] [blame^] | 435 | raise PortNotOpenError() |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 436 | if enable: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 437 | win32.EscapeCommFunction(self._port_handle, win32.SETXON) |
cliechti | a30a8a0 | 2003-10-05 12:28:13 +0000 | [diff] [blame] | 438 | else: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 439 | win32.EscapeCommFunction(self._port_handle, win32.SETXOFF) |
cliechti | a30a8a0 | 2003-10-05 12:28:13 +0000 | [diff] [blame] | 440 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 441 | @property |
| 442 | def out_waiting(self): |
| 443 | """Return how many bytes the in the outgoing buffer""" |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 444 | flags = win32.DWORD() |
| 445 | comstat = win32.COMSTAT() |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 446 | if not win32.ClearCommError(self._port_handle, ctypes.byref(flags), ctypes.byref(comstat)): |
Chris Liechti | d579018 | 2017-03-05 23:53:40 +0100 | [diff] [blame] | 447 | raise SerialException("ClearCommError failed ({!r})".format(ctypes.WinError())) |
cliechti | c8e83d8 | 2009-07-21 21:34:05 +0000 | [diff] [blame] | 448 | return comstat.cbOutQue |
Chris Liechti | 5a39b88 | 2016-05-03 22:10:39 +0200 | [diff] [blame] | 449 | |
| 450 | def _cancel_overlapped_io(self, overlapped): |
| 451 | """Cancel a blocking read operation, may be called from other thread""" |
| 452 | # check if read operation is pending |
| 453 | rc = win32.DWORD() |
| 454 | err = win32.GetOverlappedResult( |
| 455 | self._port_handle, |
| 456 | ctypes.byref(overlapped), |
| 457 | ctypes.byref(rc), |
| 458 | False) |
Chris Liechti | c0d6a0f | 2016-05-12 23:48:01 +0200 | [diff] [blame] | 459 | if not err and win32.GetLastError() in (win32.ERROR_IO_PENDING, win32.ERROR_IO_INCOMPLETE): |
Chris Liechti | 5a39b88 | 2016-05-03 22:10:39 +0200 | [diff] [blame] | 460 | # cancel, ignoring any errors (e.g. it may just have finished on its own) |
| 461 | win32.CancelIoEx(self._port_handle, overlapped) |
| 462 | |
| 463 | def cancel_read(self): |
| 464 | """Cancel a blocking read operation, may be called from other thread""" |
| 465 | self._cancel_overlapped_io(self._overlapped_read) |
| 466 | |
| 467 | def cancel_write(self): |
| 468 | """Cancel a blocking write operation, may be called from other thread""" |
| 469 | self._cancel_overlapped_io(self._overlapped_write) |
Chris Liechti | 700a238 | 2017-03-04 23:58:28 +0100 | [diff] [blame] | 470 | |
| 471 | @SerialBase.exclusive.setter |
| 472 | def exclusive(self, exclusive): |
| 473 | """Change the exclusive access setting.""" |
| 474 | if exclusive is not None and not exclusive: |
| 475 | raise ValueError('win32 only supports exclusive access (not: {})'.format(exclusive)) |
Chris Liechti | d579018 | 2017-03-05 23:53:40 +0100 | [diff] [blame] | 476 | else: |
| 477 | serial.SerialBase.exclusive.__set__(self, exclusive) |