[POS-commit] CVS: FiscalPrinter/lib PosixSerial.py,NONE,1.1.2.1 __init__.py,NONE,1.1.2.1

Bruno Trevisan bt at async.com.br
Fri Feb 14 23:18:41 BRST 2003


Update of /usr/local/cvssrc/FiscalPrinter/lib
In directory anthem:/tmp/cvs-serv26252/lib

Added Files:
      Tag: FISCALPRINTER_0_10
	PosixSerial.py __init__.py 
Log Message:
Adding support for xmlrpc <jdahlin> <bt>
Bematech MP20FI-II support <kiko> <mario>
File Priter revised <mario> <jdahlin> <bt>
New configuration <bt>
Striping out graphical interface 
python based control script (jdahlin>
modified some printing methods names <kiko>


--- NEW FILE: PosixSerial.py ---
# Posix serial port class
# Copyright 2001, Grant B. Edwards  <grante@visi.com>

# You may use this code in any way you like so long as you leave this
# copyright notice.

# Though you are not required to, it would be nice if you send
# me a copy of bufixes or enhancements and allowed me to incorporate
# and distribute them.

import sys
import fcntl
import FCNTL
import termios
import os
import struct

if (sys.hexversion < 0x020100f0):
    import TERMIOS
else:
    TERMIOS = termios

isLinux = sys.platform == 'linux-i386' or sys.platform == 'linux2'

# 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

# Do not know if theseb are right for anything except Linux
if isLinux:
    TIOCMGET = 0x5415
    TIOCMBIS = 0x5416
    TIOCMBIC = 0x5417
    TIOCMSET = 0x5418
    
    TIOCM_LE   =  0x001
    TIOCM_DTR  =  0x002
    TIOCM_RTS  =  0x004
    TIOCM_ST   =  0x008
    TIOCM_SR   =  0x010
    TIOCM_CTS  =  0x020
    TIOCM_CAR  =  0x040
    TIOCM_RNG  =  0x080
    TIOCM_DSR  =  0x100
    TIOCM_CD   =  TIOCM_CAR
    TIOCM_RI   =  TIOCM_RNG
    TIOCM_OUT1 =  0x2000
    TIOCM_OUT2 =  0x4000
    
    TIOCM_zero_str = struct.pack('I',0)
    TIOCM_RTS_str = struct.pack('I',TIOCM_RTS)
    TIOCM_DTR_str = struct.pack('I',TIOCM_DTR)

portNotOpenError = ValueError('port not open')

class Port:
    """ An object wrapper for Posix serial ports"""

    def __init__(self,path=None):
        self.fd = None
        if path:
            self.open(path)

    def __tcsetattr(self):
        termios.tcsetattr(self.fd,TERMIOS.TCSANOW,[self.iflag,self.oflag,self.cflag,self.lflag,self.ispeed,self.ospeed,self.cc])

    def __tcgetattr(self):
        self.iflag,self.oflag,self.cflag,self.lflag,self.ispeed,self.ospeed,self.cc = termios.tcgetattr(self.fd)

    def open(self,path):
        if self.fd:
            self.close()
        self.fd = os.open(path,os.O_RDWR)
        self.__tcgetattr()
        self.cook(0)
        self.echo(0)

    def close(self):
        if self.fd:
            os.close(self.fd)
            self.fd = None

    def _write(self,data):
        if not self.fd: raise portNotOpenError
        return os.write(self.fd,data)

    def write(self,data):
        if not self.fd: raise portNotOpenError
        t = len(data)
        d = data
        while t>0:
            n = os.write(self.fd,d)
            d = d[n:]
            t = t - n

    def read(self,size=1024):
        if not self.fd: raise portNotOpenError
        return os.read(self.fd,size)

    def baud(self,rate=None):
        if not self.fd: raise portNotOpenError

        if not (rate is None):
            try:
                b = baudIntToEnum[rate]
            except:
                raise ValueError,'invalid baud rate: '+str(rate)
            self.ispeed = b
            self.ospeed = b
            self.__tcsetattr()
        return baudEnumToInt[self.ispeed]

    def charLen(self,clen=None):
        if not self.fd: raise portNotOpenError
        if not clen is None:
            self.cflag = self.cflag & ~TERMIOS.CSIZE
            if clen == 8:
                self.cflag = self.cflag | TERMIOS.CS8
            elif clen == 7:
                self.cflag = self.cflag | TERMIOS.CS7
            elif clen == 6:
                self.cflag = self.cflag | TERMIOS.CS6
            elif clen == 5:
                self.cflag = self.cflag | TERMIOS.CS5
            else:
                raise ValueError,'invalid char len: '+str(clen)
            self.__tcsetattr()
        cs = self.cflag & TERMIOS.CSIZE
        if   cs == TERMIOS.CS8: 
            return 8
        elif cs == TERMIOS.CS7: 
            return 7
        elif cs == TERMIOS.CS6: 
            return 6
        elif cs == TERMIOS.CS5: 
            return 5
        else: 
            raise ValueError,'char len invalid'
            
    def parity(self,par=None):
        if not self.fd: raise portNotOpenError

        if not par is None:
            self.cflag = self.cflag & ~(TERMIOS.PARENB|TERMIOS.PARODD)
            if par == 'none':
                pass
            elif par == 'even':
                self.cflag = self.cflag | (TERMIOS.PARENB)
            elif par == 'odd':
                self.cflag = self.cflag | (TERMIOS.PARENB|TERMIOS.PARODD)
            else:
                raise ValueError,'invalid parity: '+str(par)
            self.__tcsetattr()
        if (self.cflag & TERMIOS.PARENB) == 0:
            return 'none'
        elif self.cflag & TERMIOS.PARODD:
            return 'odd'
        else:
            return 'even'

    def xonxoff(self,enable=None):
        if not self.fd: raise portNotOpenError
        if not enable is None:
            if enable:
                self.iflag = self.iflag | (TERMIO.IXON|TERMIO.IXOFF)
            else:
                self.iflag = self.iflag & ~(TERMIO.IXON|TERMIO.IXOFF)
            self.__tcsetattr()
        return (self.iflag & (TERMIO.IXON|TERMIO.IXOFF)) == (TERMIO.IXON|TERMIO.IXOFF)

    def rtscts(self,enable=None):
        if not self.fd: raise portNotOpenError
        if not enable is None:
            if enable:
                self.cflag = self.cflag | TERMIO.RTSCTS
            else:
                self.cflag = self.cflag & ~TERMIO.RTSCTS
            self.__tcsetattr()
        return (self.cflag & TERMIO.RTSCTS) != 0

    def cook(self,enable=None):
        if not self.fd: raise portNotOpenError
        if not enable is None:
            self.iflag = 0
            self.oflag = 0
            if enable:
                self.lflag = self.lflag | TERMIOS.ICANON
            else:
                self.lflag = self.lflag & ~TERMIOS.ICANON
            self.__tcsetattr()
        return (self.lflag & TERMIOS.ICANON) != 0

    def echo(self,enable=None):
        if not self.fd:
            raise portNotOpenError
        if not enable is None:
            self.iflag = 0
            self.oflag = 0
            if enable:
                self.lflag = self.lflag | TERMIOS.ECHO
            else:
                self.lflag = self.lflag & ~TERMIOS.ECHO
            self.__tcsetattr()
        return (self.lflag & TERMIOS.ECHO) != 0

    def flushInput(self):
        if not self.fd:
            raise portNotOpenError
        termios.tcflush(self.fd,TERMIOS.TCIFLUSH)

    def flushOutput(self):
        if not self.fd:
            raise portNotOpenError
        termios.tcflush(self.fd,TERMIOS.TCOFLUSH)

    def sendBreak(self):
        if not self.fd:
            raise portNotOpenError
        termios.tcsendbreak(self.fd,0)

    def drainOutput(self):
        if not self.fd: raise portNotOpenError
        termios.tcdrain(self.fd)

    def vmin(self,vm=None):
        if not self.fd: raise portNotOpenError
        if not vm is None:
            if vm<0 or vm>255:
                raise ValueError,'invalid vmin: '+str(vm)
            self.cc[TERMIOS.VMIN] = vm
            self.__tcsetattr()
        return self.cc[TERMIOS.VMIN]

    def vtime(self,vt=None):
        if not self.fd: raise portNotOpenError
        if not vt is None:
            if vt<0 or vt>255:
                raise ValueError,'invalid vtime: '+str(vt)
            self.cc[TERMIOS.VTIME] = vt
            self.__tcsetattr()
        return self.cc[TERMIOS.VTIME]

    def nonblocking(self):
        if not self.fd:
            raise portNotOpenError
        fcntl.fcntl(self.fd,FCNTL.F_SETFL,FCNTL.O_NONBLOCK)

    if isLinux:

        def DSR(self):
            if not self.fd: raise portNotOpenError
            s = fcntl.ioctl(self.fd,TIOCMGET,TIOCM_zero_str)
            return struct.unpack('I',s)[0] & TIOCM_DSR

        def CD(self):
            if not self.fd: raise portNotOpenError
            s = fcntl.ioctl(self.fd,TIOCMGET,TIOCM_zero_str)
            return struct.unpack('I',s)[0] & TIOCM_CD
            
        def RI(self):
            if not self.fd: raise portNotOpenError
            s = fcntl.ioctl(self.fd,TIOCMGET,TIOCM_zero_str)
            return struct.unpack('I',s)[0] & TIOCM_RI
    
    
        def CTS(self):
            if not self.fd: raise portNotOpenError
            s = fcntl.ioctl(self.fd,TIOCMGET,TIOCM_zero_str)
            return struct.unpack('I',s)[0] & TIOCM_CTS
    
    
        def DTR(self,on=None):
            if not self.fd: raise portNotOpenError
            if not on is None:
                if on:
                    fcntl.ioctl(self.fd,TIOCMBIS,TIOCM_DTR_str)
                else:
                    fcntl.ioctl(self.fd,TIOCMBIC,TIOCM_DTR_str)
            s = fcntl.ioctl(self.fd,TIOCMGET,TIOCM_zero_str)
            r = struct.unpack('I',s)[0] & TIOCM_DTR
            return r
    
            
        def RTS(self,on=None):
            if not self.fd: raise portNotOpenError
            if not on is None:
                if on:
                    fcntl.ioctl(self.fd,TIOCMBIS,TIOCM_RTS_str)
                else:
                    fcntl.ioctl(self.fd,TIOCMBIC,TIOCM_RTS_str)
            s = fcntl.ioctl(self.fd,TIOCMGET,TIOCM_zero_str)
            return struct.unpack('I',s)[0] & TIOCM_RTS
    
    
            
            
            
    

--- NEW FILE: __init__.py ---



More information about the POS-commit mailing list