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 ot 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 This 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

-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 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.0a1.tar.gz (4.8 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file pexpect-serial-terminal-0.5.0a1.tar.gz.

File metadata

  • Download URL: pexpect-serial-terminal-0.5.0a1.tar.gz
  • Upload date:
  • Size: 4.8 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.0a1.tar.gz
Algorithm Hash digest
SHA256 347c677a8bfed413daf9a32d33f34e68a9e025495ec1ccd27f302a44ee29333a
MD5 4d8fda532000c2712522c7347af55dc0
BLAKE2b-256 f4b0e9e131d8cac0370c0cffc1d85a09349b1d334053c6d3b8852d9bbc5da554

See more details on using hashes here.

File details

Details for the file pexpect_serial_terminal-0.5.0a1-py3-none-any.whl.

File metadata

  • Download URL: pexpect_serial_terminal-0.5.0a1-py3-none-any.whl
  • Upload date:
  • Size: 6.6 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.0a1-py3-none-any.whl
Algorithm Hash digest
SHA256 0719ef9da43336af22671360afd73074ffb44672711989877540fb2713e7fb74
MD5 06422f720fc45a1440908b15309b05de
BLAKE2b-256 acbea177d6067a0eb68d962932b6826864527a04705bb412a25d1e808d8b73d4

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