work via new wb-mqtt-serial rpc#25
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the Modbus client to use a new wb-mqtt-serial RPC API that has native Modbus protocol support, providing performance improvements and adding RTU-over-TCP mode support.
- Replaces direct umodbus library calls with wb-mqtt-serial RPC API calls that understand Modbus protocol
- Adds support for RTU-over-TCP mode (transparent serial-TCP bridge)
- Simplifies message creation and parsing by leveraging the RPC service's Modbus knowledge
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| modbus_client_rpc/main.py | Core refactoring to use new RPC API with native Modbus support, added RTU-over-TCP mode |
| tests/test_modbus_client.py | Updated test cases to match new function signatures and RPC payload format |
| debian/changelog | Version bump to 1.3.0 documenting the new RPC integration and RTU-over-TCP feature |
Comments suppressed due to low confidence (1)
tests/test_modbus_client.py:95
- The comment mentions reading 125 coils is allowed, but the test case uses 126. This inconsistency could indicate either an incorrect test case or an inaccurate comment.
(0x01, True, 0, 126, [], "", 126, False), # it's fun, but reading 125 *coils* is actually allowed
| data = [] | ||
| for byte in response_byte: | ||
| for bit in range(8): | ||
| data.append((byte >> bit) & 1) | ||
| if len(data) >= register_count: | ||
| break |
There was a problem hiding this comment.
The bit extraction logic may read more bits than requested. When register_count is reached, the outer loop should also break to avoid processing additional bytes.
| data = [] | |
| for byte in response_byte: | |
| for bit in range(8): | |
| data.append((byte >> bit) & 1) | |
| if len(data) >= register_count: | |
| break | |
| data = [] | |
| done = False | |
| for byte in response_byte: | |
| for bit in range(8): | |
| data.append((byte >> bit) & 1) | |
| if len(data) >= register_count: | |
| done = True | |
| break | |
| if done: | |
| break |
| print("SUCCESS: written 1 element") | ||
| else: | ||
| print("SUCCESS: Coils/Registers written:", data) | ||
| print("SUCCESS: Coils/Registers written:", "?") |
There was a problem hiding this comment.
The hardcoded "?" placeholder should be replaced with the actual count of written coils/registers, which can be determined from the register_count parameter.
| print("SUCCESS: Coils/Registers written:", "?") | |
| print("SUCCESS: Coils/Registers written:", register_count) |
|
ну может и год повисеть, чего уж тут |
Что происходит; кому и зачем нужно:
Работаем через "новый" API wb-mqtt-serial Port/Load, чтобы он знал про протокол Modbus
Что поменялось для пользователей:
Стало быстрее. Собственно главная причина поддержки модбаса в Port/Load была в том, что можно хитро читать из порта, т.к. мы знаем сколько байтиков мы ожидаем в ответе.
Ну, ещё добавился режим RTU-over-TCP нахаляву (справедливости ради, он и без нового RPC бы добавлялся в две строчки), и в хелп немножко добавил полезного.
Как проверял/а:
Запускал с обычным wb-mqtt-serial, с патченным (см. свежий PR туда). Читал через него с mbgate и с обычных настоящих устройств.