def openSerial( port = 0, rate = 19200, tout = 60 ): """Open serial port for SNAP RepRap communications""" global serialPort try: serialPort = serial.Serial( port, rate, timeout = tout ) except: raise _RepRapError("You do not have permissions to use the serial port, try running as root")
def getPacket(ser): bytes = [] # Read in 5 bytes while len(bytes) <= 4: byte = ser.read() if len(byte) > 0: # If this is the sync then clear the byte buffer. if byte == SYNC_BYTE: bytes = [] bytes.append( ord(byte) ) else: raise _SNAPError("Serial timeout") # Read the expected packet length from Header Byte 1 packetLength = _breakHDB1( bytes[HDB1_OFFSET] ) + PAYLOAD_OFFSET + 1 while len(bytes) < packetLength: byte = ser.read() if len(byte) > 0: bytes.append( ord(byte) ) else: raise _SNAPError("Serial timeout") # We have enough bytes, create packet object p = Packet() p._setBytes(bytes) p._decode() p._check() if printIncoming: p.printPacket("Incoming") if p.DAB == localAddress: return p else: raise _RepRapError( "Received packet bound for " + str(p.DAB) )