SmartGridready / SGrPython

Development of SGr PyPi package
Other
1 stars 1 forks source link

Modbus Client write error #32

Closed d1msk1y closed 3 months ago

d1msk1y commented 3 months ago

Description

We tried to implement the write/set functionality in the intermediary, however, the following line of code fails to write the values to the register. After numerous attempts to solve the issue on our own, we still couldn't succeed. We had also applied @robin-schoch's changes, which didn't make any difference as well.

Current code and its output:

# Code
builder = PayloadBuilder(byteorder=order, wordorder=order)
print(f"Value to be written: {value} {data_type}")
builder.encode(value, data_type, rounding=RoundingScheme.floor)
print("PayloadBuilder state before writing to registers:", builder)
self.client.write_registers(address=addr, values=builder.to_registers(), unit=slave_id)

# Output:
Setting value for 1.EMS_Current_Limit.EMSCurrentLimit: 5.9
Value to be written: 5.9 float32
ERROR:root:An unexpected error occurred while writing value to register: PayloadBuilder.encode() missing 3 required positional arguments: 'value', 'modbus_type', and 'rounding'
Traceback (most recent call last):
  File "/home/dmytro/documents/projects/SGrIntermediary/sgr/sgr_library/modbus_client.py", line 107, in value_encoder
    print("PayloadBuilder state before writing to registers:", builder)
  File "/home/dmytro/documents/projects/SGrIntermediary/venv/lib/python3.11/site-packages/pymodbus/payload.py", line 91, in __str__
    return self.encode().decode("utf-8")
           ^^^^^^^^^^^^^
TypeError: PayloadBuilder.encode() missing 3 required positional arguments: 'value', 'modbus_type', and 'rounding'

Link to the line in the latest version on github:

https://github.com/SmartGridready/SGrPython/blob/71f51ed8f5d92a6c5212029bec74d214be8e2f73/sgr_library/modbus_client.py#L103

How to reproduce

  1. Take an example python script that will use the issued functionality

or

  1. Clone the intermediary at the branch sgr-write
  2. Run the intermediary python main.py (Venv used) (instructions)
  3. Use the postman collection to make requests 3.1 Create an instance with the Create Instance request 3.2 Write it with Write Instance Data
  4. See the error output

The usage of the SGrPython write in the set endpoint: https://github.com/SmartGridready/SGrIntermediary/blob/6c81bf994d4aec57ce24b976cd9046d6f2ca615a/main.py#L173

Postman collection: SGrIntermediary.postman_collection.json