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