Hallo
ich möchte beim Start von Doorpi eine Python Skript starten das eine while schleife hat . Das Skript muss permanent laufen das hierüber geklingelt und die Türe geöffnet wird.
Kann ich diese mit EVENT_OnStartup starten ?
Wie müsste die Config aussehen ?
[EVENT_OnStartup]10 = sleep:120 = os_execute:/home/doorpi/PLC_S7.py Ich würde gerne verschiedne Variable wie IP oder die DB Adresse "S7_DRead = [390,2,0]" in der Ini von Doorpi anlegen und diese dann an das Skript übergeben. Jedoch bin ich anfänger in Python und weiß nicht wie. Hier mal das Skript
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from time import sleep
import snap7
from snap7.util import*
import struct
import string
# Datenpfat virtual Keyboard
Dateipfad_imput1 = '/var/DoorPI/keyboard/inputs/e1'
Dateipfad_output1 = '/var/DoorPI/keyboard/outputs/a1'
# IP Adresse SPS
ip='192.168.178.30'
rack = 0
slot = 2
# Adresse bit lesen aus SPS
S7_DRead = [390,2,0]
# Adresse bit schreiben in SPS
S7_DWrite = [390,0,4]
def DRead(db_nr, byte_nr, bit_nr): #Lesen aus SPS
try:
plc = snap7.client.Client()
#print "Verbindungsaufbau"
plc.connect(ip,rack,slot)
#print "Lese Byte aus der SPS"
byte = plc.db_read(db_nr,byte_nr,1)
value = get_bool(byte,0,bit_nr)
#print "DRead", value
plc.disconnect()
con = plc.get_connected()
#print "disconnect", con
return value
except Exception as ex:
print ("Fehlerm beim lesen aus SPS",ex)
def DWrite(db_nr, byte_nr, bit_nr,value): #Schreiben in SPS
try:
plc = snap7.client.Client()
#print "Verbindungsaufbau"
plc.connect(ip,rack,slot)
#print "Ausertung on value True oder Flase"
if 'True' in value:
BOOL = True
else:
BOOL = False
#print "DWrite",value
#Variable deklariren
byte = bytearray(1)
#Lese Byte aus der SPS
byte = plc.db_read(db_nr,byte_nr,1)
#Setze Bit in Value
set_bool(byte,0,bit_nr,BOOL,)
#Schreibe Daten in SPS
plc.db_write(db_nr,byte_nr,byte)
plc.disconnect()
con = plc.get_connected()
#print "disconnected",(con)
except Exception as ex:
print ("Fehlerm beim schreiben in SPS",ex)
def Write_V_Keyboard (value):
try:
#print "Schreibe in Virtual_Keyboard"
f = open (Dateipfad_imput1 ,"w")
v =str(value)
f.write(v)
f.close()
except Exception as ex:
print ("Fehlerm beim schreiben in Virtual Keyboard",ex)
def Read_V_Keyboard():
try:
#print "Value aus Doorpi Virtual Keyboard lesen"
f = open (Dateipfad_output1,"r")
v = f.readline()
f.close()
return v
except Exception as ex:
print ("Fehlerm beim lesen aus Virtual Keyboard",ex)
#Hauptprogramm
while True:
Value1 = DRead(S7_DRead[0],S7_DRead[1],S7_DRead[2])
Write_V_Keyboard(Value1)
Value2 = Read_V_Keyboard()
sleep(0.1)
DWrite(S7_DWrite[0],S7_DWrite[1],S7_DWrite[2],Value2)
sleep(0.1)
Alles anzeigen