2 # mactty module - Use a terminal line as communications channel.
4 # Note that this module is not very complete or well-designed, but it
5 # will have to serve until I have time to write something better. A unix
6 # module with the same API is available too, contact me (jack@cwi.nl)
11 # t.raw() Set in raw/no-echo mode
12 # t.baudrate(rate) Set baud rate
13 # t.reset() Back to normal
14 # t.read(len) Read up to 'len' bytes (but often reads less)
15 # t.readall(len) Read 'len' bytes
16 # t.timedread(len,tout) Read upto 'len' bytes, or until 'tout' seconds idle
17 # t.getmode() Get parameters as a string
18 # t.setmode(args) Set parameters
20 # Jack Jansen, CWI, January 1997
26 def __init__(self
, name
=None):
27 self
.connection
= ctb
.CMNew('Serial Tool', (10000, 10000, 0, 0, 0, 0))
28 #self.orig_data = self.connection.GetConfig()
30 # self.connection.SetConfig(self.orig_data + ' Port "%s"'%name)
31 self
.connection
.Open(10)
32 sizes
, status
= self
.connection
.Status()
35 return self
.connection
.GetConfig()
37 def setmode(self
, mode
):
38 length
= self
.connection
.SetConfig(mode
)
39 if length
!= 0 and length
!= len(mode
):
40 raise 'SetConfig Error', (mode
[:length
], mode
[length
:])
45 def baudrate(self
, rate
):
46 self
.setmode(self
.getmode()+' baud %d'%rate
)
49 self
.setmode(self
.orig_data
)
51 def readall(self
, length
):
53 while len(data
) < length
:
54 data
= data
+ self
.read(length
-len(data
))
57 def timedread(self
,length
, timeout
):
58 self
.connection
.Idle()
59 data
, eom
= self
.connection
.Read(length
, ctb
.cmData
, timeout
*10)
61 print 'Timedread(%d, %d)->%s'%(length
, timeout
, `data`
)
64 def read(self
, length
):
65 return self
.timedread(length
, 0x3fffffff)
67 def write(self
, data
):
69 print 'Write(%s)'%`data`
71 self
.connection
.Idle()
72 done
= self
.connection
.Write(data
, ctb
.cmData
, 0x3fffffff, 0)
73 self
.connection
.Idle()