Menu

Home

Frank Benkert

HowTo

(english version below...)

Hier eine kurze Anleitung wie man das Python-Modul einsetzen kann.

Voraussetzungen

  • eine CoDeSys 2.3 Steuerung (z.B. eine WAGO)
  • ein Computer (Raspberry Pi, Fritz!Box, PC oder sonstwas) auf dem Python 2.7 läuft
  • der Wille Daten / Messwerte von diesem Computer nach CoDeSys zu bringen
  • die Geräte müssen per Netzwerk miteinander verbunden sein

Beispiel

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.

Konfiguration CoDeSys

Zielsystemeinstellungen

Zielsystemeinstellungen

Anlegen der Variablen

Globgale Variablen hinzufügen:

Variablen hinzufügen

Netzwerkkonfiguration:

Variablen Netzwerkkonfiguration

Jetzt ein paar Variablen eingeben:

Variablen eingeben

Variablen exportieren:

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 :-)

Configuration Raspberry Pi

# 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.

HowTo (english)

Here a short instruction how to use the Python module.

Prerequisites

  • a CoDeSys 2.3 controller (e.g. a WAGO)
  • a computer (Raspberry Pi, Fritz!Box, PC or whatever) running Python 2.7
  • the need to transfer data / measured values from this computer to CoDeSys
  • the devices must be connected to each other via network

Example

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.

Configuration CoDeSys

Target Settings

Zielsystemeinstellungen

Create variables

Add global variables:

Variablen hinzufügen

Configure Network:

Variablen Netzwerkkonfiguration

Add a few variables:

Variablen eingeben

Export variables:

Variablen exportieren

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 :-)

Konfigfuration Raspberry Pi

# 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.


MongoDB Logo MongoDB