myAppSerial (II)
Bene continuamo la nostra GUI
Oggi creiamo la sezione Rx Buffer.
myTry02
In myTry02 aggiungiamo la sezione di ricezione dati.
##############################################################################
#myFrame (Rx Buffer)
# fram,[labe,xBox]
obje, othe = myFrame(name='Rx buffer', obje=None, colo='black',
bord=2, shad=Gtk.SHADOW_ETCHED_OUT,
tBox='h', aBox=[False, False, 10])
# referenzio l'oggetto
self.boxBuf = othe[1]
# change background color to Class
chaBackColor(obj=obje, css="boxBuf", col="#a0a0a0")
# insert object in application
self.bBox.pack_start(obje, False, False, 1)
#myLabFrame (counter Rx data)
#fram, [labe, lFrm, xBox]
obje, othe = myLabFrame(name='0',
leng=len('0')+1, prea=' ', post='',
font='Arial 10',
colo='blue',
nFra='Rx.data', cFra='black', bFra=1, sFra=Gtk.SHADOW_ETCHED_OUT,
tFra='h', aFra=[False, False, 1])
# referenzio l'oggetto
self.labRcv = othe[0]
# insert object in application
self.boxBuf.pack_start(obje, False, False, 1)
#myChkButton (enable Rx)
# ridefinisco la callback
def on_clicked(widg, name, *data):
pass
# butt, call
obje, othe = myChkButton(name='Ena',
valu=True, colo='black',
call= on_clicked, data=['-',])
# referenzio l'oggetto
self.chkEna = obje
# fram,[labe,xBox]
obj1, oth1 = myFrame(name='Rx', obje=obje, colo='black',
bord=2, shad=Gtk.SHADOW_ETCHED_OUT,
tBox='h' )
# insert object in application
self.boxBuf.pack_start(obj1, False, False, 1)
#myButton (clear buffer)
# ridefinisco la callback
def on_clicked(widg, *data):
self.clear()
# frame ricevuti
self.couRcv = 0
self.couSnd = 0
self.labRcv.set_text(" %d" %self.couRcv)
self.labSnd.set_text(" %d" %self.couSnd)
# fram, [labe, xBox, butt, call]
obje, othe = myButFrame(name="buffer",
icon=Gtk.STOCK_OK,
call=on_clicked, data=[],
bFra=1, sFra=Gtk.SHADOW_ETCHED_OUT,
tFra='v', aFra=[False, False, 1])
# imposto la label
othe[2].props.label = "clear"
# insert object in application
self.boxBuf.pack_start(obje, False, False, 1)
#myChkButton (view Hid)
# ridefinisco la callback
def on_clicked(widg, name, *data):
if widg.get_active():
self.dev.hid = 1
else:
self.dev.hid = 0
# butt, call
obje, othe = myChkButton(name='view',
valu=0, colo='black',
call= on_clicked, data=['-',])
# referenzio l'oggetto
self.chkHid = obje
# fram,[labe,xBox]
obj1, oth1 = myFrame(name='Hide', obje=obje, colo='black',
bord=2, shad=Gtk.SHADOW_ETCHED_OUT,
tBox='h' )
# insert object in application
self.boxBuf.pack_start(obj1, False, False, 1)
#myChkButton (view Hex)
# ridefinisco la callback
def on_clicked(widg, name, *data):
if widg.get_active():
self.dev.hex = 1
else:
self.dev.hex = 0
# butt, call
obje, othe = myChkButton(name='view',
valu=0, colo='black',
call= on_clicked, data=['-',])
# referenzio l'oggetto
self.chkHex = obje
# fram,[labe,xBox]
obj1, oth1 = myFrame(name='Hex', obje=obje, colo='black',
bord=2, shad=Gtk.SHADOW_ETCHED_OUT,
tBox='h' )
# insert object in application
self.boxBuf.pack_start(obj1, False, False, 1)
Rx
Come potete vedere abbiamo inserito:
- Il contatore dei dati ricevuti
#myLabFrame (counter Rx data)
#fram, [labe, lFrm, xBox]
obje, othe = myLabFrame(name='0',
leng=len('0')+1, prea=' ', post='',
font='Arial 10',
colo='blue',
nFra='Rx.data', cFra='black', bFra=1, sFra=Gtk.SHADOW_ETCHED_OUT,
tFra='h', aFra=[False, False, 1])
# referenzio l'oggetto
self.labRcv = othe[0]
# insert object in application
self.boxBuf.pack_start(obje, False, False, 1)
Qui referenziamo l’ oggetto self.labRcv che ci servirà per aggiornare il contatore.
- La possibilità di abilitare/disabilitare la ricezione dati
#myChkButton (enable Rx)
# ridefinisco la callback
def on_clicked(widg, name, *data):
pass
# butt, call
obje, othe = myChkButton(name='Ena',
valu=True, colo='black',
call= on_clicked, data=['-',])
# referenzio l'oggetto
self.chkEna = obje
# fram,[labe,xBox]
obj1, oth1 = myFrame(name='Rx', obje=obje, colo='black',
bord=2, shad=Gtk.SHADOW_ETCHED_OUT,
tBox='h' )
# insert object in application
self.boxBuf.pack_start(obj1, False, False, 1)
Con la referenza self.chkEna abilitiamo/disabilitiamo la ricezione dati nel metodo devRx
- Un pulsante per la pulizia del nostro logger che resetta anche i contatori Rx/Tx
#myButton (clear buffer)
# ridefinisco la callback
def on_clicked(widg, *data):
self.clear()
# frame ricevuti
self.couRcv = 0
self.couSnd = 0
self.labRcv.set_text(" %d" %self.couRcv)
self.labSnd.set_text(" %d" %self.couSnd)
# fram, [labe, xBox, butt, call]
obje, othe = myButFrame(name="buffer",
icon=Gtk.STOCK_OK,
call=on_clicked, data=[],
bFra=1, sFra=Gtk.SHADOW_ETCHED_OUT,
tFra='v', aFra=[False, False, 1])
# imposto la label
othe[2].props.label = "clear"
# insert object in application
self.boxBuf.pack_start(obje, False, False, 1)
Qui definiamo una nuova callback per il nostro pulsante che esegue tutte le operazioni richieste.
- La possibilità di visualizzare i caratteri non stampabili
#myChkButton (view Hid)
# ridefinisco la callback
def on_clicked(widg, name, *data):
if widg.get_active():
self.dev.hid = 1
else:
self.dev.hid = 0
# butt, call
obje, othe = myChkButton(name='view',
valu=0, colo='black',
call= on_clicked, data=['-',])
# referenzio l'oggetto
self.chkHid = obje
# fram,[labe,xBox]
obj1, oth1 = myFrame(name='Hide', obje=obje, colo='black',
bord=2, shad=Gtk.SHADOW_ETCHED_OUT,
tBox='h' )
# insert object in application
self.boxBuf.pack_start(obj1, False, False, 1)
Anche qui definiamo una nuova callback che attiva/disattiva il flag self.dev.hid presente nel modulo del protocollo per rendere visibili i caratteri non stampabili.
- La possibilità di visualizzare i caratteri ricevuti in formato esadecimale
#myChkButton (view Hex)
# ridefinisco la callback
def on_clicked(widg, name, *data):
if widg.get_active():
self.dev.hex = 1
else:
self.dev.hex = 0
# butt, call
obje, othe = myChkButton(name='view',
valu=0, colo='black',
call= on_clicked, data=['-',])
# referenzio l'oggetto
self.chkHex = obje
# fram,[labe,xBox]
obj1, oth1 = myFrame(name='Hex', obje=obje, colo='black',
bord=2, shad=Gtk.SHADOW_ETCHED_OUT,
tBox='h' )
# insert object in application
self.boxBuf.pack_start(obj1, False, False, 1)
Allo stesso modo definiamo una nuova callback che attiva/disattiva il flag self.dev.hex presente nel modulo del protocollo per visualizzare i caratteri ricevuti nella codifica esadecimale.
Parser
Abbiamo ridefinito il parser per le nuove necessità.
class MyParser:
def __init__(self, app):
self.data = None
self.app = app
def parser(self, *args):
"parsing"
# data input
self.data = args[0][:]
# data output
# print "rx:", self.data,
self.app.wriTg(self.data,"bluWhi")
if self.data:
# aggiorno bytes ricevuti
self.app.couRcv += len(self.data)
self.app.labRcv.set_text(" %d" %self.app.couRcv)
# mantiene il loop attivo
return True
Notare che adesso richiede in ingresso l’ attributo app che ci serve come riferimento per poter scrivere nel logger e aggiornare il contatore dei bytes ricevuti.
Main
Il nostro main adesso invia un messaggio di prova attende 3 secondi e pulisce il nostro logger.
myTry02(self)
# richiesta di invio ritardata
GLib.timeout_add(600, self.dev.sendListCommands, ["prova di invio!",], sto="\r")
# richiesta di pulizia ritardata
GLib.timeout_add(3600, self.clear)
# avvia applicazione
Gtk.main()
Se proviamo ad avviare il nostro script vedremo:

myAppSerial in esecuzione.
Come potete notare adesso ci sono le sezioni Rx e Tx con i relativi widgets.
Package
La struttura aggiornata del nostro package è la seguente:
myAppSerial/
l00_start.py
l01_startGtk.py
my00init.py
myAppSerial.py
myLib/
__init__.py
myProtocol/
__init__.py
my00init.py
myProtocol.py
mySerial.py
Per scaricare la nuova versione 20150910.zip
Saluti
Bene nel prossimo post arricchiremo la nostra applicazione con altre caratteristiche.
Ciao alla prossima. (stay tune!)