def connect(self, portName): portIdentifier = CommPortIdentifier.getPortIdentifier(portName) print 'port id:', portIdentifier if (portIdentifier.isCurrentlyOwned()): print "Error: Port is currently in use" else: commPort = portIdentifier.open('SerialReceiver', 5000) if isinstance(commPort, SerialPort): commPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE) commPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE) self.inStr = commPort.getInputStream() #self.outStr = commPort.getOutputStream() #Thread(SerialWriter(outStream))).start() commPort.addEventListener(SerialReader(self.inStr)) commPort.notifyOnDataAvailable(True) else: print "Error: Only serial ports are handled by this example."
def device(portnumber): enum = CommPortIdentifier.getPortIdentifiers() ports = [] while enum.hasMoreElements(): el = enum.nextElement() if el.getPortType() == CommPortIdentifier.PORT_SERIAL: ports.append(el) return ports[portnumber]
def connect (self, portName): portIdentifier = CommPortIdentifier.getPortIdentifier (portName) print 'port id:' , portIdentifier if (portIdentifier.isCurrentlyOwned() ): print "Error: Port is currently in use" else: commPort = portIdentifier.open('SerialReceiver',5000) if isinstance( commPort, SerialPort ): commPort.setSerialPortParams (9600,SerialPort.DATABITS_8,SerialPort.STOPBITS_1,SerialPort.PARITY_NONE) commPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE) self.inStr = commPort.getInputStream() #self.outStr = commPort.getOutputStream() #Thread(SerialWriter(outStream))).start() commPort.addEventListener( SerialReader (self.inStr)) commPort.notifyOnDataAvailable(True) else: print "Error: Only serial ports are handled by this example."
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 to wait forever xonxoff=0, #enable software flow control rtscts=0, #enable RTS/CTS flow control ): if type(port) == type(''): #strings are taken directly portId = CommPortIdentifier.getPortIdentifier(port) else: portId = device(port) #numbers are transformed to a comportid obj self.portstr = portId.getName() try: self.sPort = portId.open("python serial module", 10) except Exception, msg: self.sPort = None raise serialutil.SerialException, "could not open port: %s" % msg