Skip to main content

serial terminal for pexpect on both windows and linux platforms

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pexpect-serial-terminal-0.5.2.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

pexpect_serial_terminal-0.5.2-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file pexpect-serial-terminal-0.5.2.tar.gz.

File metadata

  • Download URL: pexpect-serial-terminal-0.5.2.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using 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

File hashes

Hashes for pexpect-serial-terminal-0.5.2.tar.gz
Algorithm Hash digest
SHA256 43a097afa2f4e873ef323b784a74c8b5b63cc94e2d5457071dc7cfd8f40d3503
MD5 b52cfb250d8d96941c3729e393e18389
BLAKE2b-256 6769434b6870119021f3e64fa1830eb6dbdf0f3891c20e33ca30a62ca6e54100

See more details on using hashes here.

File details

Details for the file pexpect_serial_terminal-0.5.2-py3-none-any.whl.

File metadata

  • Download URL: pexpect_serial_terminal-0.5.2-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using 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

File hashes

Hashes for pexpect_serial_terminal-0.5.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b10b60ec11c0686b1594985e07c312e4e5b4f9d2d1b7a4685a2a65d429c13329
MD5 40846cbfaa60f5123d0f907242f8621d
BLAKE2b-256 2c8bb49a3d13cb8033e93133b66eab95725dc942b7d5bca2f2fcf74034b0c684

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page