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 | #module for serial IO for Jython and JavaComm |
cliechti | e8f75f9 | 2002-02-14 01:33:33 +0000 | [diff] [blame] | 3 | #see __init__.py |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 4 | # |
| 5 | #(C) 2002 Chris Liechti <cliechti@gmx.net> |
| 6 | # this is distributed under a free software license, see license.txt |
| 7 | |
| 8 | import sys, os, string, javax.comm |
cliechti | e8f75f9 | 2002-02-14 01:33:33 +0000 | [diff] [blame] | 9 | import serialutil |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 10 | |
cliechti | a9e4e95 | 2002-05-26 01:20:22 +0000 | [diff] [blame^] | 11 | VERSION = string.split("$Revision: 1.6 $")[1] #extract CVS version |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 12 | |
| 13 | PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK, PARITY_SPACE = (0,1,2,3,4) |
| 14 | STOPBITS_ONE, STOPBITS_TWO, STOPBITS_ONE_HALVE = (1, 2, 3) |
| 15 | FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS = (5,6,7,8) |
| 16 | |
| 17 | |
| 18 | portNotOpenError = ValueError('port not open') |
| 19 | |
| 20 | def device(portnumber): |
| 21 | enum = javax.comm.CommPortIdentifier.getPortIdentifiers() |
| 22 | ports = [] |
| 23 | while enum.hasMoreElements(): |
| 24 | el = enum.nextElement() |
| 25 | if el.getPortType() == javax.comm.CommPortIdentifier.PORT_SERIAL: |
| 26 | ports.append(el) |
| 27 | return ports[portnumber] |
| 28 | |
cliechti | e8f75f9 | 2002-02-14 01:33:33 +0000 | [diff] [blame] | 29 | class Serial(serialutil.FileLike): |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 30 | def __init__(self, |
| 31 | port, #number of device, numbering starts at |
| 32 | #zero. if everything fails, the user |
| 33 | #can specify a device string, note |
| 34 | #that this isn't portable anymore |
| 35 | baudrate=9600, #baudrate |
| 36 | bytesize=EIGHTBITS, #number of databits |
| 37 | parity=PARITY_NONE, #enable parity checking |
| 38 | stopbits=STOPBITS_ONE, #number of stopbits |
| 39 | timeout=None, #set a timeout value, None for waiting forever |
| 40 | xonxoff=0, #enable software flow control |
| 41 | rtscts=0, #enable RTS/CTS flow control |
| 42 | ): |
| 43 | |
| 44 | if type(port) == type(''): #strings are taken directly |
| 45 | portId = javax.comm.CommPortIdentifier.getPortIdentifier(port) |
| 46 | else: |
| 47 | portId = device(port) #numbers are transformed to a comportid obj |
| 48 | self.portstr = portId.getName() |
cliechti | 4616bf1 | 2002-04-08 23:13:14 +0000 | [diff] [blame] | 49 | try: |
| 50 | self.sPort = portId.open("python serial module", 10) |
| 51 | except Exception, msg: |
| 52 | self.sPort = None |
| 53 | raise serialutil.SerialException, "could not open port: %s" % msg |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 54 | self.instream = self.sPort.getInputStream() |
| 55 | self.outstream = self.sPort.getOutputStream() |
| 56 | self.sPort.enableReceiveTimeout(30) |
| 57 | if bytesize == FIVEBITS: |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 58 | self.databits = javax.comm.SerialPort.DATABITS_5 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 59 | elif bytesize == SIXBITS: |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 60 | self.databits = javax.comm.SerialPort.DATABITS_6 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 61 | elif bytesize == SEVENBITS: |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 62 | self.databits = javax.comm.SerialPort.DATABITS_7 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 63 | elif bytesize == EIGHTBITS: |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 64 | self.databits = javax.comm.SerialPort.DATABITS_8 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 65 | else: |
| 66 | raise ValueError, "unsupported bytesize" |
| 67 | |
| 68 | if stopbits == STOPBITS_ONE: |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 69 | self.jstopbits = javax.comm.SerialPort.STOPBITS_1 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 70 | elif stopbits == STOPBITS_ONE_HALVE: |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 71 | self.jstopbits = javax.comm.SerialPort.STOPBITS_1_5 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 72 | elif stopbits == STOPBITS_TWO: |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 73 | self.jstopbits = javax.comm.SerialPort.STOPBITS_2 |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 74 | else: |
| 75 | raise ValueError, "unsupported number of stopbits" |
| 76 | |
| 77 | if parity == PARITY_NONE: |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 78 | self.jparity = javax.comm.SerialPort.PARITY_NONE |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 79 | elif parity == PARITY_EVEN: |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 80 | self.jparity = javax.comm.SerialPort.PARITY_EVEN |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 81 | elif parity == PARITY_ODD: |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 82 | self.jparity = javax.comm.SerialPort.PARITY_ODD |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 83 | elif parity == PARITY_MARK: |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 84 | self.jparity = javax.comm.SerialPort.PARITY_MARK |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 85 | elif parity == PARITY_SPACE: |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 86 | self.jparity = javax.comm.SerialPort.PARITY_SPACE |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 87 | else: |
| 88 | raise ValueError, "unsupported parity type" |
| 89 | |
| 90 | jflowin = jflowout = 0 |
| 91 | if rtscts: |
| 92 | jflowin = jflowin | javax.comm.SerialPort.FLOWCONTROL_RTSCTS_IN |
| 93 | jflowout = jflowout | javax.comm.SerialPort.FLOWCONTROL_RTSCTS_OUT |
| 94 | if xonxoff: |
| 95 | jflowin = jflowin | javax.comm.SerialPort.FLOWCONTROL_XONXOFF_IN |
| 96 | jflowout = jflowout | javax.comm.SerialPort.FLOWCONTROL_XONXOFF_OUT |
| 97 | |
cliechti | d441974 | 2002-03-05 00:34:09 +0000 | [diff] [blame] | 98 | self.sPort.setSerialPortParams(baudrate, self.databits, self.jstopbits, self.jparity) |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 99 | self.sPort.setFlowControlMode(jflowin | jflowout) |
| 100 | |
| 101 | self.timeout = timeout |
cliechti | a9e4e95 | 2002-05-26 01:20:22 +0000 | [diff] [blame^] | 102 | if timeout >= 0: |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 103 | self.sPort.enableReceiveTimeout(timeout*1000) |
| 104 | else: |
| 105 | self.sPort.disableReceiveTimeout() |
| 106 | |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 107 | def close(self): |
| 108 | if self.sPort: |
| 109 | self.instream.close() |
| 110 | self.outstream.close() |
| 111 | self.sPort.close() |
| 112 | self.sPort = None |
| 113 | |
cliechti | 95c6221 | 2002-03-04 22:17:53 +0000 | [diff] [blame] | 114 | def setBaudrate(self, baudrate): |
| 115 | """change baudrate after port is open""" |
| 116 | if not self.sPort: raise portNotOpenError |
| 117 | self.sPort.setSerialPortParams(baudrate, self.databits, self.jstopbits, self.jparity) |
| 118 | |
| 119 | |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 120 | def inWaiting(self): |
| 121 | if not self.sPort: raise portNotOpenError |
| 122 | return self.instream.available() |
| 123 | |
| 124 | def write(self, data): |
| 125 | if not self.sPort: raise portNotOpenError |
| 126 | self.outstream.write(data) |
| 127 | |
| 128 | def read(self, size=1): |
| 129 | if not self.sPort: raise portNotOpenError |
cliechti | a9e4e95 | 2002-05-26 01:20:22 +0000 | [diff] [blame^] | 130 | read = '' |
| 131 | if size > 0: |
| 132 | while len(read) < size: |
| 133 | x = self.instream.read() |
| 134 | if x == -1: |
| 135 | if self.timeout >= 0: |
| 136 | break |
| 137 | else: |
| 138 | read = read + chr(x) |
| 139 | return read |
cliechti | 89b4af1 | 2002-02-12 23:24:41 +0000 | [diff] [blame] | 140 | |
| 141 | def flushInput(self): |
| 142 | if not self.sPort: raise portNotOpenError |
| 143 | self.instream.skip(self.instream.available()) |
| 144 | |
| 145 | def flushOutput(self): |
| 146 | if not self.sPort: raise portNotOpenError |
| 147 | self.outstream.flush() |
| 148 | |
| 149 | def sendBreak(self): |
| 150 | if not self.sPort: raise portNotOpenError |
| 151 | self.sPort.sendBreak() |
| 152 | |
| 153 | def getDSR(self): |
| 154 | if not self.sPort: raise portNotOpenError |
| 155 | self.sPort.isDSR() |
| 156 | |
| 157 | def getCD(self): |
| 158 | if not self.sPort: raise portNotOpenError |
| 159 | self.sPort.isCD() |
| 160 | |
| 161 | def getRI(self): |
| 162 | if not self.sPort: raise portNotOpenError |
| 163 | self.sPort.isRI() |
| 164 | |
| 165 | def getCTS(self): |
| 166 | if not self.sPort: raise portNotOpenError |
| 167 | self.sPort.isCTS() |
| 168 | |
| 169 | def setDTR(self,on=1): |
| 170 | if not self.sPort: raise portNotOpenError |
| 171 | self.sPort.setDTR(on) |
| 172 | |
| 173 | def setRTS(self,on=1): |
| 174 | if not self.sPort: raise portNotOpenError |
| 175 | self.sPort.setRTS(on) |
| 176 | |
| 177 | if __name__ == '__main__': |
| 178 | s = Serial(0, |
| 179 | baudrate=19200, #baudrate |
| 180 | bytesize=EIGHTBITS, #number of databits |
| 181 | parity=PARITY_EVEN, #enable parity checking |
| 182 | stopbits=STOPBITS_ONE, #number of stopbits |
| 183 | timeout=3, #set a timeout value, None for waiting forever |
| 184 | xonxoff=0, #enable software flow control |
| 185 | rtscts=0, #enable RTS/CTS flow control |
| 186 | ) |
| 187 | s.setRTS(1) |
| 188 | s.setDTR(1) |
| 189 | s.flushInput() |
| 190 | s.flushOutput() |
| 191 | s.write('hello') |
| 192 | print repr(s.read(5)) |
| 193 | print s.inWaiting() |
| 194 | del s |
| 195 | |
| 196 | |
| 197 | |