14:21, EEST
August 19, 2024
I’m having trouble connecting a string to the server. When it was as a Boolean I was able to connect with this code, but now I can’t anymore and it displays this error:
Failed to write the value to NO ns=3;i=1008: “The value supplied for the attribute is not of the same type as the attribute”s value.”(BadTypeMismatch)
Below is the configuration link. I’m trying to connect it to the Process Simulate software.
code:
from opcua import Client
from opcua import ua # Para garantir o tipo correto de dado
import time
# Configuração do OPC UA
opcua_url = “opc.tcp://DESKTOP-RVOVK3R:53530/OPCUA/SimulationServer”
opcua_client = Client(opcua_url)
# Função para se conectar ao servidor OPC UA
def connect_opcua():
try:
opcua_client.connect()
print(“Conectado ao servidor OPC UA”)
except Exception as e:
print(“Falha ao conectar ao servidor OPC UA: “, e)
return False
return True
# Função para escrever um valor em um nó, garantindo que o valor seja uma string
def write_value(node_id, value):
try:
node = opcua_client.get_node(node_id)
# Convertemos explicitamente o valor para string e definimos o tipo Variant como String
value_str = ua.Variant(str(value), ua.VariantType.String)
node.set_value(value_str)
print(f”Valor ‘{value}’ escrito no NO {node_id}”)
except Exception as e:
print(f”Falha ao escrever o valor no NO {node_id}: {e}”)
# IDs dos nós que você quer alterar
node_id_1 = “ns=3;i=1001” # Exemplo de ID do nó 1 (string)
if connect_opcua():
# Definir valores iniciais como strings
write_value(node_id_1, “False”) # Escreve como string
while True:
# Alterna os valores entre “True” e “False”
write_value(node_id_1, “True”)
time.sleep(2)
write_value(node_id_1, “False”)
time.sleep(2)
10:00, EEST
Moderators
February 11, 2020
Hello,
First, your version of Simulation Server seems very old since it is missing the PubSub-tab and still enables selecting Expression as Value Type for simulations. It is recommended to update to latest version, which currently is 5.5.0.
Second, you seem to be using the Initial Value Nodes in Process Simulation. Those Nodes only have the initial value of the Node and not its current Value. Thus, you should not use those Nodes for anything.
Third, you have changed the DataType of the Nodes in Simulation Server from Boolean to String in the video, but attempt to write Boolean values to those Nodes in your code. There is a mismatch between the DataTypes and thus getting BadTypeMismatch is to be expected.
Most Users Ever Online: 1919
Currently Online:
15 Guest(s)
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Heikki Tahvanainen: 402
hbrackel: 144
rocket science: 86
pramanj: 86
Francesco Zambon: 83
Ibrahim: 78
Sabari: 62
kapsl: 57
gjevremovic: 49
Xavier: 43
Member Stats:
Guest Posters: 0
Members: 732
Moderators: 7
Admins: 1
Forum Stats:
Groups: 3
Forums: 15
Topics: 1508
Posts: 6396
Newest Members:
elainesever, powhephenry, mamiecaldwell5, Lamasom, scsneed, berrybulcock, icerdraizomma, athenasummy5, vtaletbhcx, HaroldaDobModerators: Jouni Aro: 1019, Pyry: 1, Petri: 0, Bjarne Boström: 1016, Jimmy Ni: 26, Matti Siponen: 340, Lusetti: 0
Administrators: admin: 1