IOB-PI v 2.5.3: update linguaggio x esecuzione su raspberry 8 bit con sintassi Raspbian 12 e python 3.11

This commit is contained in:
Samuele Locatelli
2025-04-17 19:08:21 +02:00
parent af9b889be5
commit 0bbcd78de3
2 changed files with 100 additions and 84 deletions
+50 -42
View File
@@ -1,7 +1,7 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# readParallela v. 2.5.2 12 Ingressi
# readParallela v. 2.5.3 12 Ingressi
# - single instance timer
# - invio multiplo x send eventi accodati
# - gestione segnali BLINKING
@@ -13,6 +13,7 @@
# - (2.5) Fix (hope) ciclo "wait send to complete", gestione timeout (rety infinito se IO riparte in modo anomalo)
# - (2.5.1) Fix numero versione 18.05.2023
# - (2.5.2) Fix gestione eccezioni con report dettagliato
# - (2.5.3) Fix gestione stringhe e print x python 3.11 in debian 12 / raspberry OS 2025
#---------------------------------------------------------------
# levare locking
@@ -27,13 +28,16 @@ import sys
from datetime import datetime
import urllib
import ConfigParser
import urllib.request
import configparser
#import ConfigParser
import os, sys
import logging
import logging.handlers
import threading
import Queue
import queue
#import Queue
from array import *
@@ -47,7 +51,7 @@ MAXRETRY = 10
# numero campioni filtraggio segnale ballerino
MAX_COUNTER_BLINK = 10
PROGRAM_NAME ="ReadPar IOB-pi v.2.5.2"
PROGRAM_NAME ="ReadPar IOB-pi v.2.5.3"
# DA FILE CONF
idxMacchina = "1001"
@@ -113,9 +117,10 @@ i_filter_counters = array ( 'i',[0,0,0,0,0,0,0,0,0,0,0,0])
#--------------------------------------------------------------
# Gestione coda (condivisa) x registrazione eventi ed invio URL
#print "Creazione coda illimitata"
#print ("Creazione coda illimitata")
Coda = Queue.Queue(0)
Coda = queue.Queue(0)
#Coda = Queue.Queue(0)
#queueLock = threading.Lock()
@@ -214,7 +219,7 @@ def readParallelaFiltrata():
#ciclo per ogni segnale
for i in xrange(12) :
for i in range(12) :
#print (i)
# v2.1 gestione inversione bit ingresso
@@ -235,12 +240,12 @@ def readParallelaFiltrata():
# vero fronte 0 -> 1
i_filter_counters[i] = MAX_COUNTER_FILTER
B_temp[i] = 0 # tengo l' ingresso a 0
#logPro.info("START spike 0->1 on bit " + `i` )
#logPro.info("START spike 0->1 on bit " + str(i) )
else :
# fine disturbo breve di uno stato 1
i_filter_counters[i] = 0
B_temp[i] = 1 # tengo l' ingresso a 1
logPro.info("END spike 0->1 on bit " + `i` )
logPro.info("END spike 0->1 on bit " + str(i) )
# stabile 1 -> 1
if ( B_input[i] == 1 ) and ( B_filter_prev [i] == 1 ) :
@@ -258,12 +263,12 @@ def readParallelaFiltrata():
# vero fronte 1 -> 0
i_filter_counters[i] = MAX_COUNTER_FILTER
B_temp[i] = 1 # tengo l' ingresso a 1
#logPro.info("START spike 1->0 on bit " + `i` )
#logPro.info("START spike 1->0 on bit " + str(i) )
else :
# fine disturbo breve di uno stato 0
i_filter_counters[i] = 0
B_temp[i] = 0 # tengo l' ingresso a 0
logPro.info("END spike 1->0 on bit " + `i` )
logPro.info("END spike 1->0 on bit " + str(i) )
# stabile 0 -> 0
if ( B_input[i] == 0 ) and ( B_filter_prev [i] == 0 ) :
@@ -296,7 +301,7 @@ def readParallelaFiltrata():
i_counters[i] = MAX_COUNTER_BLINK
#else :
# # loggo che ho rilevato un blink...
# logPro.info("Blink down on bit " + `i`)
# logPro.info("Blink down on bit " + str(i))
else:
# no , segnale eguale a prima
# se input a 0
@@ -306,7 +311,7 @@ def readParallelaFiltrata():
i_counters[i] = i_counters[i] -1
if ( i_counters[i] == 0 ) :
B_output[i] = 0
logPro.info("END Blink on bit " + `i` )
logPro.info("END Blink on bit " + str(i) )
#Rimettiamo insieme i bit
new_value = 0
@@ -341,8 +346,8 @@ def readParallelaFiltrata():
current = hex( new_value ).replace ( "0x" , "" ).upper()
except Exception as e:
print "Errore in readParallelaFiltrata \n\n"
print str(e)
print ("Errore in readParallelaFiltrata \n\n")
print (str(e))
pass
return current
@@ -356,8 +361,9 @@ def accoda():
dtEve = datetime.utcnow().strftime('%Y%m%d%H%M%S%f')[:-3]
Coda.put(dtEve + '#' + value + '#' + cont)
except Queue.Full:
logPro.error( "Queue full" + `dtEve` + '#' + `value` + '#' + `cont` )
#except Queue.Full:
except queue.Full:
logPro.error( "Queue full" + str(dtEve) + '#' + str(value) + '#' + str(cont) )
except Exception as e:
logPro.error( "NETWORK:Errore http-no com rete-timeout" + url )
logPro.error(str(e))
@@ -371,21 +377,22 @@ def svuota_coda():
global timer_busy
global NMAXSEND
#print "start timer "
#print ("start timer ")
if ( timer_busy == False ):
timer_busy = True
#print "start timer ok "
#print ("start timer ok ")
try:
if not Coda.empty():
#print "coda da svuotare!"
response = urllib.urlopen(URLALIVE)
answ = response.read()
#print ("coda da svuotare!")
response = urllib.request.urlopen(URLALIVE)
answ = response.read().decode('utf-8')
#print(answ)
if answ == 'OK':
#print "OK alive"
response2 = urllib.urlopen(URLENABLED + idxMacchina)
answ2 = response2.read()
#print ("OK alive")
response2 = urllib.request.urlopen(URLENABLED + idxMacchina)
answ2 = response2.read().decode('utf-8')
if answ2 == 'OK':
# aggiorno stato ad online
if onLine == '0':
@@ -431,13 +438,13 @@ def svuota_coda():
url = URLBASE + idxMacchina + URLADV1 + value
url = url + '&dtCurr=' + dtCurr + '&dtEve=' + dtEve + '&cnt=' + cnt
# CHIAMO URL
response3 = urllib.urlopen ( url )
answ3 = response3.read()
response3 = urllib.request.urlopen ( url )
answ3 = response3.read().decode('utf-8')
#print(url)
# log valore inviato!
logSnd.info( value + ' ['+ cnt +']' + ' R:' + answ3 )
#print "Valore smaltito dalla coda"
#print ("Valore smaltito dalla coda")
# tolgo 1 al contatore
i -= 1
@@ -463,17 +470,17 @@ def svuota_coda():
if onLine == '1':
logPro.error("Server Non raggiungibile")
logPro.error(str(e))
#print "Non raggiungibile"
#print ("Non raggiungibile")
onLine = '0'
# in ogni caso
timer_busy = False
#print "end timer ok"
#print ("end timer ok")
#print "end timer "
#print ("end timer ")
#---------------------------------------------------------------
@@ -547,7 +554,7 @@ def avviaParallela():
except Exception as e:
print( "\n\n" + PROGRAM_NAME + " - Error 3 on RPi.GPIO ! \n\n")
print str(e)
print (str(e))
sys.exit(1)
@@ -559,7 +566,8 @@ def avviaParallela():
# MAIN
try:
config = ConfigParser.RawConfigParser()
#config = ConfigParser.RawConfigParser()
config = configparser.RawConfigParser()
config.read ( 'IOB.cfg' )
SAMPLETIME = config.getfloat ( 'time' , 'SAMPLETIME' )
@@ -628,8 +636,8 @@ try:
except Exception as e:
print "\n\n" + PROGRAM_NAME + ' - Error 4 - in config file ' 'IOB.cfg'
print str(e)
print ("\n\n" + PROGRAM_NAME + ' - Error 4 - in config file ' 'IOB.cfg')
print (str(e))
sys.exit(1)
#--------------------------------------------
@@ -650,14 +658,14 @@ try:
except Exception as e:
# manda mail o simili - FARE!!!
print "LOG: Impossibile creare file log con nome"
print ("LOG: Impossibile creare file log con nome")
print (LOGFILE)
print "\n\n"
print str(e)
print ("\n\n")
print (str(e))
#--------------------------------------------
print "\n\n" + PROGRAM_NAME + "\n\n"
print ("\n\n" + PROGRAM_NAME + "\n\n")
global startstatus
startstatus = 1
@@ -709,7 +717,7 @@ except RuntimeError:
print( "\n\n" + PROGRAM_NAME + " - Error 1 - you need superuser privileges")
except Exception as e:
print( "\n\n" + PROGRAM_NAME + " - Error 2 - you need superuser privileges. USE 'sudo' to run your script\n\n")
print str(e)
print (str(e))
sys.exit(1)
@@ -720,7 +728,7 @@ avviaParallela()
# MARCO: qui inserire avvio thread di "svuotaCoda"
# avviaSvuotaCoda
#print "Avvia svuota coda"
#print ("Avvia svuota coda")
do_every ( SENDURLTIME , svuota_coda );
@@ -729,7 +737,7 @@ do_every ( SENDURLTIME , svuota_coda );
old = ''
#print "Avvio ciclo"
#print ("Avvio ciclo")
logPro.info("Avvio loop principale")
while 1:
+50 -42
View File
@@ -1,7 +1,7 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# readParallela v. 2.5.2 8 Ingressi
# readParallela v. 2.5.3 8 Ingressi
# - single instance timer
# - invio multiplo x send eventi accodati
# - gestione segnali BLINKING
@@ -13,6 +13,7 @@
# - (2.5) Fix (hope) ciclo "wait send to complete", gestione timeout (rety infinito se IO riparte in modo anomalo)
# - (2.5.1) Fix numero versione 18.05.2023
# - (2.5.2) Fix gestione eccezioni con report dettagliato
# - (2.5.3) Fix gestione stringhe e print x python 3.11 in debian 12 / raspberry OS 2025
#---------------------------------------------------------------
# levare locking
@@ -27,13 +28,16 @@ import sys
from datetime import datetime
import urllib
import ConfigParser
import urllib.request
import configparser
#import ConfigParser
import os, sys
import logging
import logging.handlers
import threading
import Queue
import queue
#import Queue
from array import *
@@ -47,7 +51,7 @@ MAXRETRY = 10
# numero campioni filtraggio segnale ballerino
MAX_COUNTER_BLINK = 10
PROGRAM_NAME ="ReadPar IOB-pi v.2.5.2"
PROGRAM_NAME ="ReadPar IOB-pi v.2.5.3"
# DA FILE CONF
idxMacchina = "1001"
@@ -109,9 +113,10 @@ i_filter_counters = array ( 'i',[0,0,0,0,0,0,0,0])
#--------------------------------------------------------------
# Gestione coda (condivisa) x registrazione eventi ed invio URL
#print "Creazione coda illimitata"
#print ("Creazione coda illimitata")
Coda = Queue.Queue(0)
Coda = queue.Queue(0)
#Coda = Queue.Queue(0)
#queueLock = threading.Lock()
@@ -180,7 +185,7 @@ def readParallelaFiltrata():
#ciclo per ogni segnale
for i in xrange(8) :
for i in range(8) :
#print (i)
# v2.1 gestione inversione bit ingresso
@@ -201,12 +206,12 @@ def readParallelaFiltrata():
# vero fronte 0 -> 1
i_filter_counters[i] = MAX_COUNTER_FILTER
B_temp[i] = 0 # tengo l' ingresso a 0
#logPro.info("START spike 0->1 on bit " + `i` )
#logPro.info("START spike 0->1 on bit " + str(i) )
else :
# fine disturbo breve di uno stato 1
i_filter_counters[i] = 0
B_temp[i] = 1 # tengo l' ingresso a 1
logPro.info("END spike 0->1 on bit " + `i` )
logPro.info("END spike 0->1 on bit " + str(i) )
# stabile 1 -> 1
if ( B_input[i] == 1 ) and ( B_filter_prev [i] == 1 ) :
@@ -224,12 +229,12 @@ def readParallelaFiltrata():
# vero fronte 1 -> 0
i_filter_counters[i] = MAX_COUNTER_FILTER
B_temp[i] = 1 # tengo l' ingresso a 1
#logPro.info("START spike 1->0 on bit " + `i` )
#logPro.info("START spike 1->0 on bit " + str(i) )
else :
# fine disturbo breve di uno stato 0
i_filter_counters[i] = 0
B_temp[i] = 0 # tengo l' ingresso a 0
logPro.info("END spike 1->0 on bit " + `i` )
logPro.info("END spike 1->0 on bit " + str(i) )
# stabile 0 -> 0
if ( B_input[i] == 0 ) and ( B_filter_prev [i] == 0 ) :
@@ -262,7 +267,7 @@ def readParallelaFiltrata():
i_counters[i] = MAX_COUNTER_BLINK
#else :
# # loggo che ho rilevato un blink...
# logPro.info("Blink down on bit " + `i`)
# logPro.info("Blink down on bit " + str(i))
else:
# no , segnale eguale a prima
# se input a 0
@@ -272,7 +277,7 @@ def readParallelaFiltrata():
i_counters[i] = i_counters[i] -1
if ( i_counters[i] == 0 ) :
B_output[i] = 0
logPro.info("END Blink on bit " + `i` )
logPro.info("END Blink on bit " + str(i) )
#Rimettiamo insieme i bit
new_value = 0
@@ -298,8 +303,8 @@ def readParallelaFiltrata():
current = hex( new_value ).replace ( "0x" , "" ).upper()
except Exception as e:
print "Errore in readParallelaFiltrata \n\n"
print str(e)
print ("Errore in readParallelaFiltrata \n\n")
print (str(e))
pass
return current
@@ -313,8 +318,9 @@ def accoda():
dtEve = datetime.utcnow().strftime('%Y%m%d%H%M%S%f')[:-3]
Coda.put(dtEve + '#' + value + '#' + cont)
except Queue.Full:
logPro.error( "Queue full" + `dtEve` + '#' + `value` + '#' + `cont` )
#except Queue.Full:
except queue.Full:
logPro.error( "Queue full" + str(dtEve) + '#' + str(value) + '#' + str(cont) )
except Exception as e:
logPro.error( "NETWORK:Errore http-no com rete-timeout" + url )
logPro.error(str(e))
@@ -328,21 +334,22 @@ def svuota_coda():
global timer_busy
global NMAXSEND
#print "start timer "
#print ("start timer ")
if ( timer_busy == False ):
timer_busy = True
#print "start timer ok "
#print ("start timer ok ")
try:
if not Coda.empty():
#print "coda da svuotare!"
response = urllib.urlopen(URLALIVE)
answ = response.read()
#print ("coda da svuotare!")
response = urllib.request.urlopen(URLALIVE)
answ = response.read().decode('utf-8')
#print(answ)
if answ == 'OK':
#print "OK alive"
response2 = urllib.urlopen(URLENABLED + idxMacchina)
answ2 = response2.read()
#print ("OK alive")
response2 = urllib.request.urlopen(URLENABLED + idxMacchina)
answ2 = response2.read().decode('utf-8')
if answ2 == 'OK':
# aggiorno stato ad online
if onLine == '0':
@@ -388,13 +395,13 @@ def svuota_coda():
url = URLBASE + idxMacchina + URLADV1 + value
url = url + '&dtCurr=' + dtCurr + '&dtEve=' + dtEve + '&cnt=' + cnt
# CHIAMO URL
response3 = urllib.urlopen ( url )
answ3 = response3.read()
response3 = urllib.request.urlopen ( url )
answ3 = response3.read().decode('utf-8')
#print(url)
# log valore inviato!
logSnd.info( value + ' ['+ cnt +']' + ' R:' + answ3 )
#print "Valore smaltito dalla coda"
#print ("Valore smaltito dalla coda")
# tolgo 1 al contatore
i -= 1
@@ -420,17 +427,17 @@ def svuota_coda():
if onLine == '1':
logPro.error("Server Non raggiungibile")
logPro.error(str(e))
#print "Non raggiungibile"
#print ("Non raggiungibile")
onLine = '0'
# in ogni caso
timer_busy = False
#print "end timer ok"
#print ("end timer ok")
#print "end timer "
#print ("end timer ")
#---------------------------------------------------------------
@@ -496,7 +503,7 @@ def avviaParallela():
except Exception as e:
print( "\n\n" + PROGRAM_NAME + " - Error 3 on RPi.GPIO ! \n\n")
print str(e)
print (str(e))
sys.exit(1)
@@ -508,7 +515,8 @@ def avviaParallela():
# MAIN
try:
config = ConfigParser.RawConfigParser()
#config = ConfigParser.RawConfigParser()
config = configparser.RawConfigParser()
config.read ( 'IOB.cfg' )
SAMPLETIME = config.getfloat ( 'time' , 'SAMPLETIME' )
@@ -565,8 +573,8 @@ try:
except Exception as e:
print "\n\n" + PROGRAM_NAME + ' - Error 4 - in config file ' 'IOB.cfg'
print str(e)
print ("\n\n" + PROGRAM_NAME + ' - Error 4 - in config file ' 'IOB.cfg')
print (str(e))
sys.exit(1)
#--------------------------------------------
@@ -587,14 +595,14 @@ try:
except Exception as e:
# manda mail o simili - FARE!!!
print "LOG: Impossibile creare file log con nome"
print ("LOG: Impossibile creare file log con nome")
print (LOGFILE)
print "\n\n"
print str(e)
print ("\n\n")
print (str(e))
#--------------------------------------------
print "\n\n" + PROGRAM_NAME + "\n\n"
print ("\n\n" + PROGRAM_NAME + "\n\n")
global startstatus
startstatus = 1
@@ -646,7 +654,7 @@ except RuntimeError:
print( "\n\n" + PROGRAM_NAME + " - Error 1 - you need superuser privileges")
except Exception as e:
print( "\n\n" + PROGRAM_NAME + " - Error 2 - you need superuser privileges. USE 'sudo' to run your script\n\n")
print str(e)
print (str(e))
sys.exit(1)
@@ -657,7 +665,7 @@ avviaParallela()
# MARCO: qui inserire avvio thread di "svuotaCoda"
# avviaSvuotaCoda
#print "Avvia svuota coda"
#print ("Avvia svuota coda")
do_every ( SENDURLTIME , svuota_coda );
@@ -666,7 +674,7 @@ do_every ( SENDURLTIME , svuota_coda );
old = ''
#print "Avvio ciclo"
#print ("Avvio ciclo")
logPro.info("Avvio loop principale")
while 1: