File: __init__.py

package info (click to toggle)
modem-cmd 1.0.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 148 kB
  • sloc: python: 83; makefile: 5
file content (34 lines) | stat: -rw-r--r-- 821 bytes parent folder | download | duplicates (3)
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
26
27
28
29
30
31
32
33
34
from __future__ import print_function
from serial import Serial
from serial import SerialException
from serial import SerialTimeoutException


class ModemcmdTimeoutException(Exception):
    def __init__(self, msg):
        self.msg = msg


class ModemcmdException(Exception):
    def __init__(self, msg):
        self.msg = msg


def modemcmd(port, cmd, timeout=0.3):
    try:
        serial = Serial(port=port,
                        timeout=float(timeout))
    except SerialException as e:
        raise ModemcmdException(e)

    cmd = cmd + '\r'
    try:
        serial.write(cmd)
    except SerialTimeoutException:
        raise ModemcmdTimeoutException('Write timeout')

    lines = serial.readlines()
    if lines == '':  # timeout
        raise ModemcmdTimeoutException('Read timeout')

    return "".join(lines)