Skip to content

Commit

Permalink
use lock for serial port use
Browse files Browse the repository at this point in the history
  • Loading branch information
gpulido committed Jul 17, 2024
1 parent e738215 commit c7b6dda
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
56 changes: 30 additions & 26 deletions selve/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/usr/bin/python
import time
import serial
from enum import Enum
import logging
from threading import Lock

from selve.utils import *
from selve.iveo import *
Expand All @@ -22,6 +23,7 @@ def __init__(self, port, discover = True):
the devices on init (default: {True})
"""
self.port = port
self.lock = Lock()
if discover:
self.discover()

Expand Down Expand Up @@ -64,35 +66,37 @@ def executeCommand(self, command):
_LOGGER.info('Gateway writting: ' + str(commandstr))

try:
self.lock.adquire()
self.configserial()

if self.ser.isOpen():
try:
self.ser.flushInput()
self.ser.flushOutput()
self.ser.write(commandstr)
self.ser.flush()
time.sleep(0.5)
response_str = ""
while True:
response = self.ser.readline().strip()
response_str += response.decode()
_LOGGER.info('read data: ' + response_str)
if response.decode() == '':
break

self.ser.close()
_LOGGER.info('read data: ' + response_str)
return process_response(response_str)
except Exception as e1:
_LOGGER.exception ("error communicating...: " + str(e1))
else:
_LOGGER.error ("cannot open serial port")

except Exception as e:
_LOGGER.error ('error open serial port: ' + str(e))
exit()

if self.ser.isOpen():
try:
self.ser.flushInput()
self.ser.flushOutput()
self.ser.write(commandstr)
self.ser.flush()
#time.sleep(0.5)
response_str = ""
while True:
response = self.ser.readline().strip()
response_str += response.decode()
_LOGGER.info('read data: ' + response_str)
if response.decode() == '':
break

self.ser.close()
_LOGGER.info('read data: ' + response_str)
return process_response(response_str)
except Exception as e1:
_LOGGER.exception ("error communicating...: " + str(e1))
else:
_LOGGER.error ("cannot open serial port")

finally:
self.lock.release()
return None

# def serial_data(self, data):
Expand All @@ -108,7 +112,7 @@ def discover(self):
while not hasattr(command, "ids") and retry_n <=num_retries:
command.execute(self)
retry_n += 1
#time.sleep(1)
time.sleep(1)

if not hasattr(command, "ids"):
_LOGGER.info("Associated Devices not found")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
setup(

name='python-selve', # Required
version='1.3.1', # Required
version='1.4.0', # Required
description='Python library for interfacing with selve devices using the USB-RF controller', # Required
long_description=long_description, # Optional
url='https://github.com/gpulido/python-selve', # Optional
Expand Down

0 comments on commit c7b6dda

Please sign in to comment.