Skip to content

Read expected response size - #59

Merged
OrangeTux merged 8 commits into
AdvancedClimateSystems:masterfrom
mobilityhouse:read-expected-response-size
May 22, 2018
Merged

OrangeTux merged 8 commits into
AdvancedClimateSystems:masterfrom
mobilityhouse:read-expected-response-size

Conversation

@lutostag

Copy link
Copy Markdown
Contributor

This should fix #49

It works by checking exactly how many bytes are expected to be returned, and waiting until we receive that many... or a timeout (where we will receive fewer bytes than requested).

This was a problem with us either on slow serial devices (when setting up the connection... parity) or tcp connections that were slow to respond (as the recv call will bail out early sometimes if there is nothing already in the buffer).

I haven't tested manually, so using your travis to see what falls out and we will fix it up.

Thanks!

Greg Lutostanski added 3 commits May 16, 2018 18:58
First attempt at only reading the expected size of the modbus requests,
should block until we get the data.

Should fix AdvancedClimateSystems#49, and be a stepping stone towards AdvancedClimateSystems#39 of upstream.
If there is a timeout waiting for the response do raise a ValueError
rather than letting the parse_response_adu fail with bad errors.
@OrangeTux

Copy link
Copy Markdown
Collaborator

Many thanks for your PR! Later this weekend I'll look into it.

@lutostag

Copy link
Copy Markdown
Contributor Author

Appreciate it. I know how I am planning to fix up the tests. I should be able to do so tomorrow.

Also wanted to say a big thank you for putting out this library. We use it to talk to several different pieces of hardware and it is the most non-confrontational python library for modbus.

Thanks again!

lutostag added 2 commits May 18, 2018 19:05
And single writes (to not depend on input length, but just be static)
Need to send a real request (so we know how much data to wait for), use
a request that has more response data than the request (in terms of
bytes) to trigger when a Timeout should be detected properly.
@coveralls

coveralls commented May 19, 2018 •

Copy link
Copy Markdown

Coverage Status

Coverage increased (+0.2%) to 96.3% when pulling d0d47fe on mobilityhouse:read-expected-response-size into 02014a8 on AdvancedClimateSystems:master.

Works better with py2.7 and timeouts of underlying sockets.
@OrangeTux

OrangeTux commented May 20, 2018 •

Copy link
Copy Markdown
Collaborator

Great patch. I actually learned something new from the page you link in the docstring of recv_exactly.

But I've some placed some comments.

Comment thread tests/unit/client/serial/test_rtu.py Outdated
data.
"""
s = serial_for_url('loop://', timeout=0)
# as we are using a loop, we need the request will be read back as response

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you capitalize the first letter of the sentence and end the sentence with a dot?

@lutostag lutostag May 20, 2018 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, thing. If there is anything else that hits your eye, let me know. Open to any requested changes.

@lutostag

Copy link
Copy Markdown
Contributor Author

Fixed the comment, if there is anything else let me know. More than happy to make any further changes needed.

Thanks for being so quick with the review. :)

@OrangeTux

Copy link
Copy Markdown
Collaborator

I've added 2 other comments about handling expection responses. Have you seen them?

@lutostag

Copy link
Copy Markdown
Contributor Author

@OrangeTux, no I don't see them as comments against the diff. GitHub seems to drop them sometimes (if you comment as part of a review, but don't submit at the very bottom)

Comment thread umodbus/client/serial/rtu.py Outdated
if len(response) == 0:
raise ValueError
serial_port.flush()
response = recv_exactly(serial_port.read, expected_response_size)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would fail if the server responds with a Modbus exception code. In this case the PDU is only 5 bytes long. See http://www.simplymodbus.ca/exceptions.htm
Could you update the code so that this case is handled?

Comment thread umodbus/client/tcp.py Outdated
sock.sendall(adu)
expected_response_size = \
expected_response_pdu_size_from_request_pdu(adu[7:]) + 7
response = recv_exactly(sock.recv, expected_response_size)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This edit has the same problem as the RTU client. It fails when the server responds with an expection code. The PDU is in this case 9 bytes, but recv_exactly is expecting more. Could you edit the code so this case is handled?

@OrangeTux

Copy link
Copy Markdown
Collaborator

I think my comments should be visible now.

For both RTU and TCP client cases. The exception ADU is only 2 bytes
(shorter than all the other ADUs), so we can read those bytes first,
check that there is no error, recv the rest of the expected ADU size,
and concatenate the two for further parsing into the correct response.
@lutostag

Copy link
Copy Markdown
Contributor Author

Indeed, thanks for checking that, I didn't know about the error handling.

The code is a bit more verbose now on the send_message side, but I think I made it as clear as possible. If you have a better idea on how to make it easier to read, I can take another attempt at it.

Hope that it covers your concerns.

Thanks for the eagle-eyed review!

@OrangeTux
OrangeTux merged commit 96cbb94 into AdvancedClimateSystems:master May 22, 2018
@OrangeTux

Copy link
Copy Markdown
Collaborator

I've merged your changes into 1.0.1. Note that 1.0.1 isn't uploaded to Pypi. I forgot to update the version number in setup.py. Therefore I created a new release, 1.0.2, which is fixed this. 1.0.2 is on Pypi.

Again. thanks for your effort.

hmaerki pushed a commit to hmaerki/obsolete-fork-uModbus that referenced this pull request Dec 31, 2023
…gfix/add-missing-issues-template-and-rp2-rtu-usage-examples

Add missing issues template and RP2 RTU usage examples
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

rtu.send_message should not read more bytes than needed.

3 participants