summaryrefslogtreecommitdiffstats
path: root/Mac/Lib/test/ctbtest.py
blob: 0f3cd90fc2b01e2b5ab19bdafafddf6a9d9a3bc3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#
# Simple test program for ctb module: emulate a terminal.
# To simplify matters use the python console window for output.
#
import ctb
import Evt
import Events
import MacOS
import sys

def cb(err):
	print 'Done, err=', err

def main():
	if not ctb.available():
		print 'Communications Toolbox not available'
		sys.exit(1)
	# Disable Python's event processing (we do that)
	MacOS.EnableAppswitch(0)
	print 'Minimal terminal emulator V1.0'
	print '(type command-Q to exit)'
	print
	
	l = ctb.CMNew('Serial Tool', None)
	l.Open(10)
	l.SetConfig(l.GetConfig() + ' baud 4800')
	
	while 1:
		l.Idle()	# Give time to ctb
		
		ok, evt = Evt.WaitNextEvent(0xffff, 0)
		if ok:
			what, message, when, where, modifiers = evt
			
			if what == Events.keyDown:
				# It is ours. Check for command-. to terminate
				ch = chr(message & Events.charCodeMask)
				if ch == 'q' and (modifiers & Events.cmdKey):
					break
				l.Write(ch, ctb.cmData, -1, 0)
		d, dummy = l.Read(1000, ctb.cmData, 1)
		if d:
			for ch in d:
				if ch != '\r':
					sys.stdout.write(ch)
			sys.stdout.flush()
	l.Close(-1, 1)
	del l
			
main()