hio.core.serial
hio.core.serial Package
Submodules
Package Contents
Classes
Class to manage non blocking interactive I/O on serial console |
- class hio.core.serial.Console(bs=None)[source]
Class to manage non blocking interactive I/O on serial console
Opens non blocking read file descriptor on console Use instance method .close to close file descriptor Use instance methods .getline to read & .put to write to console Needs os module
- bs
max buffer size for each read, defaults to 256
- Type
int
- fd
file descriptor for console
- Type
int
- opened
True means .fd opened, False means .fd closed
- Type
bool
- rxbs
of received characters (bytes)
- Type
bytearray
- reopen()
closes and reopens .fd, sets .opened
- close()
closes .fd unsets .opened
- get()
returns chars including newline but no more than bs characters
- put()
puts characters
- Hidden:
._line is bytearray of line buffer
- MaxBufSize = 256
- open(self, port='')
Opens fd on terminal console in non blocking mode.
port is the serial port device path name or if ‘’ then use os.ctermid() which returns path name of console usually ‘/dev/tty’
os.O_NONBLOCK makes non blocking io os.O_RDWR allows both read and write. os.O_NOCTTY don’t make this the controlling terminal of the process O_NOCTTY is only for cross platform portability BSD never makes it the controlling terminal
Don’t use print at same time since it will mess up non blocking reads.
Works in both canonical and non-canonical input mode. In canonical mode, no chars are available to read until eol newline is entered and eol is included in the read characters.
It appears that canonical mode is the default for fd console os.ctermid(). For other serial port fds the characters may be available immediately.
To debug use os.isatty(fd) which returns True if the file descriptor fd is open and connected to a tty-like device, else False.
- reopen(self)
Idempotently opens console
- close(self)
Closes fd.
- put(self, data=b'\n')
Writes data bytes to console and return number of bytes from data written.
- get(self, bs=None)
Gets nonblocking line of bytes from console of up to bs characters including eol newline if in bs characters otherwise must repeat get until a newline appears.
Returns empty string if no characters available else returns line. Works in both canonical and non-canonical mode In canonical mode, no chars are available to read until eol newline is entered and eol is included in the read characters.
Strips eol newline before returning line.