I did get things mostly working. I've attached what I have done. I had problems with losing data at higher baud rates, so I buffered data in read(). If this is not the desired behavior it is easy to change (just look in the source). You also need ctypes installed.
Good Luck, Ben
#some minor manual additions INVALID_HANDLE_VALUE = -1 MAXDWORD = 4294967295 # Generated by h2py from /home/bdm/Desktop/winbase.h VS_VERSION_INFO = 1 VS_USER_DEFINED = 100 VS_FFI_SIGNATURE = (-17890115) VS_FFI_STRUCVERSION = 0x00010000L VS_FFI_FILEFLAGSMASK = 0x0000003FL VS_FF_DEBUG = 0x00000001L VS_FF_PRERELEASE = 0x00000002L VS_FF_PATCHED = 0x00000004L VS_FF_PRIVATEBUILD = 0x00000008L VS_FF_INFOINFERRED = 0x00000010L VS_FF_SPECIALBUILD = 0x00000020L VOS_UNKNOWN = 0x00000000L VOS_DOS = 0x00010000L VOS_OS216 = 0x00020000L VOS_OS232 = 0x00030000L VOS_NT = 0x00040000L VOS_WINDOWSCE = 0x00050000L VOS__BASE = 0x00000000L VOS__WINDOWS16 = 0x00000001L VOS__PM16 = 0x00000002L VOS__PM32 = 0x00000003L VOS__WINDOWS32 = 0x00000004L VOS_DOS_WINDOWS16 = 0x00010001L VOS_DOS_WINDOWS32 = 0x00010004L VOS_OS216_PM16 = 0x00020002L VOS_OS232_PM32 = 0x00030003L VOS_NT_WINDOWS32 = 0x00040004L VOS_CE_WINDOWS32 = 0x00050004L VFT_UNKNOWN = 0x00000000L VFT_APP = 0x00000001L VFT_DLL = 0x00000002L VFT_DRV = 0x00000003L VFT_FONT = 0x00000004L VFT_VXD = 0x00000005L VFT_STATIC_LIB = 0x00000007L VFT2_UNKNOWN = 0x00000000L VFT2_DRV_PRINTER = 0x00000001L VFT2_DRV_KEYBOARD = 0x00000002L VFT2_DRV_LANGUAGE = 0x00000003L VFT2_DRV_DISPLAY = 0x00000004L VFT2_DRV_MOUSE = 0x00000005L VFT2_DRV_NETWORK = 0x00000006L VFT2_DRV_SYSTEM = 0x00000007L VFT2_DRV_INSTALLABLE = 0x00000008L VFT2_DRV_SOUND = 0x00000009L VFT2_DRV_COMM = 0x0000000AL VFT2_DRV_INPUTMETHOD = 0x0000000BL VFT2_FONT_RASTER = 0x00000001L VFT2_FONT_VECTOR = 0x00000002L VFT2_FONT_TRUETYPE = 0x00000003L VFFF_ISSHAREDFILE = 0x0001 VFF_CURNEDEST = 0x0001 VFF_FILEINUSE = 0x0002 VFF_BUFFTOOSMALL = 0x0004 VIFF_FORCEINSTALL = 0x0001 VIFF_DONTDELETEOLD = 0x0002 VIF_TEMPFILE = 0x00000001L VIF_MISMATCH = 0x00000002L VIF_SRCOLD = 0x00000004L VIF_DIFFLANG = 0x00000008L VIF_DIFFCODEPG = 0x00000010L VIF_DIFFTYPE = 0x00000020L VIF_WRITEPROT = 0x00000040L VIF_FILEINUSE = 0x00000080L VIF_OUTOFSPACE = 0x00000100L VIF_ACCESSVIOLATION = 0x00000200L VIF_SHARINGVIOLATION = 0x00000400L VIF_CANNOTCREATE = 0x00000800L VIF_CANNOTDELETE = 0x00001000L VIF_CANNOTRENAME = 0x00002000L VIF_CANNOTDELETECUR = 0x00004000L VIF_OUTOFMEMORY = 0x00008000L VIF_CANNOTREADSRC = 0x00010000L VIF_CANNOTREADDST = 0x00020000L VIF_BUFFTOOSMALL = 0x00040000L FILE_BEGIN = 0 FILE_CURRENT = 1 FILE_END = 2 MAXINTATOM = 0xC000 LMEM_FIXED = 0x0000 LMEM_MOVEABLE = 0x0002 LMEM_NOCOMPACT = 0x0010 LMEM_NODISCARD = 0x0020 LMEM_ZEROINIT = 0x0040 LMEM_MODIFY = 0x0080 LMEM_DISCARDABLE = 0x0F00 LMEM_VALID_FLAGS = 0x0F72 LMEM_INVALID_HANDLE = 0x8000 LMEM_DDESHARE = 0x0000 LMEM_LOWER = 0x0000 LMEM_NOT_BANKED = 0x0000 LMEM_NOTIFY = 0x0000 LMEM_SHARE = 0x0000 LHND = (LMEM_MOVEABLE | LMEM_ZEROINIT) LPTR = (LMEM_FIXED | LMEM_ZEROINIT) NONZEROLHND = (LMEM_MOVEABLE) NONZEROLPTR = (LMEM_FIXED) LMEM_DISCARDED = 0x4000 LMEM_LOCKCOUNT = 0x00FF GMEM_FIXED = LMEM_FIXED GMEM_MOVEABLE = LMEM_MOVEABLE GPTR = LPTR GHND = LHND GMEM_DDESHARE = LMEM_DDESHARE GMEM_DISCARDABLE = LMEM_DISCARDABLE GMEM_LOWER = LMEM_LOWER GMEM_NOCOMPACT = LMEM_NOCOMPACT GMEM_NODISCARD = LMEM_NODISCARD GMEM_NOT_BANKED = LMEM_NOT_BANKED GMEM_NOTIFY = LMEM_NOTIFY GMEM_SHARE = LMEM_SHARE GMEM_ZEROINIT = LMEM_ZEROINIT VER_PLATFORM_WIN32s = 0 VER_PLATFORM_WIN32_WINDOWS = 1 VER_PLATFORM_WIN32_NT = 2 VER_PLATFORM_WIN32_HH = 3 VER_PLATFORM_WIN32_CE = 3 DONT_RESOLVE_DLL_REFERENCES = 0x00000001 LOAD_LIBRARY_AS_DATAFILE = 0x00000002 LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008 MSGQUEUE_NOPRECOMMIT = 0x00000001 MSGQUEUE_ALLOW_BROKEN = 0x00000002 MSGQUEUE_MSGALERT = 0x00000001 DEBUG_PROCESS = 0x00000001 DEBUG_ONLY_THIS_PROCESS = 0x00000002 CREATE_SUSPENDED = 0x00000004 CREATE_NEW_CONSOLE = 0x00000010 STACK_SIZE_PARAM_IS_A_RESERVATION = 0x00010000 INHERIT_CALLER_PRIORITY = 0x00020000 THREAD_PRIORITY_TIME_CRITICAL = 0 THREAD_PRIORITY_HIGHEST = 1 THREAD_PRIORITY_ABOVE_NORMAL = 2 THREAD_PRIORITY_NORMAL = 3 THREAD_PRIORITY_BELOW_NORMAL = 4 THREAD_PRIORITY_LOWEST = 5 THREAD_PRIORITY_ABOVE_IDLE = 6 THREAD_PRIORITY_IDLE = 7 EXCEPTION_DEBUG_EVENT = 1 CREATE_THREAD_DEBUG_EVENT = 2 CREATE_PROCESS_DEBUG_EVENT = 3 EXIT_THREAD_DEBUG_EVENT = 4 EXIT_PROCESS_DEBUG_EVENT = 5 LOAD_DLL_DEBUG_EVENT = 6 UNLOAD_DLL_DEBUG_EVENT = 7 OUTPUT_DEBUG_STRING_EVENT = 8 RIP_EVENT = 9 WAIT_OBJECT_0 = 0x00000000L WAIT_ABANDONED = 0x00000080L WAIT_ABANDONED_0 = 0x00000080L WAIT_FAILED = (-1) INFINITE = (-1) TLS_MINIMUM_AVAILABLE = 64 STILL_ACTIVE = 0x00000103 GENERIC_READ = ((-2147483648)) GENERIC_WRITE = (0x40000000L) GENERIC_EXECUTE = (0x20000000L) GENERIC_ALL = (0x10000000L) FILE_SHARE_READ = 0x00000001 FILE_SHARE_WRITE = 0x00000002 FILE_FLAG_WRITE_THROUGH = (-2147483648) FILE_FLAG_OVERLAPPED = 0x40000000 FILE_FLAG_NO_BUFFERING = 0x20000000 FILE_FLAG_RANDOM_ACCESS = 0x10000000 FILE_FLAG_SEQUENTIAL_SCAN = 0x08000000 FILE_FLAG_DELETE_ON_CLOSE = 0x04000000 FILE_FLAG_BACKUP_SEMANTICS = 0x02000000 FILE_FLAG_POSIX_SEMANTICS = 0x01000000 CREATE_NEW = 1 CREATE_ALWAYS = 2 OPEN_EXISTING = 3 OPEN_ALWAYS = 4 TRUNCATE_EXISTING = 5 OPEN_FOR_LOADER = 6 FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x00000100 FORMAT_MESSAGE_IGNORE_INSERTS = 0x00000200 FORMAT_MESSAGE_FROM_STRING = 0x00000400 FORMAT_MESSAGE_FROM_HMODULE = 0x00000800 FORMAT_MESSAGE_FROM_SYSTEM = 0x00001000 FORMAT_MESSAGE_ARGUMENT_ARRAY = 0x00002000 FORMAT_MESSAGE_MAX_WIDTH_MASK = 0x000000FF FIND_FIRST_EX_CASE_SENSITIVE = 0x00000001 DTR_CONTROL_DISABLE = 0x00 DTR_CONTROL_ENABLE = 0x01 DTR_CONTROL_HANDSHAKE = 0x02 RTS_CONTROL_DISABLE = 0x00 RTS_CONTROL_ENABLE = 0x01 RTS_CONTROL_HANDSHAKE = 0x02 RTS_CONTROL_TOGGLE = 0x03 NOPARITY = 0 ODDPARITY = 1 EVENPARITY = 2 MARKPARITY = 3 SPACEPARITY = 4 ONESTOPBIT = 0 ONE5STOPBITS = 1 TWOSTOPBITS = 2 IGNORE = 0 CBR_110 = 110 CBR_300 = 300 CBR_600 = 600 CBR_1200 = 1200 CBR_2400 = 2400 CBR_4800 = 4800 CBR_9600 = 9600 CBR_14400 = 14400 CBR_19200 = 19200 CBR_38400 = 38400 CBR_56000 = 56000 CBR_57600 = 57600 CBR_115200 = 115200 CBR_128000 = 128000 CBR_256000 = 256000 CE_RXOVER = 0x0001 CE_OVERRUN = 0x0002 CE_RXPARITY = 0x0004 CE_FRAME = 0x0008 CE_BREAK = 0x0010 CE_TXFULL = 0x0100 CE_PTO = 0x0200 CE_IOE = 0x0400 CE_DNS = 0x0800 CE_OOP = 0x1000 CE_MODE = 0x8000 IE_BADID = (-1) IE_OPEN = (-2) IE_NOPEN = (-3) IE_MEMORY = (-4) IE_DEFAULT = (-5) IE_HARDWARE = (-10) IE_BYTESIZE = (-11) IE_BAUDRATE = (-12) EV_RXCHAR = 0x0001 EV_RXFLAG = 0x0002 EV_TXEMPTY = 0x0004 EV_CTS = 0x0008 EV_DSR = 0x0010 EV_RLSD = 0x0020 EV_BREAK = 0x0040 EV_ERR = 0x0080 EV_RING = 0x0100 EV_PERR = 0x0200 EV_RX80FULL = 0x0400 EV_EVENT1 = 0x0800 EV_EVENT2 = 0x1000 EV_POWER = 0x2000 SETXOFF = 1 SETXON = 2 SETRTS = 3 CLRRTS = 4 SETDTR = 5 CLRDTR = 6 SETBREAK = 8 CLRBREAK = 9 SETIR = 10 CLRIR = 11 PURGE_TXABORT = 0x0001 PURGE_RXABORT = 0x0002 PURGE_TXCLEAR = 0x0004 PURGE_RXCLEAR = 0x0008 LPTx = 0x80 AC_LINE_OFFLINE = 0x00 AC_LINE_ONLINE = 0x01 AC_LINE_BACKUP_POWER = 0x02 AC_LINE_UNKNOWN = 0xFF BATTERY_FLAG_HIGH = 0x01 BATTERY_FLAG_LOW = 0x02 BATTERY_FLAG_CRITICAL = 0x04 BATTERY_FLAG_CHARGING = 0x08 BATTERY_FLAG_NO_BATTERY = 0x80 BATTERY_FLAG_UNKNOWN = 0xFF BATTERY_PERCENTAGE_UNKNOWN = 0xFF BATTERY_LIFE_UNKNOWN = (-1) BATTERY_CHEMISTRY_ALKALINE = 0x01 BATTERY_CHEMISTRY_NICD = 0x02 BATTERY_CHEMISTRY_NIMH = 0x03 BATTERY_CHEMISTRY_LION = 0x04 BATTERY_CHEMISTRY_LIPOLY = 0x05 BATTERY_CHEMISTRY_ZINCAIR = 0x06 BATTERY_CHEMISTRY_UNKNOWN = 0xFF LOADFSD_ASYNCH = 0x0000 LOADFSD_SYNCH = 0x0001 RT_MUI = 222 ID_MUI = 1
"""Provide serial port access to Windows CE devices. Copyright: Ben McBride benjamin.mcbrideATgmail.com """ import sys import winbase from ctypes import * #----------------------------------------------------------- # CONSTANTS #----------------------------------------------------------- PARITY_NONE = winbase.NOPARITY PARITY_EVEN = winbase.EVENPARITY PARITY_ODD = winbase.ODDPARITY STOPBITS_ONE = winbase.ONESTOPBIT STOPBITS_TWO = winbase.TWOSTOPBITS FIVEBITS = 5 SIXBITS = 6 SEVENBITS = 7 EIGHTBITS = 8 XON = chr(17) XOFF = chr(19) #----------------------------------------------------------- # Helper functions #----------------------------------------------------------- def bitmask(b): """Return a mask for setting a bit""" return (1 << b) def bitset(value, b): """Set the specified bit of value""" return (value | bitmask(b)) def bitclear(value, b): """Clear the specified bit of value""" return (value & ~(bitmask(b))) def bittest(value, b): """Return True if the bit of value is 1, False otherwise""" return 1 == ((value >> 1) & 1) #----------------------------------------------------------- # Windows Comm Structures #----------------------------------------------------------- class DCB(Structure): """Device Control Block structure""" _fields_ = [ ("DCBlength", c_uint), ("BaudRate", c_uint), ("flags", c_uint), # codervw:bdm :1 ("fBinary", c_uint), # codervw:bdm :1 ("fParity", c_uint), # codervw:bdm :1 ("fOutxCtsFlow", c_uint), # codervw:bdm :1 ("fOutxDsrFlow", c_uint), # codervw:bdm :2 ("fDtrControl", c_uint), # codervw:bdm :1 ("fDsrSensitivity", c_uint), # codervw:bdm :1 ("fTXContinueOnXoff", c_uint), # codervw:bdm :1 ("fOutX", c_uint), # codervw:bdm :1 ("fInX", c_uint), # codervw:bdm :1 ("fErrorChar", c_uint), # codervw:bdm :1 ("fNull", c_uint), # codervw:bdm :2 ("fRtsControl", c_uint), # codervw:bdm :1 ("fAbortOnError", c_uint), # codervw:bdm :17("fDummy2", c_uint), ("wReserved", c_ushort), ("XonLim", c_ushort), ("XoffLim", c_ushort), ("ByteSize", c_byte), ("Parity", c_byte), ("StopBits", c_byte), ("XonChar", c_char), ("XoffChar", c_char), ("ErrorChar", c_char), ("EofChar", c_char), ("EvtChar", c_char), ("wReserved1", c_ushort) ] class COMMTIMEOUTS(Structure): """Comm timeout structure""" _fields_ = [ ("ReadIntervalTimeout", c_uint), ("ReadTotalTimeoutMultiplier", c_uint), ("ReadTotalTimeoutConstant", c_uint), ("WriteTotalTimeoutMultiplier", c_uint), ("WriteTotalTimeoutConstant", c_uint) ] class COMSTAT(Structure): """Communication device data""" _fields_ = [ ("flags", c_uint), ("cbInQue", c_uint), ("cbOutQue", c_uint) ] #----------------------------------------------------------- # Classes #----------------------------------------------------------- class SerialException(Exception): pass class Serial(object): """Serial port class""" _baudrates = { 110 : winbase.CBR_110, 300 : winbase.CBR_300, 600 : winbase.CBR_600, 1200 : winbase.CBR_1200, 2400 : winbase.CBR_2400, 4800 : winbase.CBR_4800, 9600 : winbase.CBR_9600, 14400 : winbase.CBR_14400, 19200 : winbase.CBR_19200, 38400 : winbase.CBR_38400, 56000 : winbase.CBR_56000, 57600 : winbase.CBR_57600, 115200 : winbase.CBR_115200, 128000 : winbase.CBR_128000, 256000 : winbase.CBR_256000 } def __init__(self, port='COM1:', baudrate=9600, bytesize=8, stopbits=STOPBITS_ONE, parity=PARITY_NONE, timeout=5, xonxoff=0, rtscts=False ): self.__handle = winbase.INVALID_HANDLE_VALUE self._port = unicode(port) #CreateFileW requires wide character data self._baudrate = self._baudrates[baudrate] self._bytesize = bytesize self._stopbits = stopbits self._parity = parity self._timeout = timeout self._xonxoff = xonxoff self._rtscts = rtscts self.buffer = create_string_buffer(2048) self._s = '' def open(self): """Try to open the serial port. Raises a SerialException exception if there are problems. """ #check to see if the port is already open if self.isOpen(): return #get file handle for the COMM port self.__handle = windll.coredll.CreateFileW( self._port, winbase.GENERIC_READ | winbase.GENERIC_WRITE, None, None, winbase.OPEN_ALWAYS, None, None ) if self.__handle == winbase.INVALID_HANDLE_VALUE: raise SerialException, windll.coredll.GetLastError() #--------------------------------------------------- # set COMM port attributes #--------------------------------------------------- #set input and output buffer sizes if not(windll.coredll.SetupComm(self.__handle, 2048, 2048)): raise SerialException, windll.coredll.GetLastError() try: self._configurePort() except SerialException, e: self.close() #try to close the already open port raise SerialException, e #--------------------------------------------------- # purge the COMM port #--------------------------------------------------- if not(windll.coredll.PurgeComm(self.__handle)): raise SerialException, windll.coredll.GetLastError() def close(self): """Close the serial port. Raises a SerialException exception if there are problems. """ if not(self.isOpen()): return if not(windll.coredll.CloseHandle(self.__handle)): raise SerialException, windll.coredll.GetLastError() self.__handle = winbase.INVALID_HANDLE_VALUE def read(self, size=1): """Read bytes from the serial port. The port needs to be opened before this function is called. Raises a SerialException exception if there are problems. """ return self.__buffered_read(size) def __unbuffered_read(self, size=1): """Unbuffered read.""" data = '' nmbr_of_bytes_to_read = c_uint(size) nmbr_of_bytes_read = c_uint() #read bytes if nmbr_of_bytes_to_read > 0: if not(windll.coredll.ReadFile( self.__handle, self.buffer, nmbr_of_bytes_to_read, byref(nmbr_of_bytes_read), None )): raise SerialException, windll.coredll.GetLastError() data = self.buffer.raw[0:nmbr_of_bytes_read.value] return data def __buffered_read(self, size=1): """NOTE: This method can replace the read() method above, if data is being lost. This method will pull all the data that is in the serial port queue automatically and buffer it. """ #read data in queue inQueue = self.inWaiting() data = '' #determine the number of bytes to read if (len(self._s) + inQueue) < size: nmbr_of_bytes_to_read = c_uint(size - len(self._s)) else: nmbr_of_bytes_to_read = c_uint(inQueue) nmbr_of_bytes_read = c_uint() #read bytes if nmbr_of_bytes_to_read != 0: if not(windll.coredll.ReadFile( self.__handle, self.buffer, nmbr_of_bytes_to_read, byref(nmbr_of_bytes_read), None )): raise SerialException, windll.coredll.GetLastError() self._s += self.buffer.raw[0:nmbr_of_bytes_read.value] #return only the request number of bytes if len(self._s) < size: #not enough bytes read before the timeout size = len(self._s) data = self._s[0:size] self._s = self._s[size:] return data def write(self, data): """Write data to the serial port. The port needs to be opened before this function is called. Raises a SerialException exception if there are problems. """ nmbr_of_bytes = c_uint(len(data)) bytes_written = c_uint() success = windll.coredll.WriteFile( self.__handle, data, nmbr_of_bytes, byref(bytes_written), None ) if not(success): raise SerialException, windll.coredll.GetLastError() def isOpen(self): """Return True if the serial port is open""" return (self.__handle != winbase.INVALID_HANDLE_VALUE) def inWaiting(self): """Return the number of characters in the received queue""" comstats, err = self.getCommStats() return comstats.cbInQue def flushInput(self): """Flush input buffer, discarding contents""" flags = winbase.PURGE_RXABORT | winbase.PURGE_RXCLEAR if not(windll.coredll.PurgeComm(self.__handle, flags)): raise SerialException, windll.coredll.GetLastError() def flushOutput(self): """Flush output buffer, abort output""" flags = winbase.PURGE_TXABORT | winbase.PURGE_TXCLEAR if not(windll.coredll.PurgeComm(self.__handle, flags)): raise SerialException, windll.coredll.GetLastError() #------------------------------------------------------- # Non PySerial methods #------------------------------------------------------- def getDCB(self): """Get the DCB structure""" dcb = DCB() if not(windll.coredll.GetCommState(self.__handle, byref(dcb))): raise SerialException, windll.coredll.GetLastError() return dcb def getCommTimeouts(self): """Get the comm timeouts.""" timeouts = COMMTIMEOUTS() if not(windll.coredll.GetCommTimeouts(self.__handle, byref(timeouts))): raise SerialException, windll.coredll.GetLastError() return timeouts def getCommStats(self): """Get the comm stats function as well as error flags. NOTE: The error flags might show a CE_RXOVER error if data is received after an EOF character. """ comstats = COMSTAT() errors = c_uint() if not(windll.coredll.ClearCommError( self.__handle, byref(errors), byref(comstats), )): raise SerialException, windll.coredll.GetLastError() return comstats, errors.value def _configurePort(self): """Configure the port properties and set timeouts.""" #set comm port timeout timeouts = self.getCommTimeouts() if self._timeout is None: timeouts.ReadIntervalTimeout = 0 timeouts.ReadTotalTimeoutMultiplier = 0 timeouts.ReadTotalTimeoutConstant = 0 elif self._timeout == 0: timeouts.ReadIntervalTimeout = winbase.MAXDWORD timeouts.ReadTotalTimeoutMultiplier = 0 timeouts.ReadTotalTimeoutConstant = 0 else: timeouts.ReadIntervalTimeout = 0 timeouts.ReadTotalTimeoutMultiplier = 0 timeouts.ReadTotalTimeoutConstant = int(self._timeout*1000) timeouts.WriteTotalTimeoutMultiplier = 0 timeouts.WriteTotalTimeoutConstant = 0 if not(windll.coredll.SetCommTimeouts(self.__handle, byref(timeouts))): raise SerialException, windll.coredll.GetLastError() #get current port attributes dcb = self.getDCB() #set attributes dcb.BaudRate = self._baudrate dcb.ByteSize = self._bytesize dcb.StopBits = self._stopbits #these fields need flags (in the bitfield) to be set dcb.Parity = self._parity if self._parity == PARITY_NONE: dcb.flags = bitclear(dcb.flags, 1) else: dcb.flags = bitset(dcb.flags, 1) if self._xonxoff: dcb.flags = bitset(dcb.flags, 8) dcb.flags = bitset(dcb.flags, 9) dcb.XonChar = XON dcb.XoffChar = XOFF else: dcb.flags = bitclear(dcb.flags, 8) dcb.flags = bitclear(dcb.flags, 9) if self._rtscts: #set fRTSControl to winbase.RTS_CONTROL_HANDSHAKE dcb.flags = bitclear(dcb.flags, 12) dcb.flags = bitset(dcb.flags, 13) dcb.flags = bitset(dcb.flags, 2) else: dcb.flags = bitclear(dcb.flags, 2) #set new attributes if not(windll.coredll.SetCommState(self.__handle, byref(dcb))): raise SerialException, windll.coredll.GetLastError() #------------------------------------------------------- # Property methods #------------------------------------------------------- #BDM TODO -- what happens when you set the port? PySerial changes on the fly def _getPort(self): return self._port def _setPort(self, p): self._port = unicode(p) port = property(_getPort, _setPort) def _getBaudrate(self): return self._baudrate def setBaudrate(self, baudrate): """Named this on purpose because PySerial makes this function public, the same thing can be accomplished through setting the baudrate attribute though. """ self._baudrate = self._baudrates[baudrate] if self.isOpen(): self._configurePort() baudrate = property(_getBaudrate, setBaudrate) def _getBytesize(self): return self._bytesize def _setBytesize(self, size): self._bytesize = size if self.isOpen(): self._configurePort() bytesize = property(_getBytesize, _setBytesize) def _getParity(self): return self._parity def _setParity(self, parity): self._parity = parity if self.isOpen(): self._configurePort() parity = property(_getParity, _setParity) def _getStopbits(self): return self._stopbits def _setStopbits(self, stopbits): self._stopbits = stopbits if self.isOpen(): self._configurePort() stopbits = property(_getStopbits, _setStopbits) def _getTimeout(self): return self._timeout def _setTimeout(self, t): if self._timeout != t: self._timeout = t if self.isOpen(): self._configurePort() timeout = property(_getTimeout, _setTimeout) def _getXonXoff(self): return self._xonxoff def _setXonXoff(self, x): self._xonxoff = x if self.isOpen(): self._configurePort() xonxoff = property(_getXonXoff, _setXonXoff) def _getRtsCts(self): return self._rtscts def _setRtsCts(self, rtscts): self._rtscts = rtscts if self.isOpen(): self._configurePort() rtscts = property(_getRtsCts, _setRtsCts) #------------------------------------------------------- # Read only attributes #------------------------------------------------------- def _getPortstr(self): return str(self.port) portstr = property(_getPortstr) def _getBaudrates(self): return Serial._baudrates.keys() BAUDRATES = property(_getBaudrates) def _getParities(self): return [PARITY_NONE, PARITY_EVEN, PARITY_ODD] PARITIES = property(_getParities) def _getBytesizes(self): return [FIVEBITS, SIXBITS, SEVENBITS, EIGHTBITS ] BYTESIZES = property(_getBytesizes) def _getStopbits(self): return [STOPBITS_ONE, STOPBITS_TWO] STOPBITS = property(_getStopbits) #------------------------------------------------------- # XXX these functions are not implemented, # but are defined by pySerial #------------------------------------------------------- def sendBreak(self): raise NotImplementedError def setRTS(self, level=1): raise NotImplementedError def setDTR(self, level=1): raise NotImplementedError def getCTS(self): raise NotImplementedError def getDSR(self): raise NotImplementedError def getRI(self): raise NotImplementedError def getCD(self): raise NotImplementedError
_______________________________________________ PythonCE mailing list PythonCE@python.org http://mail.python.org/mailman/listinfo/pythonce