Skip to content

Commit

Permalink
adds await writer.wait_closed()
Browse files Browse the repository at this point in the history
updates pymodbus (again)
  • Loading branch information
MAKOMO committed Dec 20, 2024
1 parent d9e440e commit 81824ae
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
2 changes: 2 additions & 0 deletions src/artisanlib/async_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ async def open_serial_connection(*, loop:Optional[asyncio.AbstractEventLoop] = N
writer = asyncio.StreamWriter(transport, protocol, reader, loop)
return reader, writer


# to be overwritten by the subclass

def reset_readings(self) -> None:
Expand Down Expand Up @@ -298,6 +299,7 @@ async def connect(self, connect_timeout:float=5) -> None:
if writer is not None:
try:
writer.close()
await writer.wait_closed()
except Exception as e: # pylint: disable=broad-except
_log.error(e)

Expand Down
13 changes: 10 additions & 3 deletions src/artisanlib/kaleido.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ async def ws_connect(self, mode:str, host:str, port:int, path:str,

@staticmethod
async def open_serial_connection(*, loop:Optional[asyncio.AbstractEventLoop] = None,
limit:Optional[int] = None, **kwargs:Union[int,float,str]) -> Tuple[asyncio.StreamReader, asyncio.StreamWriter]:
limit:Optional[int] = None, **kwargs:Union[int,float,str]) -> Tuple[asyncio.Transport, asyncio.StreamReader, asyncio.StreamWriter]:
"""A wrapper for create_serial_connection() returning a (reader,
writer) pair.
Expand All @@ -364,7 +364,7 @@ async def open_serial_connection(*, loop:Optional[asyncio.AbstractEventLoop] = N
loop=loop, protocol_factory=lambda: protocol, **kwargs
)
writer = asyncio.StreamWriter(transport, protocol, reader, loop)
return reader, writer
return transport, reader, writer


async def serial_handle_reads(self, reader: asyncio.StreamReader) -> None:
Expand Down Expand Up @@ -427,6 +427,7 @@ async def serial_connect(self, mode:str, serial:SerialSettings,
connected_handler:Optional[Callable[[], None]] = None,
disconnected_handler:Optional[Callable[[], None]] = None) -> None:

transport = None
writer = None
while True:
try:
Expand All @@ -440,7 +441,7 @@ async def serial_connect(self, mode:str, serial:SerialSettings,
parity=serial['parity'],
timeout=serial['timeout'])
# Wait for 2 seconds, then raise TimeoutError
reader, writer = await asyncio.wait_for(connect, timeout=self._open_timeout)
transport, reader, writer = await asyncio.wait_for(connect, timeout=self._open_timeout)

self._write_queue = asyncio.Queue()
await asyncio.wait_for(self.serial_initialize(reader, writer, mode), timeout=self._init_timeout)
Expand Down Expand Up @@ -472,6 +473,12 @@ async def serial_connect(self, mode:str, serial:SerialSettings,
if writer is not None:
try:
writer.close()
await writer.wait_closed()
except Exception as e: # pylint: disable=broad-except
_log.error(e)
if transport is not None:
try:
transport.close()
except Exception as e: # pylint: disable=broad-except
_log.error(e)

Expand Down
30 changes: 22 additions & 8 deletions src/artisanlib/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5517,22 +5517,36 @@ def openMachineSettings(self, _checked:bool = False) -> None:
if self.qmc.roastersize_setup in heating_ratings:
ratings = heating_ratings[self.qmc.roastersize_setup]
if 'loadlabels' in ratings and len(ratings['loadlabels']) == 4:
self.qmc.loadlabels_setup = ratings['loadlabels']
self.qmc.loadlabels_setup = [str(lb) for lb in ratings['loadlabels']]
if 'loadratings' in ratings and len(ratings['loadratings']) == 4:
self.qmc.loadratings_setup = ratings['loadratings']
self.qmc.loadratings_setup = [float(lr) for lr in ratings['loadratings']]
if 'ratingunits' in ratings and len(ratings['ratingunits']) == 4:
self.qmc.ratingunits_setup = ratings['ratingunits']
self.qmc.ratingunits_setup = [int(lu) for lu in ratings['ratingunits']]
if 'sourcetypes' in ratings and len(ratings['sourcetypes']) == 4:
self.qmc.sourcetypes_setup = ratings['sourcetypes']
self.qmc.sourcetypes_setup = [int(st) for st in ratings['sourcetypes']]
if 'load_etypes' in ratings and len(ratings['load_etypes']) == 4:
self.qmc.load_etypes_setup = ratings['load_etypes']
self.qmc.load_etypes_setup = [int(let) for let in ratings['load_etypes']]
if 'presssure_percents' in ratings and len(ratings['presssure_percents']) == 4:
self.qmc.presssure_percents_setup = ratings['presssure_percents']
self.qmc.presssure_percents_setup = [bool(pp) for pp in ratings['presssure_percents']]
if 'loadevent_zeropcts' in ratings and len(ratings['loadevent_zeropcts']) == 4:
self.qmc.loadevent_zeropcts_setup = ratings['loadevent_zeropcts']
self.qmc.loadevent_zeropcts_setup = [int(lzp) for lzp in ratings['loadevent_zeropcts']]
if 'loadevent_hundpcts' in ratings and len(ratings['loadevent_hundpcts']) == 4:
self.qmc.loadevent_hundpcts_setup = ratings['loadevent_hundpcts']
self.qmc.loadevent_hundpcts_setup = [int(lhp) for lhp in ratings['loadevent_hundpcts']]
if 'preheatDuration' in ratings and len(ratings['preheatenergies']) == 1:
self.qmc.preheatDuration_setup = int(ratings['preheatDuration'][0])
if 'preheatenergies' in ratings and len(ratings['preheatenergies']) == 4:
self.qmc.preheatenergies_setup = [float(phe) for phe in ratings['preheatenergies']]
if 'betweenbatchDuration' in ratings and len(ratings['betweenbatchDuration']) == 1:
self.qmc.betweenbatchDuration_setup = int(ratings['betweenbatchDuration'][0])
if 'betweenbatchenergies' in ratings and len(ratings['betweenbatchenergies']) == 4:
self.qmc.betweenbatchenergies_setup = [float(bbe) for bbe in ratings['betweenbatchenergies']]
if 'coolingDuration' in ratings and len(ratings['coolingDuration']) == 1:
self.qmc.coolingDuration_setup = int(ratings['coolingDuration'][0])
if 'coolingenergies' in ratings and len(ratings['coolingenergies']) == 4:
self.qmc.coolingenergies_setup = [float(ce) for ce in ratings['coolingenergies']]

self.qmc.restoreEnergyLoadDefaults()
self.qmc.restoreEnergyProtocolDefaults()
self.sendmessage(QApplication.translate('Message','Energy loads configured for {0} {1}kg').format(label,self.qmc.roastersize_setup))
self.sendmessage(QApplication.translate('Message','Artisan configured for {0}').format(label))
_log.info('Artisan configured for %s',label)
Expand Down
2 changes: 1 addition & 1 deletion src/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ setuptools==70.3.0 # py2app fails on 71.0.3 and 71.0.4; pyinstaller windows/linu
wheel==0.45.1
pyserial==3.5
pymodbus==3.6.9; python_version < '3.9' # last Python 3.8 release
pymodbus==3.8.1; python_version >= '3.9'
pymodbus==3.8.2; python_version >= '3.9'
python-snap7==1.3; python_version < '3.10' # last Python 3.9 release
python-snap7==2.0.2; python_version >= '3.10'
Phidget22==1.22.20241209
Expand Down

0 comments on commit 81824ae

Please sign in to comment.