cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 1 | #! python |
cliechti | c54b2c8 | 2008-06-21 01:59:08 +0000 | [diff] [blame] | 2 | # Python Serial Port Extension for Win32, Linux, BSD, Jython |
| 3 | # serial driver for win32 |
| 4 | # see __init__.py |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 5 | # |
Chris Liechti | 68340d7 | 2015-08-03 14:15:48 +0200 | [diff] [blame] | 6 | # (C) 2001-2015 Chris Liechti <cliechti@gmx.net> |
Chris Liechti | fbdd8a0 | 2015-08-09 02:37:45 +0200 | [diff] [blame] | 7 | # |
| 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 | |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 12 | import ctypes |
Chris Liechti | d240cf5 | 2015-08-05 02:57:03 +0200 | [diff] [blame] | 13 | import time |
cliechti | 39cfb7b | 2011-08-22 00:30:07 +0000 | [diff] [blame] | 14 | from serial import win32 |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 15 | |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 16 | import serial |
| 17 | from serial.serialutil import SerialBase, SerialException, to_bytes, portNotOpenError, writeTimeoutError |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 18 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 19 | |
Chris Liechti | ef6b7b4 | 2015-08-06 22:19:26 +0200 | [diff] [blame] | 20 | class Serial(SerialBase): |
cliechti | 1e2a6df | 2009-07-24 02:00:50 +0000 | [diff] [blame] | 21 | """Serial port implementation for Win32 based on ctypes.""" |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 22 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 23 | BAUDRATES = (50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, |
| 24 | 9600, 19200, 38400, 57600, 115200) |
cliechti | 80a0ed1 | 2003-10-03 23:53:42 +0000 | [diff] [blame] | 25 | |
cliechti | 5735a07 | 2009-10-26 23:57:48 +0000 | [diff] [blame] | 26 | def __init__(self, *args, **kwargs): |
Chris Liechti | 76f8349 | 2015-08-10 01:22:01 +0200 | [diff] [blame] | 27 | super(SerialBase, self).__init__() |
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 |
cliechti | 5735a07 | 2009-10-26 23:57:48 +0000 | [diff] [blame] | 31 | SerialBase.__init__(self, *args, **kwargs) |
| 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( |
| 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 |
cliechti | 1083be7 | 2011-12-28 20:45:30 +0000 | [diff] [blame] | 62 | raise SerialException("could not open port %r: %r" % (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( |
| 83 | self._port_handle, |
cliechti | 1ae5ab0 | 2013-10-11 01:08:02 +0000 | [diff] [blame] | 84 | win32.PURGE_TXCLEAR | win32.PURGE_TXABORT | |
| 85 | win32.PURGE_RXCLEAR | win32.PURGE_RXABORT) |
| 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: |
| 143 | raise ValueError("Unsupported number of data bits: %r" % self._bytesize) |
| 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: |
| 161 | raise ValueError("Unsupported parity mode: %r" % self._parity) |
| 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: |
| 170 | raise ValueError("Unsupported number of stop bits: %r" % 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( |
| 185 | 'Unsupported value for RS485Settings.rts_level_for_tx: %r' % ( |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 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( |
| 189 | 'Unsupported value for RS485Settings.rts_level_for_rx: %r' % ( |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 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( |
| 193 | 'Unsupported value for RS485Settings.delay_before_tx: %r' % ( |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 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( |
| 197 | 'Unsupported value for RS485Settings.delay_before_rx: %r' % ( |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 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( |
| 201 | 'Unsupported value for RS485Settings.loopback: %r' % ( |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 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 | a629c5d | 2015-12-12 23:19:14 +0100 | [diff] [blame^] | 220 | raise SerialException("Cannot configure port, something went wrong. Original message: %r" % ctypes.WinError()) |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 221 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 222 | #~ def __del__(self): |
| 223 | #~ self.close() |
| 224 | |
cliechti | 1ae5ab0 | 2013-10-11 01:08:02 +0000 | [diff] [blame] | 225 | def _close(self): |
| 226 | """internal close port helper""" |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 227 | if self._port_handle: |
cliechti | 1ae5ab0 | 2013-10-11 01:08:02 +0000 | [diff] [blame] | 228 | # Restore original timeout values: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 229 | win32.SetCommTimeouts(self._port_handle, self._orgTimeouts) |
cliechti | 1ae5ab0 | 2013-10-11 01:08:02 +0000 | [diff] [blame] | 230 | # Close COM-Port: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 231 | win32.CloseHandle(self._port_handle) |
| 232 | if self._overlapped_read is not None: |
| 233 | win32.CloseHandle(self._overlapped_read.hEvent) |
| 234 | self._overlapped_read = None |
| 235 | if self._overlapped_write is not None: |
| 236 | win32.CloseHandle(self._overlapped_write.hEvent) |
| 237 | self._overlapped_write = None |
| 238 | self._port_handle = None |
cliechti | 1ae5ab0 | 2013-10-11 01:08:02 +0000 | [diff] [blame] | 239 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 240 | def close(self): |
| 241 | """Close port""" |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 242 | if self.is_open: |
cliechti | 1ae5ab0 | 2013-10-11 01:08:02 +0000 | [diff] [blame] | 243 | self._close() |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 244 | self.is_open = False |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 245 | |
| 246 | # - - - - - - - - - - - - - - - - - - - - - - - - |
cliechti | edfba4e | 2009-02-07 00:29:47 +0000 | [diff] [blame] | 247 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 248 | @property |
| 249 | def in_waiting(self): |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 250 | """Return the number of bytes currently in the input buffer.""" |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 251 | flags = win32.DWORD() |
| 252 | comstat = win32.COMSTAT() |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 253 | if not win32.ClearCommError(self._port_handle, ctypes.byref(flags), ctypes.byref(comstat)): |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 254 | raise SerialException('call to ClearCommError failed') |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 255 | return comstat.cbInQue |
| 256 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 257 | def read(self, size=1): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 258 | """\ |
| 259 | Read size bytes from the serial port. If a timeout is set it may |
| 260 | return less characters as requested. With no timeout it will block |
| 261 | until the requested number of bytes is read.""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 262 | if not self._port_handle: |
| 263 | raise portNotOpenError |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 264 | if size > 0: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 265 | win32.ResetEvent(self._overlapped_read.hEvent) |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 266 | flags = win32.DWORD() |
| 267 | comstat = win32.COMSTAT() |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 268 | if not win32.ClearCommError(self._port_handle, ctypes.byref(flags), ctypes.byref(comstat)): |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 269 | raise SerialException('call to ClearCommError failed') |
cliechti | 5c39e70 | 2002-06-04 14:56:34 +0000 | [diff] [blame] | 270 | if self.timeout == 0: |
cliechti | 17f177f | 2002-06-07 22:33:37 +0000 | [diff] [blame] | 271 | n = min(comstat.cbInQue, size) |
| 272 | if n > 0: |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 273 | buf = ctypes.create_string_buffer(n) |
| 274 | rc = win32.DWORD() |
Chris Liechti | 7731e98 | 2015-11-02 23:46:07 +0100 | [diff] [blame] | 275 | read_ok = win32.ReadFile(self._port_handle, buf, n, ctypes.byref(rc), ctypes.byref(self._overlapped_read)) |
| 276 | if not read_ok and win32.GetLastError() not in (win32.ERROR_SUCCESS, win32.ERROR_IO_PENDING): |
cliechti | 1083be7 | 2011-12-28 20:45:30 +0000 | [diff] [blame] | 277 | raise SerialException("ReadFile failed (%r)" % ctypes.WinError()) |
Chris Liechti | 7731e98 | 2015-11-02 23:46:07 +0100 | [diff] [blame] | 278 | win32.WaitForSingleObject(self._overlapped_read.hEvent, win32.INFINITE) |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 279 | read = buf.raw[:rc.value] |
cliechti | f622faf | 2003-07-12 23:41:43 +0000 | [diff] [blame] | 280 | else: |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 281 | read = bytes() |
cliechti | 5c39e70 | 2002-06-04 14:56:34 +0000 | [diff] [blame] | 282 | else: |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 283 | buf = ctypes.create_string_buffer(size) |
| 284 | rc = win32.DWORD() |
Chris Liechti | 7731e98 | 2015-11-02 23:46:07 +0100 | [diff] [blame] | 285 | read_ok = win32.ReadFile(self._port_handle, buf, size, ctypes.byref(rc), ctypes.byref(self._overlapped_read)) |
| 286 | if not read_ok and win32.GetLastError() not in (win32.ERROR_SUCCESS, win32.ERROR_IO_PENDING): |
cliechti | 1083be7 | 2011-12-28 20:45:30 +0000 | [diff] [blame] | 287 | raise SerialException("ReadFile failed (%r)" % ctypes.WinError()) |
Chris Liechti | 7731e98 | 2015-11-02 23:46:07 +0100 | [diff] [blame] | 288 | win32.GetOverlappedResult(self._port_handle, ctypes.byref(self._overlapped_read), ctypes.byref(rc), True) |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 289 | read = buf.raw[:rc.value] |
cliechti | f622faf | 2003-07-12 23:41:43 +0000 | [diff] [blame] | 290 | else: |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 291 | read = bytes() |
| 292 | return bytes(read) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 293 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 294 | def write(self, data): |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 295 | """Output the given byte string over the serial port.""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 296 | if not self._port_handle: |
| 297 | raise portNotOpenError |
cliechti | 23dc2a0 | 2009-07-30 17:25:09 +0000 | [diff] [blame] | 298 | #~ if not isinstance(data, (bytes, bytearray)): |
| 299 | #~ raise TypeError('expected %s or bytearray, got %s' % (bytes, type(data))) |
| 300 | # 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] | 301 | data = to_bytes(data) |
cliechti | b2f5fc8 | 2006-10-20 00:09:07 +0000 | [diff] [blame] | 302 | if data: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 303 | #~ win32event.ResetEvent(self._overlapped_write.hEvent) |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 304 | n = win32.DWORD() |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 305 | err = win32.WriteFile(self._port_handle, data, len(data), ctypes.byref(n), self._overlapped_write) |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 306 | if not err and win32.GetLastError() != win32.ERROR_IO_PENDING: |
cliechti | 1083be7 | 2011-12-28 20:45:30 +0000 | [diff] [blame] | 307 | raise SerialException("WriteFile failed (%r)" % ctypes.WinError()) |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 308 | if self._write_timeout != 0: # if blocking (None) or w/ write timeout (>0) |
cliechti | 31000fc | 2011-08-05 01:47:26 +0000 | [diff] [blame] | 309 | # Wait for the write to complete. |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 310 | #~ win32.WaitForSingleObject(self._overlapped_write.hEvent, win32.INFINITE) |
| 311 | err = win32.GetOverlappedResult(self._port_handle, self._overlapped_write, ctypes.byref(n), True) |
cliechti | 31000fc | 2011-08-05 01:47:26 +0000 | [diff] [blame] | 312 | if n.value != len(data): |
| 313 | raise writeTimeoutError |
cliechti | 23dc2a0 | 2009-07-30 17:25:09 +0000 | [diff] [blame] | 314 | return n.value |
| 315 | else: |
| 316 | return 0 |
cliechti | edfba4e | 2009-02-07 00:29:47 +0000 | [diff] [blame] | 317 | |
cliechti | 2640978 | 2013-05-31 00:47:16 +0000 | [diff] [blame] | 318 | def flush(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 319 | """\ |
| 320 | Flush of file like objects. In this case, wait until all data |
| 321 | is written. |
| 322 | """ |
Chris Liechti | bc119fd | 2015-09-09 17:08:25 +0200 | [diff] [blame] | 323 | while self.out_waiting: |
cliechti | 2640978 | 2013-05-31 00:47:16 +0000 | [diff] [blame] | 324 | time.sleep(0.05) |
| 325 | # XXX could also use WaitCommEvent with mask EV_TXEMPTY, but it would |
| 326 | # require overlapped IO and its also only possible to set a single mask |
| 327 | # on the port--- |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 328 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 329 | def reset_input_buffer(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 330 | """Clear input buffer, discarding all that is in the buffer.""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 331 | if not self._port_handle: |
| 332 | raise portNotOpenError |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 333 | win32.PurgeComm(self._port_handle, win32.PURGE_RXCLEAR | win32.PURGE_RXABORT) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 334 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 335 | def reset_output_buffer(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 336 | """\ |
| 337 | Clear output buffer, aborting the current output and discarding all |
| 338 | that is in the buffer. |
| 339 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 340 | if not self._port_handle: |
| 341 | raise portNotOpenError |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 342 | win32.PurgeComm(self._port_handle, win32.PURGE_TXCLEAR | win32.PURGE_TXABORT) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 343 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 344 | def _update_break_state(self): |
cliechti | 997b63c | 2008-06-21 00:09:31 +0000 | [diff] [blame] | 345 | """Set break: Controls TXD. When active, to transmitting is possible.""" |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 346 | if not self._port_handle: |
| 347 | raise portNotOpenError |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 348 | if self._break_state: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 349 | win32.SetCommBreak(self._port_handle) |
cliechti | 997b63c | 2008-06-21 00:09:31 +0000 | [diff] [blame] | 350 | else: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 351 | win32.ClearCommBreak(self._port_handle) |
cliechti | 997b63c | 2008-06-21 00:09:31 +0000 | [diff] [blame] | 352 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 353 | def _update_rts_state(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 354 | """Set terminal status line: Request To Send""" |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 355 | if self._rts_state: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 356 | win32.EscapeCommFunction(self._port_handle, win32.SETRTS) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 357 | else: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 358 | win32.EscapeCommFunction(self._port_handle, win32.CLRRTS) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 359 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 360 | def _update_dtr_state(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 361 | """Set terminal status line: Data Terminal Ready""" |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 362 | if self._dtr_state: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 363 | win32.EscapeCommFunction(self._port_handle, win32.SETDTR) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 364 | else: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 365 | win32.EscapeCommFunction(self._port_handle, win32.CLRDTR) |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 366 | |
| 367 | def _GetCommModemStatus(self): |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 368 | if not self._port_handle: |
| 369 | raise portNotOpenError |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 370 | stat = win32.DWORD() |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 371 | win32.GetCommModemStatus(self._port_handle, ctypes.byref(stat)) |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 372 | return stat.value |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 373 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 374 | @property |
| 375 | def cts(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 376 | """Read terminal status line: Clear To Send""" |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 377 | return win32.MS_CTS_ON & self._GetCommModemStatus() != 0 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 378 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 379 | @property |
| 380 | def dsr(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 381 | """Read terminal status line: Data Set Ready""" |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 382 | return win32.MS_DSR_ON & self._GetCommModemStatus() != 0 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 383 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 384 | @property |
| 385 | def ri(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 386 | """Read terminal status line: Ring Indicator""" |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 387 | return win32.MS_RING_ON & self._GetCommModemStatus() != 0 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 388 | |
Chris Liechti | ef1fe25 | 2015-08-27 23:25:21 +0200 | [diff] [blame] | 389 | @property |
| 390 | def cd(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 391 | """Read terminal status line: Carrier Detect""" |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 392 | return win32.MS_RLSD_ON & self._GetCommModemStatus() != 0 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 393 | |
cliechti | a30a8a0 | 2003-10-05 12:28:13 +0000 | [diff] [blame] | 394 | # - - platform specific - - - - |
| 395 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 396 | def set_buffer_size(self, rx_size=4096, tx_size=None): |
cliechti | 5919964 | 2011-12-28 20:54:30 +0000 | [diff] [blame] | 397 | """\ |
| 398 | Recommend a buffer size to the driver (device driver can ignore this |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 399 | value). Must be called before the port is opended. |
cliechti | 5919964 | 2011-12-28 20:54:30 +0000 | [diff] [blame] | 400 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 401 | if tx_size is None: |
| 402 | tx_size = rx_size |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 403 | win32.SetupComm(self._port_handle, rx_size, tx_size) |
cliechti | 5919964 | 2011-12-28 20:54:30 +0000 | [diff] [blame] | 404 | |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 405 | def set_output_flow_control(self, enable=True): |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 406 | """\ |
| 407 | Manually control flow - when software flow control is enabled. |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 408 | This will do the same as if XON (true) or XOFF (false) are received |
| 409 | from the other device and control the transmission accordingly. |
cliechti | 2f0f8a3 | 2011-12-28 22:10:00 +0000 | [diff] [blame] | 410 | WARNING: this function is not portable to different platforms! |
| 411 | """ |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 412 | if not self._port_handle: |
| 413 | raise portNotOpenError |
Chris Liechti | 518b0d3 | 2015-08-30 02:20:39 +0200 | [diff] [blame] | 414 | if enable: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 415 | win32.EscapeCommFunction(self._port_handle, win32.SETXON) |
cliechti | a30a8a0 | 2003-10-05 12:28:13 +0000 | [diff] [blame] | 416 | else: |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 417 | win32.EscapeCommFunction(self._port_handle, win32.SETXOFF) |
cliechti | a30a8a0 | 2003-10-05 12:28:13 +0000 | [diff] [blame] | 418 | |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 419 | @property |
| 420 | def out_waiting(self): |
| 421 | """Return how many bytes the in the outgoing buffer""" |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 422 | flags = win32.DWORD() |
| 423 | comstat = win32.COMSTAT() |
Chris Liechti | 3ad62fb | 2015-08-29 21:53:32 +0200 | [diff] [blame] | 424 | if not win32.ClearCommError(self._port_handle, ctypes.byref(flags), ctypes.byref(comstat)): |
cliechti | 183d4ae | 2009-07-23 22:03:51 +0000 | [diff] [blame] | 425 | raise SerialException('call to ClearCommError failed') |
cliechti | c8e83d8 | 2009-07-21 21:34:05 +0000 | [diff] [blame] | 426 | return comstat.cbOutQue |
| 427 | |
cliechti | 1f89a0a | 2011-08-05 02:53:24 +0000 | [diff] [blame] | 428 | |
cliechti | edfba4e | 2009-02-07 00:29:47 +0000 | [diff] [blame] | 429 | # Nur Testfunktion!! |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 430 | if __name__ == '__main__': |
Chris Liechti | 033f17c | 2015-08-30 21:28:04 +0200 | [diff] [blame] | 431 | import sys |
cliechti | 93db61b | 2006-08-26 19:16:18 +0000 | [diff] [blame] | 432 | s = Serial(0) |
cliechti | 7aaead3 | 2009-07-23 14:02:41 +0000 | [diff] [blame] | 433 | sys.stdout.write("%s\n" % s) |
cliechti | edfba4e | 2009-02-07 00:29:47 +0000 | [diff] [blame] | 434 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 435 | s = Serial() |
cliechti | 7aaead3 | 2009-07-23 14:02:41 +0000 | [diff] [blame] | 436 | sys.stdout.write("%s\n" % s) |
cliechti | edfba4e | 2009-02-07 00:29:47 +0000 | [diff] [blame] | 437 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 438 | s.baudrate = 19200 |
| 439 | s.databits = 7 |
| 440 | s.close() |
cliechti | 93db61b | 2006-08-26 19:16:18 +0000 | [diff] [blame] | 441 | s.port = 0 |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 442 | s.open() |
cliechti | 7aaead3 | 2009-07-23 14:02:41 +0000 | [diff] [blame] | 443 | sys.stdout.write("%s\n" % s) |