Skip to main content

Issue

PySerial does not have POSIX fileno attribute on Windows platform. If you want to use pexpect and serial device on Windows platform, you must use something like this:

import os
import pexpect.fdpexpect

device = pexpect.fdpexpect.fdspawn(os.open("COM3", os.O_RDWR))

But using this method cannot change the baud rate or data bits or etc. And you have to deal with the serial device's input/output buffered stuff. It's very annoying.

Main Idea

Create a simple terminal pexpect-serial-terminal for serial device. And we can use it with pexpect package. The pexpect-serial-terminal will send input to the device and get output from the device continuously.

flow-chart

Installation

Install from PyPI:

pip install pexpect-serial-terminal

Install directly. Clone this repository, and then:

pip install .

Verify Serial Device With Module

Use the help command to find out the parameters.

> python -m pexpect_serial_terminal -h

The option and option's parameter list below:
Ex:
        __main__.py -p COM3 -b 115200

-l | --line-ending
        [ default value: LF ]
        < LF | CR | CRLF >
        Device line ending.
-p | --port
        [ default value: None ]
        Serial port name.
-b | --baud-rate
        [ default value: 115200 ]
        Baud rate.
-w | --data-bits
        [ default value: 8 ]
        < 8 | 7 >
        Data bits.
-r | --parity
        [ default value: None ]
        < None | Even | Odd | Space | Mark >
        Parity.
-s | --stop-bits
        [ default value: 1 ]
        < 1 | 2 | 1.5 >
        Stop bits.
-x | --soft-flow-ctl
        [ default value: off ]
        < on | off >
        Software flow control switch.
-f | --hard-flow-ctl
        [ default value: off ]
        < on | off >
        Hardware flow control switch.

Use this terminal to verify the serial device. This terminal is too simple to do the terminal well. It doesn't have input buffer to manipulate the content, so it doesn't support dos key function. I strongly suggest you should only use this terminal for testing or automatic script, don't use it as pure terminal.

python -m pexpect_serial_terminal -p COM3

Exit the terminal

Ctl+C

How To Use It With Pexpect?

You can also see the tests folder for the pytest example with this module.

start the child process on Windows:

import sys
from pexpect.popen_spawn import PopenSpawn

process = PopenSpawn('cmd.exe', timeout=3)
command = f'{sys.executable} -m pexpect_serial_terminal -p COM3'
process.sendline(command)

start the child process on Linux:

import sys
from pexpect.popen_spawn import PopenSpawn

process = PopenSpawn('/bin/bash', timeout=3)
command = f'{sys.executable} -m pexpect_serial_terminal -p /dev/ttyUSB0'
process.sendline(command)

do your own pexpect stuff:

prompt = r'\w+@.+:.+[#\$]'
process.sendline('date')
process.expect(prompt, 3)
process.before
process.after

exit the terminal:

process.write('\x03')

Metadata

Release files for pexpect-serial-terminal 0.5.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pexpect-serial-terminal 0.5.2
File Size Uploaded
pexpect-serial-terminal-0.5.2.tar.gz 5.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pexpect-serial-terminal 0.5.2
File Interpreter ABI Platform
pexpect_serial_terminal-0.5.2-py3-none-any.whl Python 3 none any Details

Total release size: 11.8 kB

Release files / pexpect-serial-terminal-0.5.2.tar.gz

Download URL pexpect-serial-terminal-0.5.2.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
43a097afa2f4e873ef323b784a74c8b5b63cc94e2d5457071dc7cfd8f40d3503
BLAKE2b-256 checksum
How to use checksums
6769434b6870119021f3e64fa1830eb6dbdf0f3891c20e33ca30a62ca6e54100
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.6.9

Release files / pexpect_serial_terminal-0.5.2-py3-none-any.whl

Download URL pexpect_serial_terminal-0.5.2-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b10b60ec11c0686b1594985e07c312e4e5b4f9d2d1b7a4685a2a65d429c13329
BLAKE2b-256 checksum
How to use checksums
2c8bb49a3d13cb8033e93133b66eab95725dc942b7d5bca2f2fcf74034b0c684
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.6.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.54.0 CPython/3.6.9

Release history Release notifications | RSS feed

This release

0.5.2 This release

2 release files

0.5.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page