hio.core.serial.serialing
Asynchronous (nonblocking) serial io
Module Contents
Classes
Class to manage non blocking interactive I/O on serial console |
|
Basic Console Doer. Wraps console in doer context so opens and closes console |
|
Basic Terminal Console IO to buffers. Echos input back to output |
|
Class to manage non blocking IO on serial device port. |
|
Class to manage non blocking IO on serial device port using pyserial |
|
Nonblocking Serial Device Port Driver |
Attributes
- exception hio.core.serial.serialing.LineError[source]
Bases:
hio.hioing.HioErrorSerial line error. Too big for buffer.
- Usage:
raise LineError(“error message”)
- class hio.core.serial.serialing.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
- Hidden:
._line is bytearray of line buffer
- open(self, port='')[source]
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.
- put(self, data=b'\n')[source]
Writes data bytes to console and return number of bytes from data written.
- get(self, bs=None)[source]
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.
- class hio.core.serial.serialing.ConsoleDoer(console, **kwa)[source]
Bases:
hio.base.doing.DoerBasic Console Doer. Wraps console in doer context so opens and closes console
To test in WingIde must configure Debug I/O to use external console See Doer for inherited attributes, properties, and methods.
- .console is serial Console instance
- class hio.core.serial.serialing.EchoConsoleDoer(console, lines=None, txbs=None, **kwa)[source]
Bases:
hio.base.doing.DoerBasic Terminal Console IO to buffers. Echos input back to output
To test in WingIde must configure Debug I/O to use external console
See Doer for inherited attributes, properties, and methods.
- .console is serial Console instance
- class hio.core.serial.serialing.Device(port=None, speed=9600, bs=1024)[source]
Class to manage non blocking IO on serial device port.
Opens non blocking read file descriptor on serial port Use instance method close to close file descriptor Use instance methods get & put to read & write to serial device Needs os module
- reopen(self, port=None, speed=None, bs=None)[source]
Idempotently open serial device port Opens fd on serial port 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 and console at same time since it will mess up non blocking reads.
The input mode, canonical or noncanonical, is controlled by the ICANON flag see termios module.
Raw mode
- def setraw(fd, when=TCSAFLUSH):
Put terminal into a raw mode. mode = tcgetattr(fd) mode[IFLAG] = mode[IFLAG] & ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON) mode[OFLAG] = mode[OFLAG] & ~(OPOST) mode[CFLAG] = mode[CFLAG] & ~(CSIZE | PARENB) mode[CFLAG] = mode[CFLAG] | CS8 mode[LFLAG] = mode[LFLAG] & ~(ECHO | ICANON | IEXTEN | ISIG) mode[CC][VMIN] = 1 mode[CC][VTIME] = 0 tcsetattr(fd, when, mode)
# set up raw mode / no echo / binary cflag |= (TERMIOS.CLOCAL|TERMIOS.CREAD) lflag &= ~(TERMIOS.ICANON|TERMIOS.ECHO|TERMIOS.ECHOE|TERMIOS.ECHOK|TERMIOS.ECHONL|
TERMIOS.ISIG|TERMIOS.IEXTEN) #|TERMIOS.ECHOPRT
- for flag in (‘ECHOCTL’, ‘ECHOKE’): # netbsd workaround for Erk
- if hasattr(TERMIOS, flag):
lflag &= ~getattr(TERMIOS, flag)
oflag &= ~(TERMIOS.OPOST) iflag &= ~(TERMIOS.INLCR|TERMIOS.IGNCR|TERMIOS.ICRNL|TERMIOS.IGNBRK) if hasattr(TERMIOS, ‘IUCLC’):
iflag &= ~TERMIOS.IUCLC
- if hasattr(TERMIOS, ‘PARMRK’):
iflag &= ~TERMIOS.PARMRK
- class hio.core.serial.serialing.Serial(port=None, speed=9600, bs=1024)[source]
Class to manage non blocking IO on serial device port using pyserial
Opens non blocking read file descriptor on serial port Use instance method close to close file descriptor Use instance methods get & put to read & write to serial device Needs os module
- reopen(self, port=None, speed=None, bs=None)[source]
Opens fd on serial port in non blocking mode.
port is the serial port device path name or if None then use os.ctermid() which returns path name of console usually ‘/dev/tty’
- class hio.core.serial.serialing.Driver(name='', uid=0, port=None, speed=9600, bs=1024, server=None)[source]
Nonblocking Serial Device Port Driver