cliechti | e8f75f9 | 2002-02-14 01:33:33 +0000 | [diff] [blame] | 1 | #!jython |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 2 | # |
Chris Liechti | 3e02f70 | 2015-12-16 23:06:04 +0100 | [diff] [blame^] | 3 | # Backend Jython with JavaComm |
cliechti | 809b82e | 2008-06-22 22:45:04 +0000 | [diff] [blame] | 4 | # |
Chris Liechti | 3e02f70 | 2015-12-16 23:06:04 +0100 | [diff] [blame^] | 5 | # This file is part of pySerial. https://github.com/pyserial/pyserial |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 6 | # (C) 2002-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 | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 9 | |
cliechti | 39cfb7b | 2011-08-22 00:30:07 +0000 | [diff] [blame] | 10 | from serial.serialutil import * |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 11 | |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 12 | |
cliechti | 809b82e | 2008-06-22 22:45:04 +0000 | [diff] [blame] | 13 | def my_import(name): |
| 14 | mod = __import__(name) |
| 15 | components = name.split('.') |
| 16 | for comp in components[1:]: |
| 17 | mod = getattr(mod, comp) |
| 18 | return mod |
| 19 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 20 | |
cliechti | 809b82e | 2008-06-22 22:45:04 +0000 | [diff] [blame] | 21 | def detect_java_comm(names): |
| 22 | """try given list of modules and return that imports""" |
| 23 | for name in names: |
| 24 | try: |
| 25 | mod = my_import(name) |
| 26 | mod.SerialPort |
| 27 | return mod |
| 28 | except (ImportError, AttributeError): |
| 29 | pass |
| 30 | raise ImportError("No Java Communications API implementation found") |
| 31 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 32 | |
cliechti | 809b82e | 2008-06-22 22:45:04 +0000 | [diff] [blame] | 33 | # Java Communications API implementations |
| 34 | # http://mho.republika.pl/java/comm/ |
| 35 | |
| 36 | comm = detect_java_comm([ |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 37 | 'javax.comm', # Sun/IBM |
| 38 | 'gnu.io', # RXTX |
cliechti | 809b82e | 2008-06-22 22:45:04 +0000 | [diff] [blame] | 39 | ]) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 40 | |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 41 | |
| 42 | def device(portnumber): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 43 | """Turn a port number into a device name""" |
cliechti | 809b82e | 2008-06-22 22:45:04 +0000 | [diff] [blame] | 44 | enum = comm.CommPortIdentifier.getPortIdentifiers() |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 45 | ports = [] |
| 46 | while enum.hasMoreElements(): |
| 47 | el = enum.nextElement() |
cliechti | 809b82e | 2008-06-22 22:45:04 +0000 | [diff] [blame] | 48 | if el.getPortType() == comm.CommPortIdentifier.PORT_SERIAL: |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 49 | ports.append(el) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 50 | return ports[portnumber].getName() |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 51 | |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 52 | |
Chris Liechti | ef6b7b4 | 2015-08-06 22:19:26 +0200 | [diff] [blame] | 53 | class Serial(SerialBase): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 54 | """\ |
| 55 | Serial port class, implemented with Java Communications API and |
| 56 | thus usable with jython and the appropriate java extension. |
| 57 | """ |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 58 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 59 | def open(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 60 | """\ |
| 61 | Open port with current settings. This may throw a SerialException |
| 62 | if the port cannot be opened. |
| 63 | """ |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 64 | if self._port is None: |
| 65 | raise SerialException("Port must be configured before it can be used.") |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 66 | if self.is_open: |
cliechti | 8f69e70 | 2011-03-19 00:22:32 +0000 | [diff] [blame] | 67 | raise SerialException("Port is already open.") |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 68 | if type(self._port) == type(''): # strings are taken directly |
cliechti | 809b82e | 2008-06-22 22:45:04 +0000 | [diff] [blame] | 69 | portId = comm.CommPortIdentifier.getPortIdentifier(self._port) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 70 | else: |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 71 | portId = comm.CommPortIdentifier.getPortIdentifier(device(self._port)) # numbers are transformed to a comport id obj |
cliechti | 4616bf1 | 2002-04-08 23:13:14 +0000 | [diff] [blame] | 72 | try: |
| 73 | self.sPort = portId.open("python serial module", 10) |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 74 | except Exception as msg: |
cliechti | 4616bf1 | 2002-04-08 23:13:14 +0000 | [diff] [blame] | 75 | self.sPort = None |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 76 | raise SerialException("Could not open port: %s" % msg) |
| 77 | self._reconfigurePort() |
| 78 | self._instream = self.sPort.getInputStream() |
| 79 | self._outstream = self.sPort.getOutputStream() |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 80 | self.is_open = True |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 81 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 82 | def _reconfigurePort(self): |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 83 | """Set communication parameters on opened port.""" |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 84 | if not self.sPort: |
| 85 | raise SerialException("Can only operate on a valid port handle") |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 86 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 87 | self.sPort.enableReceiveTimeout(30) |
| 88 | if self._bytesize == FIVEBITS: |
cliechti | 809b82e | 2008-06-22 22:45:04 +0000 | [diff] [blame] | 89 | jdatabits = comm.SerialPort.DATABITS_5 |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 90 | elif self._bytesize == SIXBITS: |
cliechti | 809b82e | 2008-06-22 22:45:04 +0000 | [diff] [blame] | 91 | jdatabits = comm.SerialPort.DATABITS_6 |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 92 | elif self._bytesize == SEVENBITS: |
cliechti | 809b82e | 2008-06-22 22:45:04 +0000 | [diff] [blame] | 93 | jdatabits = comm.SerialPort.DATABITS_7 |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 94 | elif self._bytesize == EIGHTBITS: |
cliechti | 809b82e | 2008-06-22 22:45:04 +0000 | [diff] [blame] | 95 | jdatabits = comm.SerialPort.DATABITS_8 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 96 | else: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 97 | raise ValueError("unsupported bytesize: %r" % self._bytesize) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 98 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 99 | if self._stopbits == STOPBITS_ONE: |
cliechti | 809b82e | 2008-06-22 22:45:04 +0000 | [diff] [blame] | 100 | jstopbits = comm.SerialPort.STOPBITS_1 |
cliechti | c5aed01 | 2014-08-01 02:34:22 +0000 | [diff] [blame] | 101 | elif self._stopbits == STOPBITS_ONE_POINT_FIVE: |
| 102 | jstopbits = comm.SerialPort.STOPBITS_1_5 |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 103 | elif self._stopbits == STOPBITS_TWO: |
cliechti | 809b82e | 2008-06-22 22:45:04 +0000 | [diff] [blame] | 104 | jstopbits = comm.SerialPort.STOPBITS_2 |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 105 | else: |
| 106 | raise ValueError("unsupported number of stopbits: %r" % self._stopbits) |
| 107 | |
| 108 | if self._parity == PARITY_NONE: |
cliechti | 809b82e | 2008-06-22 22:45:04 +0000 | [diff] [blame] | 109 | jparity = comm.SerialPort.PARITY_NONE |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 110 | elif self._parity == PARITY_EVEN: |
cliechti | 809b82e | 2008-06-22 22:45:04 +0000 | [diff] [blame] | 111 | jparity = comm.SerialPort.PARITY_EVEN |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 112 | elif self._parity == PARITY_ODD: |
cliechti | 809b82e | 2008-06-22 22:45:04 +0000 | [diff] [blame] | 113 | jparity = comm.SerialPort.PARITY_ODD |
| 114 | elif self._parity == PARITY_MARK: |
| 115 | jparity = comm.SerialPort.PARITY_MARK |
| 116 | elif self._parity == PARITY_SPACE: |
| 117 | jparity = comm.SerialPort.PARITY_SPACE |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 118 | else: |
| 119 | raise ValueError("unsupported parity type: %r" % self._parity) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 120 | |
| 121 | jflowin = jflowout = 0 |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 122 | if self._rtscts: |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 123 | jflowin |= comm.SerialPort.FLOWCONTROL_RTSCTS_IN |
| 124 | jflowout |= comm.SerialPort.FLOWCONTROL_RTSCTS_OUT |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 125 | if self._xonxoff: |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 126 | jflowin |= comm.SerialPort.FLOWCONTROL_XONXOFF_IN |
| 127 | jflowout |= comm.SerialPort.FLOWCONTROL_XONXOFF_OUT |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 128 | |
cliechti | 6670f73 | 2009-07-25 23:49:26 +0000 | [diff] [blame] | 129 | self.sPort.setSerialPortParams(self._baudrate, jdatabits, jstopbits, jparity) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 130 | self.sPort.setFlowControlMode(jflowin | jflowout) |
cliechti | 58b481c | 2009-02-16 20:42:32 +0000 | [diff] [blame] | 131 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 132 | if self._timeout >= 0: |
cliechti | c5aed01 | 2014-08-01 02:34:22 +0000 | [diff] [blame] | 133 | self.sPort.enableReceiveTimeout(int(self._timeout*1000)) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 134 | else: |
| 135 | self.sPort.disableReceiveTimeout() |
| 136 | |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 137 | def close(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 138 | """Close port""" |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 139 | if self.is_open: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 140 | if self.sPort: |
| 141 | self._instream.close() |
| 142 | self._outstream.close() |
| 143 | self.sPort.close() |
| 144 | self.sPort = None |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 145 | self.is_open = False |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 146 | |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 147 | # - - - - - - - - - - - - - - - - - - - - - - - - |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 148 | |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 149 | @property |
| 150 | def in_waiting(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 151 | """Return the number of characters currently in the input buffer.""" |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 152 | if not self.sPort: |
| 153 | raise portNotOpenError |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 154 | return self._instream.available() |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 155 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 156 | def read(self, size=1): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 157 | """\ |
| 158 | Read size bytes from the serial port. If a timeout is set it may |
| 159 | return less characters as requested. With no timeout it will block |
| 160 | until the requested number of bytes is read. |
| 161 | """ |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 162 | if not self.sPort: |
| 163 | raise portNotOpenError |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 164 | read = bytearray() |
cliechti | a9e4e95 | 2002-05-26 01:20:22 +0000 | [diff] [blame] | 165 | if size > 0: |
| 166 | while len(read) < size: |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 167 | x = self._instream.read() |
cliechti | a9e4e95 | 2002-05-26 01:20:22 +0000 | [diff] [blame] | 168 | if x == -1: |
| 169 | if self.timeout >= 0: |
| 170 | break |
| 171 | else: |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 172 | read.append(x) |
| 173 | return bytes(read) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 174 | |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 175 | def write(self, data): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 176 | """Output the given string over the serial port.""" |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 177 | if not self.sPort: |
| 178 | raise portNotOpenError |
cliechti | ddd7813 | 2009-07-28 01:13:28 +0000 | [diff] [blame] | 179 | if not isinstance(data, (bytes, bytearray)): |
| 180 | raise TypeError('expected %s or bytearray, got %s' % (bytes, type(data))) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 181 | self._outstream.write(data) |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 182 | return len(data) |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 183 | |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 184 | def reset_input_buffer(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 185 | """Clear input buffer, discarding all that is in the buffer.""" |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 186 | if not self.sPort: |
| 187 | raise portNotOpenError |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 188 | self._instream.skip(self._instream.available()) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 189 | |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 190 | def reset_output_buffer(self): |
cliechti | 7d44856 | 2014-08-03 21:57:45 +0000 | [diff] [blame] | 191 | """\ |
| 192 | Clear output buffer, aborting the current output and |
| 193 | discarding all that is in the buffer. |
| 194 | """ |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 195 | if not self.sPort: |
| 196 | raise portNotOpenError |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 197 | self._outstream.flush() |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 198 | |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 199 | def send_break(self, duration=0.25): |
cliechti | 997b63c | 2008-06-21 00:09:31 +0000 | [diff] [blame] | 200 | """Send break condition. Timed, returns to idle state after given duration.""" |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 201 | if not self.sPort: |
| 202 | raise portNotOpenError |
cliechti | aaa0460 | 2006-02-05 23:02:46 +0000 | [diff] [blame] | 203 | self.sPort.sendBreak(duration*1000.0) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 204 | |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 205 | def _update_break_state(self): |
cliechti | 997b63c | 2008-06-21 00:09:31 +0000 | [diff] [blame] | 206 | """Set break: Controls TXD. When active, to transmitting is possible.""" |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 207 | if self.fd is None: |
| 208 | raise portNotOpenError |
| 209 | raise SerialException("The _update_break_state function is not implemented in java.") |
cliechti | 997b63c | 2008-06-21 00:09:31 +0000 | [diff] [blame] | 210 | |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 211 | def _update_rts_state(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 212 | """Set terminal status line: Request To Send""" |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 213 | if not self.sPort: |
| 214 | raise portNotOpenError |
| 215 | self.sPort.setRTS(self._rts_state) |
cliechti | f81362e | 2009-07-25 03:44:33 +0000 | [diff] [blame] | 216 | |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 217 | def _update_dtr_state(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 218 | """Set terminal status line: Data Terminal Ready""" |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 219 | if not self.sPort: |
| 220 | raise portNotOpenError |
| 221 | self.sPort.setDTR(self._dtr_state) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 222 | |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 223 | @property |
| 224 | def cts(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 225 | """Read terminal status line: Clear To Send""" |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 226 | if not self.sPort: |
| 227 | raise portNotOpenError |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 228 | self.sPort.isCTS() |
| 229 | |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 230 | @property |
| 231 | def dsr(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 232 | """Read terminal status line: Data Set Ready""" |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 233 | if not self.sPort: |
| 234 | raise portNotOpenError |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 235 | self.sPort.isDSR() |
| 236 | |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 237 | @property |
| 238 | def ri(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 239 | """Read terminal status line: Ring Indicator""" |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 240 | if not self.sPort: |
| 241 | raise portNotOpenError |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 242 | self.sPort.isRI() |
| 243 | |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 244 | @property |
| 245 | def cd(self): |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 246 | """Read terminal status line: Carrier Detect""" |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 247 | if not self.sPort: |
| 248 | raise portNotOpenError |
cliechti | d6bf52c | 2003-10-01 02:28:12 +0000 | [diff] [blame] | 249 | self.sPort.isCD() |
| 250 | |
| 251 | |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 252 | if __name__ == '__main__': |
| 253 | s = Serial(0, |
cliechti | 4a567a0 | 2009-07-27 22:09:31 +0000 | [diff] [blame] | 254 | baudrate=19200, # baudrate |
| 255 | bytesize=EIGHTBITS, # number of databits |
| 256 | parity=PARITY_EVEN, # enable parity checking |
| 257 | stopbits=STOPBITS_ONE, # number of stopbits |
| 258 | timeout=3, # set a timeout value, None for waiting forever |
| 259 | xonxoff=0, # enable software flow control |
| 260 | rtscts=0, # enable RTS/CTS flow control |
| 261 | ) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 262 | s.setRTS(1) |
| 263 | s.setDTR(1) |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 264 | s.reset_input_buffer() |
| 265 | s.reset_output_buffer() |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 266 | s.write('hello') |
cliechti | 7aaead3 | 2009-07-23 14:02:41 +0000 | [diff] [blame] | 267 | sys.stdio.write('%r\n' % s.read(5)) |
Chris Liechti | 2cda7e6 | 2015-09-19 22:08:54 +0200 | [diff] [blame] | 268 | sys.stdio.write('%s\n' % s.in_waiting()) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 269 | del s |
| 270 | |