Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove code to set X address (digital inputs can't be set) #5

Merged
merged 1 commit into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions clickplc/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,34 +367,6 @@ async def _get_ctd(self, start: int, end: int) -> dict:
return decoder.decode_32bit_int()
return {f'ctd{n}': decoder.decode_32bit_int() for n in range(start, end + 1)}

async def _set_x(self, start: int, data: list[bool] | bool):
"""Set X addresses. Called by `set`.

For more information on the quirks of X coils, read the `_get_x`
docstring.
"""
if start % 100 == 0 or start % 100 > 16:
raise ValueError('X start address must be *01-*16.')
if start < 1 or start > 816:
raise ValueError('X start address must be in [001, 816].')
coil = 32 * (start // 100) + start % 100 - 1

if isinstance(data, list):
if len(data) > 16 * (9 - start // 100) - start % 100:
raise ValueError('Data list longer than available addresses.')
payload = []
if (start % 100) + len(data) > 16:
i = 17 - (start % 100)
payload += data[:i] + [False] * 16
data = data[i:]
while len(data) > 16:
payload += data[:16] + [False] * 16
data = data[16:]
payload += data
await self.write_coils(coil, payload)
else:
await self.write_coil(coil, data)

async def _set_y(self, start: int, data: list[bool] | bool):
"""Set Y addresses. Called by `set`.

Expand Down
29 changes: 16 additions & 13 deletions clickplc/tests/test_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,13 @@ async def test_tagged_driver(tagged_driver, expected_tags):


@pytest.mark.asyncio
@pytest.mark.parametrize('prefix', ['x', 'y'])
async def test_bool_roundtrip(plc_driver, prefix):
"""Confirm x and y bools are read back correctly after being set."""
await plc_driver.set(f'{prefix}2', True)
await plc_driver.set(f'{prefix}3', [False, True])
expected = {f'{prefix}001': False, f'{prefix}002': True, f'{prefix}003': False,
f'{prefix}004': True, f'{prefix}005': False}
assert expected == await plc_driver.get(f'{prefix}1-{prefix}5')
async def test_y_roundtrip(plc_driver):
"""Confirm y (output bools) are read back correctly after being set."""
await plc_driver.set('y2', True)
await plc_driver.set('y3', [False, True])
expected = {'y001': False, 'y002': True, 'y003': False,
'y004': True, 'y005': False}
assert expected == await plc_driver.get('y1-y5')


@pytest.mark.asyncio
Expand Down Expand Up @@ -146,8 +145,8 @@ async def test_set_error_handling(plc_driver):

@pytest.mark.asyncio
@pytest.mark.parametrize('prefix', ['x', 'y'])
async def test_xy_error_handling(plc_driver, prefix):
"""Ensure errors are handled for invalid requests of x and y registers."""
async def test_get_xy_error_handling(plc_driver, prefix):
"""Ensure errors are handled for invalid get requests of x and y registers."""
with pytest.raises(ValueError, match=r'address must be \*01-\*16.'):
await plc_driver.get(f'{prefix}17')
with pytest.raises(ValueError, match=r'address must be in \[001, 816\].'):
Expand All @@ -156,12 +155,16 @@ async def test_xy_error_handling(plc_driver, prefix):
await plc_driver.get(f'{prefix}1-{prefix}17')
with pytest.raises(ValueError, match=r'address must be in \[001, 816\].'):
await plc_driver.get(f'{prefix}1-{prefix}1001')

@pytest.mark.asyncio
async def test_set_y_error_handling(plc_driver):
"""Ensure errors are handled for invalid set requests of y registers."""
with pytest.raises(ValueError, match=r'address must be \*01-\*16.'):
await plc_driver.set(f'{prefix}17', True)
await plc_driver.set('y17', True)
with pytest.raises(ValueError, match=r'address must be in \[001, 816\].'):
await plc_driver.set(f'{prefix}1001', True)
await plc_driver.set('y1001', True)
with pytest.raises(ValueError, match=r'Data list longer than available addresses.'):
await plc_driver.set(f'{prefix}816', [True, True])
await plc_driver.set('y816', [True, True])


@pytest.mark.asyncio
Expand Down