Hello,
In this code with arduino card I create differents files json (bouton3,bouton4,selecteur,joystick) and open this for write diferents values which are sended to arduino card to json.
import serial
import time
import json
ser = serial.Serial('COM5', 115200)
ser.timeout = 0.5 # valeur en secondes
ser.reset_input_buffer() # flushInput est dépréciée depuis PySerial 3.0
ser.reset_output_buffer() # idem pour flushOutput
etape = 0
while True:
try:
msg_recu = ser.readline().decode('utf8').rstrip().lower() # réception puis décodage puis retrait des '\r\n' (avec rstrip()) puis mise en minuscule)
except serial.SerialTimeoutException:
print("Erreur: Sortie par Timeout de lecture")
print(f"Pas de message reçu dans l'intervalle de temps indiqué ({ser.timeout}s)")
break
except serial.SerialException:
print("Erreur: Erreur d'E/S du port série")
break
else:
print((msg_recu))
if msg_recu.startswith("bouton3"):
etape = 1
if msg_recu.startswith("bouton4"):
etape = 2
elif msg_recu.startswith("selecteur"):
etape = 10
elif msg_recu.startswith("joystick"):
etape = 20
elif msg_recu == '': # on passe ici si la chaine est vide (correspond à un changement de type de message)
etape = 0 # ça évite de le tester à chaque type de message
elif etape == 1:
# on passe ici si on a eu le message "boutons poussoirs" et que l'on a reçu le 1er message suivant
bouton3 = str(msg_recu)
dictionnaire ={"bouton3": bouton3}
with open("bouton3.json", "w", encoding='utf-8') as l:
json.dump(dictionnaire, l)
elif etape == 2:
bouton4 = str(msg_recu)
dictio = {"bouton4":bouton4}
with open("bouton4.json","w", encoding= 'utf-8') as j:
json.dump(dictio,j)
elif etape == 10:
# on passe ici si on a eu le message "sélecteur" et une des valeurs
selecteur = int(msg_recu)
dico ={"selecteur": selecteur }
with open("selecteur.json", "w", encoding='utf-8') as p:
json.dump(dico, p)
# on traite la valeur reçue
elif etape == 20:
# on passe ici si on a eu le message "joystick" et une des valeurs
joystick = float(msg_recu)
my_dictionnaire ={"joystick": joystick}
with open("joystick.json","w",encoding= 'utf-8') as f:
json.dump(my_dictionnaire,f)
ser.close()
In this second code I want to load values of my arduino card to exceute differents actions in logiciel in depending on the value but on the Visual Components side it didn’t work and the values weren’t updating correctly so I stopped the project.
import time
import json
from opcua import ua, Server
if __name__ == "__main__":
# setup server
server = Server()
server.set_endpoint("opc.tcp://localhost:4841")
'''
server.set_security_policy([
ua.SecurityPolicyType.NoSecurity,
ua.SecurityPolicyType.Basic256Sha256_SignAndEncrypt,
ua.SecurityPolicyType.Basic256Sha256_Sign
])
'''
OFFSET_JOYSTICK = 50
# setup our own namespace, not really necessary but should as spec
uri = "http://localhost"
idx = server.register_namespace(uri)
# get Objects node, this is where we should put our nodes
objects = server.get_objects_node()
print(objects)
# populating our address space
MyObjects = objects.add_object(idx, "MyObjects")
button3 = MyObjects.add_variable(idx,"button3",int())
button4 = MyObjects.add_variable(idx,"button4",int())
joyst = MyObjects.add_variable(idx,"joyst",int())
select = MyObjects.add_variable(idx, "selector", int())
# starting!
server.start()
while True:
with open ("bouton3.json") as k:
bouton3 = json.load(k)
#print(bouton3)
#print(bouton3['bouton3'])
if bouton3 == 1:
ButtonSignal = True
button3.set_value(1)
else:
ButtonSignal = False
button3.set_value(0)
with open("bouton4.json") as p:
bouton4 = json.load(p)
#print(bouton4)
#print(bouton4['bouton4'])
if bouton4 == 1:
for i in range(1,4):
if i == 1:
SlowSpeed = True
FastSpeed = False
StartStopSpeed = False
button4.set_value(1)
if i == 2:
SlowSpeed = False
StartStopSpeed = False
FastSpeed = True
button4.set_value(1)
if i == 3:
FastSpeed = False
SlowSpeed = False
StartStopSpeed = True
button4.set_value(1)
if bouton4 == 0:
SlowSpeed = False
FastSpeed = False
StartStopSpeed = False
button4.set_value(0)
with open("selecteur.json") as f:
selecteur = json.load(f)
#print(selecteur)
#print(selecteur['selecteur'])
if selecteur == 1:
Vitesse_lente = True
select.set_value(1)
with open("joystick.json") as o:
joystick = json.load(o)
#print(joystick)
#print(joystick['joystick'])
if joystick >= 0 and joystick < 512:
joyst.set_value(joystick)
low_vitesse = 399 - 10
if joystick == 512:
joyst.set_value(joystick)
low_vitesse = 399
elif joystick > 512:
joyst.set_value(joystick)
low_vitesse = 200 + 10
if selecteur == 2:
Vitesse_normale = True
select.set_value(2)
with open("joystick.json") as l:
joystick = json.load(l)
#print(joystick)
#print(joystick['joystick'])
if joystick >= 0 and joystick < 512:
joyst.set_value(joystick)
normal_vitesse = 599 - 10
elif joystick == 512:
joyst.set_value(joystick)
normal_vitesse = 599
elif joystick > 512:
joyst.set_value(joystick)
normal_vitesse = 400 + 10
if selecteur == 3:
Vitesse_rapide = True
select.set_value(3)
with open("joystick.json") as r:
joystick = json.load(r)
#print(joystick)
#print(joystick['joystick'])
if joystick >= 0 and joystick < 512:
joyst.set_value(joystick)
fast_vitesse = 799 - 10
elif joystick == 512:
joyst.set_value(joystick)
fast_vitesse = 799
elif joystick > 512:
joyst.set_value(joystick)
fast_vitesse = 600 + 10
# Set MyVariable to be writable by clients
print(server.get_namespace_array())
try:
# count = 0
# while True:
# time.sleep(1)
# count += 0.1
# myvar.set_value(count)
input("Press Enter to shutdown server...\n")
finally:
# close connection, remove subcsriptions, etc
server.stop()