Avatar

Please consider registering
guest

sp_LogInOut Log In sp_Registration Register

Register | Lost password?
Advanced Search

— Forum Scope —




— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

sp_Feed Topic RSS sp_TopicIcon
Problems connecting a string to the server
September 28, 2024
14:21, EEST
Avatar
rvp
Member
Members
Forum Posts: 4
Member Since:
August 19, 2024
sp_UserOfflineSmall Offline

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.

https://cefsaedu-my.sharepoint.com/:v:/g/personal/062200020_faculdade_cefsa_edu_br/EfvVOHl3JVZGt9QbZses1AkBljTYbtaCjwD6QD0EK3_EZA?nav=eyJyZWZlcnJhbEluZm8iOnsicmVmZXJyYWxBcHAiOiJTdHJlYW1XZWJBcHAiLCJyZWZlcnJhbFZpZXciOiJTaGFyZURpYWxvZy1MaW5rIiwicmVmZXJyYWxBcHBQbGF0Zm9ybSI6IldlYiIsInJlZmVycmFsTW9kZSI6InZpZXcifX0%3D&e=QLzxBk

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)

September 30, 2024
10:00, EEST
Avatar
Matti Siponen
Moderator
Members

Moderators
Forum Posts: 349
Member Since:
February 11, 2020
sp_UserOfflineSmall Offline

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.

Forum Timezone: Europe/Helsinki

Most Users Ever Online: 1919

Currently Online:
11 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

Heikki Tahvanainen: 402

hbrackel: 144

rocket science: 88

pramanj: 86

Francesco Zambon: 83

Ibrahim: 78

Sabari: 62

kapsl: 57

gjevremovic: 49

Xavier: 43

Member Stats:

Guest Posters: 0

Members: 726

Moderators: 7

Admins: 1

Forum Stats:

Groups: 3

Forums: 15

Topics: 1529

Posts: 6471

Newest Members:

gabriellabachus, Deakin, KTP25Zof, Wojciech Kubala, efrennowell431, wilfredostuart, caitlynfajardo, jeromechubb7, franciscagrimwad, adult_gallery

Moderators: Jouni Aro: 1026, Pyry: 1, Petri: 0, Bjarne Boström: 1032, Jimmy Ni: 26, Matti Siponen: 349, Lusetti: 0

Administrators: admin: 1