diff options
author | cliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a> | 2002-02-12 23:24:41 +0000 |
---|---|---|
committer | cliechti <cliechti@f19166aa-fa4f-0410-85c2-fa1106f25c8a> | 2002-02-12 23:24:41 +0000 |
commit | 89b4af15d858f4289c5e1c8040b171cbe95c4e6e (patch) | |
tree | 108dfde04fdaad726ff95fe5cc043aa40d4e281f /pyserial/serial/serialjava.py | |
parent | 87a11f32a29fcd5f16f67143f566f2839f7b2f7b (diff) | |
download | pyserial-git-89b4af15d858f4289c5e1c8040b171cbe95c4e6e.tar.gz |
This commit was generated by cvs2svn to compensate for changes in r3,
which included commits to RCS files with non-trunk default branches.
Diffstat (limited to 'pyserial/serial/serialjava.py')
-rw-r--r-- | pyserial/serial/serialjava.py | 187 |
1 files changed, 187 insertions, 0 deletions
diff --git a/pyserial/serial/serialjava.py b/pyserial/serial/serialjava.py new file mode 100644 index 0000000..f7601ec --- /dev/null +++ b/pyserial/serial/serialjava.py @@ -0,0 +1,187 @@ +#!/usr/bin/env python +#module for serial IO for Jython and JavaComm +#see serial.py +# +#(C) 2002 Chris Liechti <cliechti@gmx.net> +# this is distributed under a free software license, see license.txt + +import sys, os, string, javax.comm + +VERSION = string.split("$Revision: 1.1.1.1 $")[1] #extract CVS version + +PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK, PARITY_SPACE = (0,1,2,3,4) +STOPBITS_ONE, STOPBITS_TWO, STOPBITS_ONE_HALVE = (1, 2, 3) +FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS = (5,6,7,8) + + +portNotOpenError = ValueError('port not open') + +def device(portnumber): + enum = javax.comm.CommPortIdentifier.getPortIdentifiers() + ports = [] + while enum.hasMoreElements(): + el = enum.nextElement() + if el.getPortType() == javax.comm.CommPortIdentifier.PORT_SERIAL: + ports.append(el) + return ports[portnumber] + +class Serial: + def __init__(self, + port, #number of device, numbering starts at + #zero. if everything fails, the user + #can specify a device string, note + #that this isn't portable anymore + baudrate=9600, #baudrate + bytesize=EIGHTBITS, #number of databits + parity=PARITY_NONE, #enable parity checking + stopbits=STOPBITS_ONE, #number of stopbits + timeout=None, #set a timeout value, None for waiting forever + xonxoff=0, #enable software flow control + rtscts=0, #enable RTS/CTS flow control + ): + + if type(port) == type(''): #strings are taken directly + portId = javax.comm.CommPortIdentifier.getPortIdentifier(port) + else: + portId = device(port) #numbers are transformed to a comportid obj + self.portstr = portId.getName() + + self.sPort = portId.open("python serial module", 10) + self.instream = self.sPort.getInputStream() + self.outstream = self.sPort.getOutputStream() + self.sPort.enableReceiveTimeout(30) + if bytesize == FIVEBITS: + databits = javax.comm.SerialPort.DATABITS_5 + elif bytesize == SIXBITS: + databits = javax.comm.SerialPort.DATABITS_6 + elif bytesize == SEVENBITS: + databits = javax.comm.SerialPort.DATABITS_7 + elif bytesize == EIGHTBITS: + databits = javax.comm.SerialPort.DATABITS_8 + else: + raise ValueError, "unsupported bytesize" + + if stopbits == STOPBITS_ONE: + jstopbits = javax.comm.SerialPort.STOPBITS_1 + elif stopbits == STOPBITS_ONE_HALVE: + jstopbits = javax.comm.SerialPort.STOPBITS_1_5 + elif stopbits == STOPBITS_TWO: + jstopbits = javax.comm.SerialPort.STOPBITS_2 + else: + raise ValueError, "unsupported number of stopbits" + + if parity == PARITY_NONE: + jparity = javax.comm.SerialPort.PARITY_NONE + elif parity == PARITY_EVEN: + jparity = javax.comm.SerialPort.PARITY_EVEN + elif parity == PARITY_ODD: + jparity = javax.comm.SerialPort.PARITY_ODD + elif parity == PARITY_MARK: + jparity = javax.comm.SerialPort.PARITY_MARK + elif parity == PARITY_SPACE: + jparity = javax.comm.SerialPort.PARITY_SPACE + else: + raise ValueError, "unsupported parity type" + + jflowin = jflowout = 0 + if rtscts: + jflowin = jflowin | javax.comm.SerialPort.FLOWCONTROL_RTSCTS_IN + jflowout = jflowout | javax.comm.SerialPort.FLOWCONTROL_RTSCTS_OUT + if xonxoff: + jflowin = jflowin | javax.comm.SerialPort.FLOWCONTROL_XONXOFF_IN + jflowout = jflowout | javax.comm.SerialPort.FLOWCONTROL_XONXOFF_OUT + + self.sPort.setSerialPortParams(baudrate, databits, jstopbits, jparity) + self.sPort.setFlowControlMode(jflowin | jflowout) + + self.timeout = timeout + if timeout: + self.sPort.enableReceiveTimeout(timeout*1000) + else: + self.sPort.disableReceiveTimeout() + + + def close(self): + if self.sPort: + self.instream.close() + self.outstream.close() + self.sPort.close() + self.sPort = None + + def inWaiting(self): + if not self.sPort: raise portNotOpenError + return self.instream.available() + + def write(self, data): + if not self.sPort: raise portNotOpenError + self.outstream.write(data) + + def read(self, size=1): + if not self.sPort: raise portNotOpenError + res = '' + while len(res) < size: + x = self.instream.read() + if x == -1: + if self.timeout: + break + else: + res = res + chr(x) + return res + + def flushInput(self): + if not self.sPort: raise portNotOpenError + self.instream.skip(self.instream.available()) + + def flushOutput(self): + if not self.sPort: raise portNotOpenError + self.outstream.flush() + + def sendBreak(self): + if not self.sPort: raise portNotOpenError + self.sPort.sendBreak() + + def getDSR(self): + if not self.sPort: raise portNotOpenError + self.sPort.isDSR() + + def getCD(self): + if not self.sPort: raise portNotOpenError + self.sPort.isCD() + + def getRI(self): + if not self.sPort: raise portNotOpenError + self.sPort.isRI() + + def getCTS(self): + if not self.sPort: raise portNotOpenError + self.sPort.isCTS() + + def setDTR(self,on=1): + if not self.sPort: raise portNotOpenError + self.sPort.setDTR(on) + + def setRTS(self,on=1): + if not self.sPort: raise portNotOpenError + self.sPort.setRTS(on) + +if __name__ == '__main__': + s = Serial(0, + baudrate=19200, #baudrate + bytesize=EIGHTBITS, #number of databits + parity=PARITY_EVEN, #enable parity checking + stopbits=STOPBITS_ONE, #number of stopbits + timeout=3, #set a timeout value, None for waiting forever + xonxoff=0, #enable software flow control + rtscts=0, #enable RTS/CTS flow control + ) + s.setRTS(1) + s.setDTR(1) + s.flushInput() + s.flushOutput() + s.write('hello') + print repr(s.read(5)) + print s.inWaiting() + del s + + + |