mactty.py :  » Language-Interface » ChinesePython » chinesepython2.1.3-0.4 » Mac » Lib » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » ChinesePython 
ChinesePython » chinesepython2.1.3 0.4 » Mac » Lib » mactty.py
#
# mactty module - Use a terminal line as communications channel.
#
# Note that this module is not very complete or well-designed, but it
# will have to serve until I have time to write something better. A unix
# module with the same API is available too, contact me (jack@cwi.nl)
# if you need it.
#
# Usage:
# t = Tty(toolname)
# t.raw()    Set in raw/no-echo mode
# t.baudrate(rate)  Set baud rate
# t.reset()    Back to normal
# t.read(len)    Read up to 'len' bytes (but often reads less)
# t.readall(len)  Read 'len' bytes
# t.timedread(len,tout)  Read upto 'len' bytes, or until 'tout' seconds idle
# t.getmode()    Get parameters as a string
# t.setmode(args)  Set parameters
#
# Jack Jansen, CWI, January 1997
#
import ctb
DEBUG=1

class Tty:
  def __init__(self, name=None):
    self.connection = ctb.CMNew('Serial Tool', (10000, 10000, 0, 0, 0, 0))
    #self.orig_data = self.connection.GetConfig()
    #if name:
    #  self.connection.SetConfig(self.orig_data + ' Port "%s"'%name)
    self.connection.Open(10)
    sizes, status = self.connection.Status()

  def getmode(self):
    return self.connection.GetConfig()

  def setmode(self, mode):
    length = self.connection.SetConfig(mode)
    if length != 0 and length != len(mode):
      raise 'SetConfig Error', (mode[:length], mode[length:])
    
  def raw(self):
    pass
    
  def baudrate(self, rate):
    self.setmode(self.getmode()+' baud %d'%rate)

  def reset(self):
    self.setmode(self.orig_data)

  def readall(self, length):
    data = ''
    while len(data) < length:
      data = data + self.read(length-len(data))
    return data

  def timedread(self,length, timeout):
    self.connection.Idle()
    data, eom = self.connection.Read(length, ctb.cmData, timeout*10)
    if DEBUG:
      print 'Timedread(%d, %d)->%s'%(length, timeout, `data`)
    return data
    
  def read(self, length):
    return self.timedread(length, 0x3fffffff)
    
  def write(self, data):
    if DEBUG:
      print 'Write(%s)'%`data`
    while data:
      self.connection.Idle()
      done = self.connection.Write(data, ctb.cmData, 0x3fffffff, 0)
      self.connection.Idle()
      data = data[done:]
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.