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

-c
        Add CR before LF when you change a new line.
-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.1.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

pexpect_serial_terminal-0.5.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: pexpect-serial-terminal-0.5.1.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.1.tar.gz
Algorithm Hash digest
SHA256 bb11b669b0dee65e5fb3eba852f05bdddb55d3fe6c38954943ebc67f65c0373e
MD5 f470610564cbefa21d0de2ae55f734de
BLAKE2b-256 0a2bbd570327daaefd2d192243824fc9a8cf32c99dc5b0429dc5d8672eece15a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: pexpect_serial_terminal-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 6.7 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 93b38b0848079ef5ef2bb2d66987074a07f8b6b0194db7e627e2f940bcd92b63
MD5 06bd22f7bf0b2c21832581414dbbf97c
BLAKE2b-256 2ec6a76b398824a3616611c52df77102d89014da70ca7e55fdcd46bc1578965c

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