Programming the serial ports in unix

Michael P. Reilly arcege at shore.net
Wed Sep 8 07:37:06 EDT 1999


Alan Langman <alanlang at iafrica.com> wrote:
: Hi

: I wanting to write some python code to chat to my serial port. Does anyone
: know of some unix examples, modules etc.
: Anyhelp would be great appreciated.

Depending on what you want, ExpectPy can "chat" with a serial port.

import ExpectPy
cua0 = open('/dev/cua/a', 'rb+')
devcua0 = ExpectPy.spawnfile(cua0)
# sync with the modem
devcua0.send('AT\n')  # not \r on my system
devcua0.expect( (ExpectPy.EXACT, "AT\n", 1) )
# dialout
devcua0.send('ATDT5551212\n')
rc = devcua0.expect(
  (ExpectPy.EXACT, "RING\n", lambda match: devcua0.cont()),
  (ExpectPy.GLOB, "CONNECT *\n", "connected"),
  (ExpectPy.REGEXP, "NO CARRIER|BUSY", "not connected")
)
if rc == 'not connected':
  devcua0.close()
  raise SystemExit, "Modem could not connect"
...

(I haven't tested this bit of code specifically, but tested something
similar.)

  -Arcege





More information about the Python-list mailing list