/      日本語

mwings.utils Module

mwings.utils

Contains general-purpose utility functions.

src/mwings/utils.py

ask_user()

ask_user(prompt, regex, on_error, ex_verifier=None, max_attempts=None)

Gets input from the user via the console.

Arguments

NameTypeDescription
promptstrMessage displayed in the prompt
regexstrRegular expression to validate user input
on_errorstrMessage displayed when user input is invalid
ex_verifieroptional Callable[[str], bool]Function to further validate input string beyond regex
max_attemptsoptional intNumber of retry attempts. None means unlimited

Returns

TypeValueDescription
str-Validated user input string

ask_user_for_port()

ask_user_for_port()

Prompts the user to select a port to use.

Arguments

None

Returns

TypeValueDescription
str-Selected port name

byte_count_from()

byte_count_from(character_count)

Converts the length of a byte sequence represented as an ASCII string to the length of its binary representation.

Arguments

NameTypeDescription
character_countintLength of the ASCII representation of the byte sequence

Returns

TypeValueDescription
int-Length of the byte sequence in binary form

character_from()

character_from(hexvalue)

Converts a number from 0x0 to 0xF to its hexadecimal character representation.

Arguments

NameTypeDescription
hexvalueintNumber

Returns

TypeValueDescription
int-ASCII code of character

find_ascii()

find_ascii(port, data, timeout, debugging=False)

Waits to receive the specified ASCII string.

Arguments

NameTypeDescription
portserial.SerialSerial port
datastrASCII string
timeoutintTimeout (seconds)
debuggingboolDebug output

Returns

TypeValueDescription
boolTrueReception confirmed
boolFalseReception not confirmed

find_binary()

find_binary(port, data, timeout, debugging=False)

Waits to receive the specified byte sequence.

Arguments

NameTypeDescription
portserial.SerialSerial port
datastrByte sequence
with_terminalboolWhether to use terminal data
terminalintData to treat as terminal
timeoutintTimeout (seconds)
debuggingboolDebug output

Returns

TypeValueDescription
boolTrueReception confirmed
boolFalseReception not confirmed

flush_rx_buffer()

flush_rx_buffer(port)

Clears the receive buffer.

Arguments

NameTypeDescription
portserial.SerialSerial port

Returns

None

flush_tx_buffer()

flush_tx_buffer(port)

Clears the transmit buffer.

Arguments

NameTypeDescription
portserial.SerialSerial port

Returns

None

get_ports()

get_ports()

Retrieves a list of available serial ports.

Arguments

None

Returns

TypeValueDescription
list[str]-List of port names

hex_from()

hex_from(character)

Converts an ASCII character representing a hexadecimal digit to its numeric value. The result is undefined for characters other than 0-9 and A-F.

Arguments

NameTypeDescription
characterintASCII code of the character

Returns

TypeValueDescription
int-Numeric value

is_initialized()

is_initialized(port)

Checks whether the serial port is initialized.

Arguments

NameTypeDescription
portserial.SerialSerial port

Returns

TypeValueDescription
boolTrueInitialized
FalseNot initialized

is_readable()

is_readable(port)

Checks whether data can be read from the serial port.

Arguments

NameTypeDescription
portserial.SerialSerial port

Returns

TypeValueDescription
boolTrueData available in receive buffer
FalseNot readable

is_there_some_ports()

is_there_some_ports()

Checks whether any serial ports are available.

Arguments

None

Returns

TypeValueDescription
boolTrueSerial ports exist
FalseNone available

is_writable()

is_writable(port)

Checks whether data can be written to the serial port.

Arguments

NameTypeDescription
portserial.SerialSerial port

Returns

TypeValueDescription
boolTrueWritable
FalseNot writable

lrc8()

lrc8(data)

Calculates the 8-bit LRC.

Arguments

NameTypeDescription
databytesByte sequence to calculate

Returns

TypeValueDescription
int-LRC8 value

millis()

millis()

Gets the current system time in milliseconds.

Arguments

None

Returns

TypeValueDescription
int-System time (ms)

open_on_system()

open_on_system(path)

Opens the specified file path with the system’s default application.

Arguments

NameTypeDescription
pathPathFile path

Returns

None

write_binary()

write_binary(port, data)

Sends a byte sequence to the serial port.

Arguments

NameTypeDescription
portserial.SerialSerial port
dataintData to send
bytesSequence of data to send

Returns

None

write_in_ascii()

write_in_ascii(port, data)

Sends an ASCII representation of a byte sequence to the serial port.

Arguments

NameTypeDescription
portserial.SerialSerial port
dataintCharacter to send
bytesString to send

Returns

None