File: __init__.py

package info (click to toggle)
pymonoprice 0.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 136 kB
  • sloc: python: 641; makefile: 2
file content (25 lines) | stat: -rw-r--r-- 738 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import threading
import os
import pty


def create_dummy_port(responses):
    def listener(port):
        # continuously listen to commands on the master device
        while 1:
            res = b''
            while not res.endswith(b"\r"):
                # keep reading one byte at a time until we have a full line
                res += os.read(port, 1)
            print("command: %s" % res)

            # write back the response
            if res in responses:
                resp = responses[res]
                del responses[res]
                os.write(port, resp)

    master, slave = pty.openpty()
    thread = threading.Thread(target=listener, args=[master], daemon=True)
    thread.start()
    return os.ttyname(slave)