-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserial_connection.py
58 lines (49 loc) · 1.8 KB
/
serial_connection.py
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
#!/usr/bin/env python
import logging
import serial
logger = logging.getLogger("paradox_mqtt").getChild(__name__)
class Serial_Connection:
def __init__(self, port="/dev/ttyUSB0", baudrate=9600, timeout=5, rtscts=False):
"""Initialise Serial_Connection."""
logger.debug("Initialising Serial_Connection...")
self.connection = None
self.port = port
self.baudrate = baudrate
self.timeout = timeout
self.rtscts = rtscts
logger.debug("Initialised Serial_Connection.")
def connect(self):
"""Connect to serial port."""
logger.debug("Connecting to serial port...")
self.connection = serial.Serial(
port=self.port,
baudrate=self.baudrate,
timeout=self.timeout,
rtscts=self.rtscts,
)
try:
self.connection.open()
logger.debug("Connected to serial port.")
return True
except:
logger.error("Could not connect to serial port.")
return False
def write(self, data):
"""Write data to serial port."""
self.connection.write(data)
def read(self, bytes=37, timeout=1):
"""Read bytes from serial port waiting up to timeout."""
old_timeout = self.connection.timeout
self.connection.timeout = timeout
data = self.connection.read(bytes)
self.connection.timeout = old_timeout
return data
def in_waiting(self):
"""Check how many butes are waiting on connection."""
return self.connection.in_waiting
def reset_input_buffer(self):
"""Clear input buffer."""
return self.connection.reset_input_buffer()
def reset_output_buffer(self):
"""Clear output buffer."""
return self.connection.reset_output_buffer()