[Tutor] Raw Bits! (Control characters ahoy!)
alan.gauld at freenet.co.uk
Tue Apr 18 09:45:37 CEST 2006
Have you looked at the struct module which provides tools for
encoding binary values into byte streams and decoding byte streams
into binary data.
However if you only need to inject some hex codes into a string
you can use the chr() function, this works even for unprintable
characters like control sequences.
Author of the learn to program web tutor
----- Original Message -----
From: "doug shawhan" <doug.shawhan at gmail.com>
To: <tutor at python.org>
Sent: Monday, April 17, 2006 10:40 PM
Subject: [Tutor] Raw Bits! (Control characters ahoy!)
I am in the middle of a project that requires me to send and retrieve
information from a machine connected to a serial port. My problems are
1. I cannot send control characters
2. I cannot read data streaming from the serial port
I have been doing fine with:
os.system("echo '5' >/dev/tty00")
os.system("echo '8' >/dev/tty00")
and the like to the remote machine, but I need to be able to send control
characters the same way to scroll through menus. I have tried pyserial and
pexpect with mixed (mostly horrible) results (not the fault of the module
builders!), thus far the simple act of echoing the strings to the serial
port is the only reliable method I have found.
Reading from the serial port has also been problematic. I find that simply
opening the port /dev/tty00 file and using readlines(), read() or whatever
gives me empty strings or lists. Pbth.
So first: how should I go about adding the hex escape sequences (\x0a and
\x50) to the above strings in their raw form?
second: what method should I use to read a raw stream from the serial port
for a given period?
I have looked carefully at the neat and tidy miniterm.py example in the
pyserial examples directory, and see how one can continuously read via a
thread from the port, but have not been able to replicate it thus far.
Any help whatsoever would be great! I'm beginning to feel a little
More information about the Tutor