Skip to main content

Concurrency agnostic serialio API

Project description

serialio

Pypi version

A python concurrency agnostic serial line library.

Helpful when handling with instrumentation which work over serial line and implement simple REQ-REP communication protocols (example: SCPI).

Besides local serial line, serialio also supports serial line over RFC2217 protocol, raw TCP socket and tango.

As far as RFC2217 is concerned, it should be compatible with:

  • ser2net bridge with telnet(RFC2217) and raw configurations
  • gserial[ser2tcp] bridge (RFC2217)

As far as tango is concerned, it should be compatible with the tango classes:

Base implementation written in asyncio with support for different concurrency models:

  • asyncio
  • classic blocking API (TODO)
  • future based API (TODO)

Here is a summary of what is forseen and what is implemented

Concurrency Local RFC2217 Raw TCP Tango
asyncio Y Y Y Y
classic sync N N N N
conc. futures N N N N

Installation

From within your favourite python environment:

pip install serialio

Usage

asyncio

import asyncio
import serialio.aio.tcp

async def main():
    sl = serialio.serial_for_url("serial-tcp://lab1.acme.org:5000")
    # or the equivalent:
    # sl = serialio.aio.tcp.Serial("lab1.acme.org", 5000)

    await sl.open()

    # Assuming a SCPI complient on the other end we can ask for:
    reply = await sl.write_readline(b"*IDN?\n")
    print(reply)
    await sl.close()

asyncio.run(main())

local serial line

import serialio.aio.posix

sl = serialio.aio.posix.Serial("/dev/ttyS0")

# or the equivalent

sl = serialio.serial_for_url("serial:///dev/ttyS0")

raw TCP socket

import serialio.aio.tcp

sl = serialio.aio.tcp.Serial("lab1.acme.org:5000")

# or the equivalent

sl = serialio.serial_for_url("serial-tcp://lab1.acme.org:5000")

RFC2217 (telnet)

import serialio.aio.rfc2217

sl = serialio.aio.rfc2217.Serial("lab1.acme.org:5000")

# or the equivalent

sl = serialio.serial_for_url("rfc2217://lab1.acme.org:5000")

Tango

(needs a pip install serialio[tango] installation)

import serialio.aio.tango
sl = serialio.aio.tango.Serial("lab/01/serial-01")

# or the equivalent

sl = serialio.serial_for_url("tango://lab/01/serial-01")

classic (TODO)

from serialio.aio.tcp import Serial

sl = Serial("lab1.acme.org", 5000)
reply = sl.write_readline(b"*IDN?\n")
print(reply)

concurrent.futures (TODO)

from serialio.sio.tcp import Serial

sl = Serial("lab1.acme.org", 5000, resolve_futures=False)
reply = sl.write_readline(b"*IDN?\n").result()
print(reply)

API differences with serial

  • coroutine based API
  • open() coroutine must be called explicitly before using the serial line
  • setting of parameters done through functions instead of properties (ie: await ser_line.set_XXX(value) instead of ser_line.XXX = value (ex: await ser_line.set_baudrate()))
  • custom eol character (serial is fixed to b"\n")
  • included REQ/REP atomic functions (write_read() family)

Features

The main goal of a serialio Serial object is to facilitate communication with instruments connected to a serial line.

The most frequent cases include instruments which expect a REQ/REP semantics with ASCII protocols like SCPI. In these cases most commands translate in small packets being exchanged between the host and the instrument.

REQ-REP semantics

Many instruments out there have a Request-Reply protocol. A serialio Serial provides helpfull write_read family of methods which simplify communication with these instruments.

Custom EOL

In line based protocols, sometimes people decide \n is not a good EOL character. A serialio can be customized with a different EOL character. For example, the XIA-PFCU always replies with ;\r\n, so we could configure it using the following snippet:

sl = serialio.serial_for_url("serial:///dev/ttyS0", eol=b";\r\n")
await sl.open()

The initial EOL character can be overwritten in any of the readline methods. Example:

await sl.write_readline(b"*IDN?\n", eol=b"\r")

Streams

TODO: Write this chapter

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

serialio-2.3.2.tar.gz (21.3 kB view details)

Uploaded Source

File details

Details for the file serialio-2.3.2.tar.gz.

File metadata

  • Download URL: serialio-2.3.2.tar.gz
  • Upload date:
  • Size: 21.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.0 setuptools/50.3.1.post20201107 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.0

File hashes

Hashes for serialio-2.3.2.tar.gz
Algorithm Hash digest
SHA256 a7711ec3760dd318b847abb20190833098f68d27f8ef15e7bf60625ba64da824
MD5 0cf9f873979846e6d78bb863404e29e2
BLAKE2b-256 53ec22f77acf429bacc870f4dfabd8b569bc9e8f2755405321f86e422d1adbd3

See more details on using hashes here.

Supported by

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