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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
# -*- coding: utf-8 -*-
"""Common routines and base driver class for serial-based meters.
"""
__author__ = 'Diego Elio Pettenò'
__email__ = 'flameeyes@flameeyes.eu'
__copyright__ = 'Copyright © 2017, Diego Elio Pettenò'
__license__ = 'MIT'
import logging
try:
from typing import Text
except:
pass
import serial
from glucometerutils import exceptions
class SerialDevice(object):
"""A Serial-connected glucometer driver base.
This class does not implement an actual driver by itself, but provides an
easier access to the boilerplate code required for pyserial.
This helper assumes that communication happens on a standard 8n1
configuration, with variable baudrate and no hardware flow control.
The actual drivers should set the following parameters:
BAUDRATE: (int) the speed the serial port should be opened at.
DEFAULT_CABLE_ID: (string) USB Vendor/Product ID pair, in format
abcd:abcd, of the default cable for the meter, in case the user
didn't pass an explicit device driver.
Optional parameters available:
TIMEOUT: (float, default: 1) the read timeout in seconds as defined by
pyserial.
After initialization, the following attributes can be used by the driver:
serial_: (serial.Serial) the open Serial object.
"""
BAUDRATE = None # type: int
DEFAULT_CABLE_ID = None # type: Text
TIMEOUT = 1 # type: float
def __init__(self, device):
assert self.BAUDRATE is not None
if not device and self.DEFAULT_CABLE_ID:
logging.info(
'No --device parameter provided, looking for default cable.')
device = 'hwgrep://' + self.DEFAULT_CABLE_ID
if not device:
raise exceptions.CommandLineError(
'No --device parameter provided, and no default cable known.')
self.serial_ = serial.serial_for_url(
device,
baudrate=self.BAUDRATE,
timeout=self.TIMEOUT,
writeTimeout=None,
bytesize=serial.EIGHTBITS, parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
xonxoff=False, rtscts=False, dsrdtr=False)
|