blob: 91824d35585030881639f61876e10af7cd021f35 [file] [log] [blame]
Jack Jansen3b0a5cd1995-03-07 16:40:18 +00001#
2# Simple test program for ctb module: emulate a terminal.
Jack Jansencb2554e1997-04-03 14:45:28 +00003# To simplify matters use the python console window for output.
Jack Jansen3b0a5cd1995-03-07 16:40:18 +00004#
5import ctb
Jack Jansen5a6fdcd2001-08-25 12:15:04 +00006from Carbon import Evt
7from Carbon import Events
Jack Jansencb2554e1997-04-03 14:45:28 +00008import MacOS
Jack Jansen3b0a5cd1995-03-07 16:40:18 +00009import sys
10
11def cb(err):
12 print 'Done, err=', err
13
14def main():
15 if not ctb.available():
16 print 'Communications Toolbox not available'
17 sys.exit(1)
Jack Jansencb2554e1997-04-03 14:45:28 +000018 # Disable Python's event processing (we do that)
Jack Jansen7fb76e01997-06-12 10:49:56 +000019 MacOS.SchedParams(1, 0)
Jack Jansen3b0a5cd1995-03-07 16:40:18 +000020 print 'Minimal terminal emulator V1.0'
Jack Jansencb2554e1997-04-03 14:45:28 +000021 print '(type command-Q to exit)'
Jack Jansen3b0a5cd1995-03-07 16:40:18 +000022 print
Jack Jansen3b0a5cd1995-03-07 16:40:18 +000023
24 l = ctb.CMNew('Serial Tool', None)
Jack Jansencb2554e1997-04-03 14:45:28 +000025 l.Open(10)
26 l.SetConfig(l.GetConfig() + ' baud 4800')
Jack Jansen3b0a5cd1995-03-07 16:40:18 +000027
28 while 1:
Jack Jansencb2554e1997-04-03 14:45:28 +000029 l.Idle() # Give time to ctb
30
31 ok, evt = Evt.WaitNextEvent(0xffff, 0)
32 if ok:
33 what, message, when, where, modifiers = evt
34
35 if what == Events.keyDown:
36 # It is ours. Check for command-. to terminate
37 ch = chr(message & Events.charCodeMask)
38 if ch == 'q' and (modifiers & Events.cmdKey):
39 break
40 l.Write(ch, ctb.cmData, -1, 0)
41 d, dummy = l.Read(1000, ctb.cmData, 1)
Jack Jansen3b0a5cd1995-03-07 16:40:18 +000042 if d:
Jack Jansencb2554e1997-04-03 14:45:28 +000043 for ch in d:
44 if ch != '\r':
45 sys.stdout.write(ch)
46 sys.stdout.flush()
Jack Jansen3b0a5cd1995-03-07 16:40:18 +000047 l.Close(-1, 1)
48 del l
49
Jack Jansenb696b251995-12-21 15:38:14 +000050main()