Handling it .read(1) at a time is probably your best bet. Append them
into a bytearray and pull it all out when you're done.
It's a serial port; it's not like there is any degree of inefficiently
that you could write the code that will make it slow with respect to the
I/O. I write a LOT of serial instrument I/O and I've definitely had to
fall back to this plan. Your code gets a little long, you hide it all
in a function somewhere and never think on it again.
One paradigm I stick for ASCII serial is to have 3 functions:
def command(msg: str):
"""Sends a command, raises CommError if it doesn't get some
expected OK sort of thing."""
def query(msg: str):
"""Sends a commmand, returns a (trimmed) response line."""
def _communicate(msg: str):
The ugliest stuff, all the str->bytes->str stuff, the line-ending and
protocols, goes into _communicate. Query usually just calls
_communicate. Command slaps on whatever checks are needed. It feels a
bit heavy, but it leads to highly-usable code and makes it easy to
integrate logging, retries, integrating "*OPC?" handshakes, whatever
sort of things turn out to be necessary on a given device.
--
Rob Gaddi, Highland Technology --
www.highlandtechnology.com
Email address domain is currently out of order. See above to fix.