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