(english version below...)
Hier eine kurze Anleitung wie man das Python-Modul einsetzen kann.
Für unser Szenario gehen wir davon aus, dass wir eine Steuerung mit CoDeSys 2.3 und einen Raspberry Pi haben.
Auf dem Raspberry Pi soll ein Datenpaket an die Steuerung gesendet werden. In meinem Fall wird zyklisch über einen Ultraschallsensor eine Entfernung ermittelt und zur Steuerung gesendet. Das Ganze dient der Füllstandsmessung einer Zisterne - der Ultraschallsensor misst die Entfernung zur Wasseroberfläche.
Hier wollen wir aber nur den eigentlichen Sendevorgang programmieren.
Die IP-Adressen unserer Targets spielen keine Rolle! Wir konfigurieren sogenanntes Multicast um die anderen Netzwerkteilnehmer nicht zu belästigen.

Globgale Variablen hinzufügen:

Netzwerkkonfiguration:

Jetzt ein paar Variablen eingeben:

Variablen exportieren:

z.B in die Datei "NET01RX.EXP".
Jetzt natürlich Programm kompilieren, laden und starten.
PS: Falls wir ein leeres Programm laden wollen (als Test) sollten wir nicht vergessen im (noch leeren) Programmcode der PLC_PRG einen Strichpunkt zu setzen.
Dann ist er nicht mehr leer :-)
# Entpacken des pyNETVARS Beispiels
unzip pyNETVARS-r0.zip
cd pyNETVARS
ls
Wenn wir nun aber ein eigenes Projekt anlegen wollen...
# eigenes Projekt anlegen...
cd ..
mkdir myproject
cd myproject
# Modul holen
cp ../pyNETVARS/netvars.py .
# unsere exportierten NET01RX.EXP holen
cp /[vonwoauchimmer]/NET01RX.EXP .
Jetzt ein Python-Skript anlegen mit ungefähr diesem Inhalt:
from netvars import NetVars
localIf = ''
mcastGrp = '225.10.10.10'
mcastPort = 1202
netVars1 = NetVars( 1, 'UDP', mcastGrp, mcastPort, localIf )
netVars1.varsFromExpFile( './NET01RX.EXP' )
netVars1.startSender()
# bei z.B. 63,62cm Entfernung wären das...
netVars1.latestValues['NetVar01_Counter'] = 1
netVars1.latestValues['NetVar01_ZisterneAbstandZoll'] = 25
netVars1.latestValues['NetVar01_ZisterneAbstandZentimeter'] = 64
netVars1.latestValues['NetVar01_ZisterneAbstandMikrosekunden'] = 3712
netVars1.sendValues()
Diese Werte sollten wir nun in unserem laufenden CoDeSys-Programm sehen.
Here a short instruction how to use the Python module.
For our scenario we assume that we have a controller with CoDeSys 2.3 and a Raspberry Pi.
A data packet is to be sent to the controller from the Raspberry Pi. In my case, a distance is determined cyclically via an ultrasonic sensor and sent to the controller. The whole system is used to measure the level of a cistern - the ultrasonic sensor measures the distance to the water surface.
Here, however, we only want to program the actual transmission process.
The IP addresses of our targets are not important! We configure so-called Multicast in order not to disturb the other network participants.

Add global variables:

Configure Network:

Add a few variables:

Export variables:

e.g to the file "NET01RX.EXP".
Now of course compile, load and start the program.
PS: If we want to load an empty program (as a test) we should not forget to set a semicolon in the (still empty) program code of PLC_PRG.
Then it's not empty anymore :-)
# Unpacking the pyNETVARS example
unzip pyNETVARS-r0.zip
cd pyNETVARS
ls
But if we want to create our own project...
# create your own project...
cd ..
mkdir myproject
cd myproject
# get the module
cp ../pyNETVARS/netvars.py .
# get the exported NET01RX.EXP
cp /[whereever]/NET01RX.EXP .
Now create a Python script with about this content:
from netvars import NetVars
localIf = ''
mcastGrp = '225.10.10.10'
mcastPort = 1202
netVars1 = NetVars( 1, 'UDP', mcastGrp, mcastPort, localIf )
netVars1.varsFromExpFile( './NET01RX.EXP' )
netVars1.startSender()
# e.g. at 63,62cm distance...
netVars1.latestValues['NetVar01_Counter'] = 1
netVars1.latestValues['NetVar01_ZisterneAbstandZoll'] = 25
netVars1.latestValues['NetVar01_ZisterneAbstandZentimeter'] = 64
netVars1.latestValues['NetVar01_ZisterneAbstandMikrosekunden'] = 3712
netVars1.sendValues()
We should now see these values in our current CoDeSys program.