hio.core.serial.serialing

Asynchronous (nonblocking) serial io

Module Contents

Classes

Console

Class to manage non blocking interactive I/O on serial console

ConsoleDoer

Basic Console Doer. Wraps console in doer context so opens and closes console

EchoConsoleDoer

Basic Terminal Console IO to buffers. Echos input back to output

Device

Class to manage non blocking IO on serial device port.

Serial

Class to manage non blocking IO on serial device port using pyserial

Driver

Nonblocking Serial Device Port Driver

Attributes

logger

hio.core.serial.serialing.logger[source]
exception hio.core.serial.serialing.LineError[source]

Bases: hio.hioing.HioError

Serial 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

reopen()[source]

closes and reopens .fd, sets .opened

close()[source]

closes .fd unsets .opened

get()[source]

returns chars including newline but no more than bs characters

put()[source]

puts characters

Hidden:

._line is bytearray of line buffer

MaxBufSize = 256[source]
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.

reopen(self)[source]

Idempotently opens console

close(self)[source]

Closes fd.

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.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(self)[source]
exit(self)[source]
class hio.core.serial.serialing.EchoConsoleDoer(console, lines=None, txbs=None, **kwa)[source]

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(self)[source]
recur(self, tyme)[source]
exit(self)[source]
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

close(self)[source]

Closes fd.

receive(self)[source]

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(self, data=b'\n')[source]

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

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’

close(self)[source]

Closes .serial

receive(self)[source]

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(self, data=b'\n')[source]

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)[source]

Nonblocking Serial Device Port Driver

serviceReceives(self)[source]

Service receives until no more

clearRxbs(self)[source]

Clear .rxbs

scan(self, start)[source]

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(self, data)[source]

Handle one tx data

tx(self, data)[source]

Queue data onto .txbs

serviceSends(self)[source]

Service .txbs

service(self)[source]

Sevice receives and sends