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