hio.core.serial.serialing

Asynchronous (nonblocking) serial io

Module Contents

exception hio.core.serial.serialing.LineError

Bases: hio.hioing.HioError

Serial line error. Too big for buffer.

Usage:

raise LineError("error message")
class hio.core.serial.serialing.Console(bs=None)

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 and unsets .opened

- get

returns chars including newline but no more than bs characters

- put

puts characters

Hidden:

._line is bytearray of line buffer

open(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.

On UNIX/macOS systems uses os.ctermid() to get console On Windows systems uses console directly via msvcrt

reopen(**kwa)

Idempotently opens console

close()

Closes fd.

put(data=b'\n')

Writes data bytes to console and return number of bytes from data written.

get(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.

service()

Service puts and gets

class hio.core.serial.serialing.ConsoleDoer(console, **kwa)

Bases: hio.base.doing.Doer

Basic 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
enter(*, temp=None)

Do ‘enter’ context actions. Override in subclass. Not a generator method. Set up resources. Comparable to context manager enter.

Parameters:

temp (bool | None) – True means use temporary file resources if any None means ignore parameter value. Use self.temp

Inject temp or self.temp into file resources here if any

Doist or DoDoer winds its doers on enter

recur(tyme)

Do ‘recur’ context actions. Override in subclass. Regular method that perform repetitive actions once per invocation. Assumes resource setup in .enter() and resource takedown in .exit() (see ReDoer below for example of .recur that is a generator method)

Returns completion state of recurrence actions: True means done, False means continue.

Doist provides the time value.

.recur maybe implemented by a subclass either as a non-generator method or a generator method. This stub here is as a non-generator method. The base class .do detects which type:

If non-generator .do method runs .recur method once per iteration
    until .recur returns (True)
If generator .do method runs .recur with (yield from) until .recur
    returns (see ReDoer for example of generator .recur)
exit()

Do ‘exit’ context actions. Override in subclass. Not a generator method. Clean up resources. Comparable to context manager exit. Called by finally after normal return, close, or abort. After .exit() do returns resulting in StopIteration.

class hio.core.serial.serialing.EchoConsoleDoer(console, lines=None, txbs=None, **kwa)

Bases: hio.base.doing.Doer

Basic 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
enter(*, temp=None)

Do ‘enter’ context actions. Override in subclass. Not a generator method. Set up resources. Comparable to context manager enter.

Parameters:

temp (bool | None) – True means use temporary file resources if any None means ignore parameter value. Use self.temp

Inject temp or self.temp into file resources here if any

Doist or DoDoer winds its doers on enter

recur(tyme)

Do ‘recur’ context actions. Override in subclass. Regular method that perform repetitive actions once per invocation. Assumes resource setup in .enter() and resource takedown in .exit() (see ReDoer below for example of .recur that is a generator method)

Returns completion state of recurrence actions: True means done, False means continue.

Doist provides the time value.

.recur maybe implemented by a subclass either as a non-generator method or a generator method. This stub here is as a non-generator method. The base class .do detects which type:

If non-generator .do method runs .recur method once per iteration
    until .recur returns (True)
If generator .do method runs .recur with (yield from) until .recur
    returns (see ReDoer for example of generator .recur)
exit()

Do ‘exit’ context actions. Override in subclass. Not a generator method. Clean up resources. Comparable to context manager exit. Called by finally after normal return, close, or abort. After .exit() do returns resulting in StopIteration.

class hio.core.serial.serialing.Device(port=None, speed=9600, bs=1024)

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(port=None, speed=None, bs=None)

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
close()

Closes fd.

receive()

Reads nonblocking characters from serial device up to bs characters Returns empty bytes if no characters available else returns all available. In canonical mode no chars are available until newline is entered.

send(data=b'\n')

Writes data bytes to serial device port. Returns number of bytes sent

class hio.core.serial.serialing.Serial(port=None, speed=9600, bs=1024)

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(port=None, speed=None, bs=None)

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’

close()

Closes .serial

receive()

Reads nonblocking characters from serial device up to bs characters Returns empty bytes if no characters available else returns all available. In canonical mode no chars are available until newline is entered.

send(data=b'\n')

Writes data bytes to serial device port. Returns number of bytes sent

class hio.core.serial.serialing.Driver(name='', uid=0, port=None, speed=9600, bs=1024, server=None)

Nonblocking Serial Device Port Driver

serviceReceives()

Service receives until no more

clearRxbs()

Clear .rxbs

scan(start)

Returns offset of given start byte in self.rxbs Returns None if start is not given or not found If strip then remove any bytes before offset

send(data)

Handle one tx data

tx(data)

Queue data onto .txbs

serviceSends()

Service .txbs

service()

Sevice receives and sends