Skip to content

Commit

Permalink
Add lock and reset buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
joopert committed Jun 5, 2017
1 parent b435fdf commit c49379b
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions nad_receiver/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from time import sleep
from nad_receiver.nad_commands import CMDS
import serial # pylint: disable=import-error
import threading

DEFAULT_TIMEOUT = 1
DEFAULT_WRITE_TIMEOUT = 1
Expand All @@ -23,6 +24,7 @@ def __init__(self, serial_port, timeout=DEFAULT_TIMEOUT,
"""Create RS232 connection."""
self.ser = serial.Serial(serial_port, baudrate=115200, timeout=timeout,
write_timeout=write_timeout)
self.lock = threading.Lock()

def exec_command(self, domain, function, operator, value=None):
"""
Expand All @@ -45,14 +47,19 @@ def exec_command(self, domain, function, operator, value=None):
if not self.ser.is_open:
self.ser.open()

self.ser.reset_input_buffer()
self.ser.reset_output_buffer()
self.lock.acquire()

self.ser.write(''.join(['\r', cmd, '\r']).encode('utf-8'))

self.ser.read(1) # NAD uses the prefix and suffix \r.
# With this we read the first \r and skip it
msg = self.ser.read_until(bytes('\r'.encode()))
self.lock.release()

return msg.decode().strip().split('=')[
1] # b'Main.Volume=-12\r will return -12
return msg.decode().strip().split('=')[1]
# b'Main.Volume=-12\r will return -12

def main_dimmer(self, operator, value=None):
"""Execute Main.Dimmer."""
Expand Down

0 comments on commit c49379b

Please sign in to comment.