sunspec / pysunspec2

SunSpec Python library for interfacing with SunSpec devices.
Apache License 2.0
57 stars 21 forks source link

Using write function #45

Closed yuvalfro closed 2 years ago

yuvalfro commented 3 years ago

Hi, I'm trying to use the "write" function in SunSpecModbusClientDeviceTCP. I am having a tough time to understand what is the format of the 'data' parameter in the function. From the comment in the function: Parameters:

        addr :
            Starting Modbus address.

        count :
            Byte string containing register contents.

This is what I tried to do:

 d = client.SunSpecModbusClientDeviceTCP(slave_id=1, ipaddr=ip, ipport=port) 

 try:

    d.scan()

except Exception as e:

    print("Exception occured during Sunspec scan: ", e)

lst = [1,1]

byte_obj = bytes(lst)

d.write(d.DERCtlAC[0].model_addr+d.DERCtlAC[0].PFWInjEna.offset,byte_obj)

What is wrong in my code? Thanks!

GZPERRA commented 3 years ago

First of all make sure that your model DERCtlAC (DER AC controls model, id=704) is available on your target device. You can check that by printing the models dictionary print(d.models) and looking for two keys: 704 (model ID) and DERCtlAC (model name).

Now to change your value, it's recommended to do it this way (using the class Point):

# Enabling power factor when injecting active power.
d.DERCtlAC[0].PFWInjEna.value = 1 # The value has changed in the object
d.DERCtlAC[0].PFWInjEna.write() # The value has changed in the device

Hope it helps, although I'm late :/.

shelcrow commented 2 years ago

Please reopen if you still have an issue