FreeOpcUa / python-opcua

LGPL Pure Python OPC-UA Client and Server
http://freeopcua.github.io/
GNU Lesser General Public License v3.0
1.35k stars 658 forks source link

Problem with set value in OPC UA S7-1200 #1504

Closed Cykorek closed 1 year ago

Cykorek commented 1 year ago

I can't write data to the S7 controller. I have the OPC server activated and using the python application I want to write data to one of the exposed variables. I am able to connect to the server and read the data but I can't write my own value. Please help. Below is the code:

from opcua import Client from opcua import ua

url = "opc.tcp://192.168.1.200:4840"

client = Client(url) client.connect()

print("Client connected")

adres1 = client.get_node("ns=4;i=2")

data1 = adres1.get_value()

adres1.set_value(20)

value = adres1.get_value()

print(data1)

if data1 == 4: print("Wartość to 4")

client.disconnect()

AndreasHeine commented 1 year ago

whats the error / debug log?

Cykorek commented 1 year ago

"The server does not support writing the combination of value, status and timestamps provided."(BadWriteNotSupported) File "C:\Users\Jarek\Desktop\Programy do OPC.vscode\Test.py", line 19, in adres1.set_value(20) opcua.ua.uaerrors._auto.BadWriteNotSupported: "The server does not support writing the combination of value, status and timestamps provided."(BadWriteNotSupported)

AndreasHeine commented 1 year ago

this is an example for asyncua but the issue is the same! you need to specifie the correct variant/datavalue ... python-opcua tries to guess what varianttype the value has, if you dont pass a Variant/DataValue-Class, which is not always correct e.g. if you pass 200 it could be an Int16 / 32 & 64 or UInt 16 / 32 / 64 ... and opcua is strict in types!

https://github.com/AndreasHeine/opcua-tutorial/blob/8f1dc98e8f2301514aff5fd83ea31fdb35c82029/client/write.py#L36

schroeder- commented 1 year ago

See #1159

Cykorek commented 1 year ago

Thanks for this code but I found another solution:

from opcua import Client from opcua import ua

url = "opc.tcp://192.168.1.200:4840" client = Client(url) client.connect() print("Client connected")

adres1 = client.get_node("ns=4;i=2")

new_value = ua.Variant(1000, ua.VariantType.Int16)

data_value = ua.DataValue(new_value)

adres1.set_value(data_value)

data1 = adres1.get_value() print(data1)

client.disconnect()>

Cykorek commented 1 year ago

The most important things to do:

AndreasHeine commented 1 year ago

setting up the server properly is indeed the first step 😉 understanding that python is dynamicaly typed and opcua strict the second one!