This is a patch for the serial IO routines used by the BSL on posix/unix type systems. I've also made a few changes to error messages to make them more informative, but the main differences are: - Made it a non-fatal error if it can't figure out how to convert an integer to a device file name. On unixlike systems, the normal way to specify a serial port is by its entry in /dev (since there isn't necessarily a fixed or agreed-upon numbering of serial devices). - Changed the baudrate enum code to avoid using a fixed table of possible baud rates. - Changed the openbsd case so that it does the expected thing if you're running on PC hardware (com1 --> /dev/tty00, etc.) - Deleted unused symbols from the module after initialization. - Simplified some of the termios arithmetic slightly.
Wim. Index: __init__.py =================================================================== RCS file: /cvsroot/mspgcc/pybsl/serial/__init__.py,v retrieving revision 1.1 diff -c -r1.1 __init__.py *** __init__.py 4 Mar 2002 22:20:20 -0000 1.1 --- __init__.py 5 Apr 2004 01:16:11 -0000 *************** *** 5,11 **** # (C)2001-2002 Chris Liechti <cliec...@gmx.net> # this is distributed under a free software license, see license.txt ! import sys, os, string VERSION = string.split("$Revision: 1.1 $")[1] #extract CVS version #chose an implementation, depending on os --- 5,11 ---- # (C)2001-2002 Chris Liechti <cliec...@gmx.net> # this is distributed under a free software license, see license.txt ! import os, string VERSION = string.split("$Revision: 1.1 $")[1] #extract CVS version #chose an implementation, depending on os *************** *** 16,21 **** elif os.name == 'java': from serialjava import * else: ! raise "Sorry no implementation for your platform available." #no "mac" implementation. someone want's to write it? i have no access to a mac. --- 16,21 ---- elif os.name == 'java': from serialjava import * else: ! raise "Sorry: no serial implementation for platform '%s'" % os.name #no "mac" implementation. someone want's to write it? i have no access to a mac. Index: serialposix.py =================================================================== RCS file: /cvsroot/mspgcc/pybsl/serial/serialposix.py,v retrieving revision 1.3 diff -c -r1.3 serialposix.py *** serialposix.py 9 Jan 2003 02:38:41 -0000 1.3 --- serialposix.py 5 Apr 2004 01:16:15 -0000 *************** *** 9,15 **** # ftp://ftp.visi.com/users/grante/python/PosixSerial.py # references: http://www.easysw.com/~mike/serial/serial.html ! import sys, os, fcntl, termios, struct, string, select import serialutil VERSION = string.split("$Revision: 1.3 $")[1] #extract CVS version --- 9,15 ---- # ftp://ftp.visi.com/users/grante/python/PosixSerial.py # references: http://www.easysw.com/~mike/serial/serial.html ! import sys, os, fcntl, termios, struct, string, select, re import serialutil VERSION = string.split("$Revision: 1.3 $")[1] #extract CVS version *************** *** 42,48 **** elif plat == 'openbsd3': #BSD (confirmed) def device(port): ! return '/dev/ttyp%d' % port elif plat[:3] == 'bsd' or \ plat[:6] == 'netbsd' or \ --- 42,48 ---- elif plat == 'openbsd3': #BSD (confirmed) def device(port): ! return '/dev/tty%02d' % port elif plat[:3] == 'bsd' or \ plat[:6] == 'netbsd' or \ *************** *** 71,86 **** else: #platform detection has failed... info = "sys.platform = %r\nos.name = %r\nserialposix.py version = %s" % (sys.platform, os.name, VERSION) ! print """send this information to the author of this module: %s also add the device name of the serial port and where the counting starts for the first serial port. e.g. 'first serial port: /dev/ttyS0' ! and with a bit luck you can get this module running... ! """ ! raise Exception, "this module does not run on this platform, sorry." #whats up with "aix", "beos", "sco", .... #they should work, just need to know the device names. --- 71,89 ---- else: #platform detection has failed... info = "sys.platform = %r\nos.name = %r\nserialposix.py version = %s" % (sys.platform, os.name, VERSION) ! def device(port, info=info): ! print """don't know how to number ttys on this system. ! Use an explicit path (eg /dev/ttyS1) or send this information to ! the author of this module: %s also add the device name of the serial port and where the counting starts for the first serial port. e.g. 'first serial port: /dev/ttyS0' ! """ % info ! raise Exception, "can't handle numeric serial ports on this platform" ! del info #whats up with "aix", "beos", "sco", .... #they should work, just need to know the device names. *************** *** 89,104 **** # construct dictionaries for baud rate lookups baudEnumToInt = {} baudIntToEnum = {} ! for rate in (0,50,75,110,134,150,200,300,600,1200,1800,2400,4800,9600, ! 19200,38400,57600,115200,230400,460800,500000,576000,921600, ! 1000000,1152000,1500000,2000000,2500000,3000000,3500000,4000000 ! ): ! try: ! i = eval('TERMIOS.B'+str(rate)) ! baudEnumToInt[i]=rate ! baudIntToEnum[rate] = i ! except: ! pass #load some constants for later use. --- 92,106 ---- # construct dictionaries for baud rate lookups baudEnumToInt = {} baudIntToEnum = {} ! baudEnumPattern = re.compile('^B([0-9]+)$') ! for baudEnumName in dir(TERMIOS): ! match = baudEnumPattern.match(baudEnumName) ! if match: ! rate = int(match.group(1)) ! baudEnumValue = getattr(TERMIOS, baudEnumName) ! baudEnumToInt[baudEnumValue]=rate ! baudIntToEnum[rate] = baudEnumValue ! del baudEnumPattern, baudEnumName, baudEnumValue, rate, match #load some constants for later use. *************** *** 168,177 **** self.lflag = self.lflag & ~(TERMIOS.ICANON|TERMIOS.ECHO|TERMIOS.ECHOE|TERMIOS.ECHOK|TERMIOS.ECHONL| TERMIOS.ECHOCTL|TERMIOS.ECHOKE|TERMIOS.ISIG|TERMIOS.IEXTEN) #|TERMIOS.ECHOPRT self.oflag = self.oflag & ~(TERMIOS.OPOST) if hasattr(TERMIOS, 'IUCLC'): ! self.iflag = self.iflag & ~(TERMIOS.INLCR|TERMIOS.IGNCR|TERMIOS.ICRNL|TERMIOS.IUCLC|TERMIOS.IGNBRK) ! else: ! self.iflag = self.iflag & ~(TERMIOS.INLCR|TERMIOS.IGNCR|TERMIOS.ICRNL|TERMIOS.IGNBRK) #setup baudrate try: self.ispeed = self.ospeed = baudIntToEnum[baudrate] --- 170,178 ---- self.lflag = self.lflag & ~(TERMIOS.ICANON|TERMIOS.ECHO|TERMIOS.ECHOE|TERMIOS.ECHOK|TERMIOS.ECHONL| TERMIOS.ECHOCTL|TERMIOS.ECHOKE|TERMIOS.ISIG|TERMIOS.IEXTEN) #|TERMIOS.ECHOPRT self.oflag = self.oflag & ~(TERMIOS.OPOST) + self.iflag = self.iflag & ~(TERMIOS.INLCR|TERMIOS.IGNCR|TERMIOS.ICRNL|TERMIOS.IGNBRK) if hasattr(TERMIOS, 'IUCLC'): ! self.iflag = self.iflag & ~(TERMIOS.IUCLC) #setup baudrate try: self.ispeed = self.ospeed = baudIntToEnum[baudrate] *************** *** 390,392 **** --- 391,398 ---- print repr(s.read(5)) print s.inWaiting() del s + + # remove unused references from the module + del sys, string, re + del plat +