blob: a0d48a6009e508b7333a348533043edffe1013ec [file] [log] [blame]
cliechtie8f75f92002-02-14 01:33:33 +00001#!jython
cliechti89b4af12002-02-12 23:24:41 +00002#module for serial IO for Jython and JavaComm
cliechtie8f75f92002-02-14 01:33:33 +00003#see __init__.py
cliechti89b4af12002-02-12 23:24:41 +00004#
5#(C) 2002 Chris Liechti <cliechti@gmx.net>
6# this is distributed under a free software license, see license.txt
7
8import sys, os, string, javax.comm
cliechtie8f75f92002-02-14 01:33:33 +00009import serialutil
cliechti89b4af12002-02-12 23:24:41 +000010
cliechtia9e4e952002-05-26 01:20:22 +000011VERSION = string.split("$Revision: 1.6 $")[1] #extract CVS version
cliechti89b4af12002-02-12 23:24:41 +000012
13PARITY_NONE, PARITY_EVEN, PARITY_ODD, PARITY_MARK, PARITY_SPACE = (0,1,2,3,4)
14STOPBITS_ONE, STOPBITS_TWO, STOPBITS_ONE_HALVE = (1, 2, 3)
15FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS = (5,6,7,8)
16
17
18portNotOpenError = ValueError('port not open')
19
20def 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
cliechtie8f75f92002-02-14 01:33:33 +000029class Serial(serialutil.FileLike):
cliechti89b4af12002-02-12 23:24:41 +000030 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()
cliechti4616bf12002-04-08 23:13:14 +000049 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
cliechti89b4af12002-02-12 23:24:41 +000054 self.instream = self.sPort.getInputStream()
55 self.outstream = self.sPort.getOutputStream()
56 self.sPort.enableReceiveTimeout(30)
57 if bytesize == FIVEBITS:
cliechti95c62212002-03-04 22:17:53 +000058 self.databits = javax.comm.SerialPort.DATABITS_5
cliechti89b4af12002-02-12 23:24:41 +000059 elif bytesize == SIXBITS:
cliechti95c62212002-03-04 22:17:53 +000060 self.databits = javax.comm.SerialPort.DATABITS_6
cliechti89b4af12002-02-12 23:24:41 +000061 elif bytesize == SEVENBITS:
cliechti95c62212002-03-04 22:17:53 +000062 self.databits = javax.comm.SerialPort.DATABITS_7
cliechti89b4af12002-02-12 23:24:41 +000063 elif bytesize == EIGHTBITS:
cliechti95c62212002-03-04 22:17:53 +000064 self.databits = javax.comm.SerialPort.DATABITS_8
cliechti89b4af12002-02-12 23:24:41 +000065 else:
66 raise ValueError, "unsupported bytesize"
67
68 if stopbits == STOPBITS_ONE:
cliechti95c62212002-03-04 22:17:53 +000069 self.jstopbits = javax.comm.SerialPort.STOPBITS_1
cliechti89b4af12002-02-12 23:24:41 +000070 elif stopbits == STOPBITS_ONE_HALVE:
cliechti95c62212002-03-04 22:17:53 +000071 self.jstopbits = javax.comm.SerialPort.STOPBITS_1_5
cliechti89b4af12002-02-12 23:24:41 +000072 elif stopbits == STOPBITS_TWO:
cliechti95c62212002-03-04 22:17:53 +000073 self.jstopbits = javax.comm.SerialPort.STOPBITS_2
cliechti89b4af12002-02-12 23:24:41 +000074 else:
75 raise ValueError, "unsupported number of stopbits"
76
77 if parity == PARITY_NONE:
cliechti95c62212002-03-04 22:17:53 +000078 self.jparity = javax.comm.SerialPort.PARITY_NONE
cliechti89b4af12002-02-12 23:24:41 +000079 elif parity == PARITY_EVEN:
cliechti95c62212002-03-04 22:17:53 +000080 self.jparity = javax.comm.SerialPort.PARITY_EVEN
cliechti89b4af12002-02-12 23:24:41 +000081 elif parity == PARITY_ODD:
cliechti95c62212002-03-04 22:17:53 +000082 self.jparity = javax.comm.SerialPort.PARITY_ODD
cliechti89b4af12002-02-12 23:24:41 +000083 elif parity == PARITY_MARK:
cliechti95c62212002-03-04 22:17:53 +000084 self.jparity = javax.comm.SerialPort.PARITY_MARK
cliechti89b4af12002-02-12 23:24:41 +000085 elif parity == PARITY_SPACE:
cliechti95c62212002-03-04 22:17:53 +000086 self.jparity = javax.comm.SerialPort.PARITY_SPACE
cliechti89b4af12002-02-12 23:24:41 +000087 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
cliechtid4419742002-03-05 00:34:09 +000098 self.sPort.setSerialPortParams(baudrate, self.databits, self.jstopbits, self.jparity)
cliechti89b4af12002-02-12 23:24:41 +000099 self.sPort.setFlowControlMode(jflowin | jflowout)
100
101 self.timeout = timeout
cliechtia9e4e952002-05-26 01:20:22 +0000102 if timeout >= 0:
cliechti89b4af12002-02-12 23:24:41 +0000103 self.sPort.enableReceiveTimeout(timeout*1000)
104 else:
105 self.sPort.disableReceiveTimeout()
106
cliechti89b4af12002-02-12 23:24:41 +0000107 def close(self):
108 if self.sPort:
109 self.instream.close()
110 self.outstream.close()
111 self.sPort.close()
112 self.sPort = None
113
cliechti95c62212002-03-04 22:17:53 +0000114 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
cliechti89b4af12002-02-12 23:24:41 +0000120 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
cliechtia9e4e952002-05-26 01:20:22 +0000130 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
cliechti89b4af12002-02-12 23:24:41 +0000140
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
177if __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