hio.core.serial package

Submodules

hio.core.serial.serialing module

Asynchronous (nonblocking) serial io

class hio.core.serial.serialing.Console(bs=None)

Bases: object

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

MaxBufSize = 256
__init__(bs=None)

Initialization method for instance. Creates attributes.

close()

Closes fd.

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.

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

put(data=b'\n')

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

reopen(**kwa)

Idempotently opens console

service()

Service puts and gets

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

Bases: 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
__init__(console, **kwa)

Initialize instance.

Parameters:

instance (console is serial Console)

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

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

Bases: object

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

__init__(port=None, speed=9600, bs=1024)

Initialization method for instance.

port = serial device port path string speed = serial port speed in bps bs = buffer size for reads

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.

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

Bases: object

Nonblocking Serial Device Port Driver

__init__(name='', uid=0, port=None, speed=9600, bs=1024, server=None)

Initialization method for instance.

Parameters:
  • driver (uid = unique identifier for)

  • driver

  • string (port = serial device port path)

  • bps (speed = serial port speed in)

  • False (canonical = canonical mode True or)

  • reads (bs = buffer size for)

  • any (server = serial port device server if)

name = user friendly name for driver
uid = unique identifier for driver
server = serial device server nonblocking
txbs = bytearray of data bytes to send
rxbs = bytearray of data bytes received
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

service()

Sevice receives and sends

serviceReceives()

Service receives until no more

serviceSends()

Service .txbs

tx(data)

Queue data onto .txbs

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

Bases: 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
__init__(console, lines=None, txbs=None, **kwa)

Initialize instance.

Parameters:
  • instance (console is serial Console)

  • console (txbs is ouput bytes bytearray to send to)

  • console

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

exit()
recur(tyme)
exception hio.core.serial.serialing.LineError

Bases: HioError

Serial line error. Too big for buffer.

Usage:

raise LineError("error message")
class hio.core.serial.serialing.Serial(port=None, speed=9600, bs=1024)

Bases: object

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

__init__(port=None, speed=9600, bs=1024)

Initialization method for instance.

port = serial device port path string speed = serial port speed in bps bs = buffer size for reads

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.

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’

send(data=b'\n')

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

Module contents

hio.core.serial Package