<div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">Il giorno 04 novembre 2013 16:11, Manlio Perillo <span dir="ltr"><<a href="mailto:manlio.perillo@gmail.com" target="_blank">manlio.perillo@gmail.com</a>></span> ha scritto:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><br>
import sys<br>
import select<br>
<br>
<br>
# ... open serial port, init curses<br>
while 1:<br>
rfd, wfd, efd = select.select([stdin, conn], [], [], 0)<br>
if conn in rfd:<br>
dati = conn.read(64)<br>
# ... aggiorna schermo e file dati<br>
if stdin in rfd:<br>
cmd = stdin.read()<br>
if cmd.strip().lower() == "q":<br>
# ... salva il file excel<br>
break<br>
# close serial port, close curser</blockquote></div><div class="gmail_extra"><br></div>Ho cercato di sistemarlo un po', ne è uscito questo e pare funzionare ma devo testarlo in loco per cantare vittoria:</div><div class="gmail_extra">
<br></div><div class="gmail_extra"><div class="gmail_extra">#!/usr/bin/python</div><div class="gmail_extra"># -*- coding: utf-8 -*-</div><div class="gmail_extra"> </div><div class="gmail_extra">import curses</div><div class="gmail_extra">
import time</div><div class="gmail_extra">import serial</div><div class="gmail_extra">import xlwt</div><div class="gmail_extra">import sys</div><div class="gmail_extra">import select</div><div class="gmail_extra"><br></div>
<div class="gmail_extra">def aprischermo():</div><div class="gmail_extra"><br></div><div class="gmail_extra"> stdscr = curses.initscr()</div><div class="gmail_extra"> curses.noecho()</div><div class="gmail_extra"> titolo = 'Ricezione Dati'</div>
<div class="gmail_extra"> pos = int (40 - (len(titolo))/2)</div><div class="gmail_extra"> stdscr.addstr(1, pos, titolo, curses.A_REVERSE | curses.A_BOLD)</div><div class="gmail_extra"> stdscr.addstr(7, 5, "Lettura -- Pressione -- Flusso - Permeabilita'", curses.A_BOLD)</div>
<div class="gmail_extra"> stdscr.addstr(15, 5, 'Premi per uscire')</div><div class="gmail_extra"> stdscr.addstr(15, 11, 'Q', curses.A_REVERSE | curses.A_BOLD)</div><div class="gmail_extra"> stdscr.addstr(5, 5, 'Dati Ricevuti:', curses.A_BOLD)</div>
<div class="gmail_extra"> stdscr.refresh()</div><div class="gmail_extra"> return stdscr</div><div class="gmail_extra"><br></div><div class="gmail_extra">def connessione(stdscr):</div><div class="gmail_extra"><br></div>
<div class="gmail_extra"> try:</div><div class="gmail_extra"> #conn = serial.Serial("/dev/ttyUSB0", 9600, 7, "N", 1, timeout=1) # <-- Senza connessione esce...</div><div class="gmail_extra">
conn = serial.Serial("/dev/ttyS0", 19200, timeout=1) # <-- per provare...</div><div class="gmail_extra"><br></div><div class="gmail_extra"> except:</div><div class="gmail_extra"><br>
</div>
<div class="gmail_extra"> stdscr.addstr(3, 20, 'Porta Seriale Non Disponibile')</div><div class="gmail_extra"> stdscr.refresh()</div><div class="gmail_extra"> time.sleep(3)</div><div class="gmail_extra">
chiudi()</div><div class="gmail_extra"><br></div><div class="gmail_extra"> return conn</div><div class="gmail_extra"><br></div><div class="gmail_extra">def ciclo(conn):</div><div class="gmail_extra"><br></div>
<div class="gmail_extra">
# Definizione file temporaneo</div><div class="gmail_extra"><br></div><div class="gmail_extra"> fou = open("/home/Dati.txt", "w")</div><div class="gmail_extra"><br></div><div class="gmail_extra">
a = 0</div><div class="gmail_extra"><br></div><div class="gmail_extra"> while 1:</div><div class="gmail_extra"><br></div><div class="gmail_extra"> # rfd = input ready</div><div class="gmail_extra"> # wft = output reeady</div>
<div class="gmail_extra"> # efd = exception ready</div><div class="gmail_extra"><br></div><div class="gmail_extra"> rfd, wft, efd = select.select([sys.stdin, conn], [], [], 0)</div><div class="gmail_extra">
<br>
</div><div class="gmail_extra"> # Controllo la disponibilità di dati sulla seriale</div><div class="gmail_extra"><br></div><div class="gmail_extra"> if conn in rfd:</div><div class="gmail_extra"><br></div><div class="gmail_extra">
dati = conn.read(256)</div><div class="gmail_extra"><br></div><div class="gmail_extra"> # Elimino quello che non mi serve.</div><div class="gmail_extra"><br></div><div class="gmail_extra"> dati = dati.replace('*', '')</div>
<div class="gmail_extra"> dati = dati.replace('SAMPLE --- PRESSURE --- FLOW --- PERMEABILITY', '')</div><div class="gmail_extra"> dati = dati.replace('2 SQUARE CM CLAMP', '')</div>
<div class="gmail_extra"> dati = dati.replace('PPM100 V2.1 FIAL', '')</div><div class="gmail_extra"> dati = dati.strip()</div><div class="gmail_extra"><br></div><div class="gmail_extra">
# Stringa vuota: continuo</div><div class="gmail_extra"><br></div><div class="gmail_extra"> if dati == '':</div><div class="gmail_extra"> continue</div><div class="gmail_extra">
<br></div><div class="gmail_extra"> # Visualizzo quanto ricevuto dalla seriale</div><div class="gmail_extra"><br></div><div class="gmail_extra"> stdscr.addstr(9, 5, dati)</div><div class="gmail_extra">
stdscr.refresh()</div><div class="gmail_extra"><br></div><div class="gmail_extra"> # Scrivo i dati sul file temporaneo</div><div class="gmail_extra"><br></div><div class="gmail_extra"> fou.write(dati + "\n")</div>
<div class="gmail_extra"><br></div><div class="gmail_extra"> # Controllo la pressione del tasto 'Q' sulla tastiera.</div><div class="gmail_extra"><br></div><div class="gmail_extra"> if sys.stdin in rfd:</div>
<div class="gmail_extra"> cmd = sys.stdin.read(1)</div><div class="gmail_extra"><br></div><div class="gmail_extra"> # Se premuto scrivo il file ed esco.</div><div class="gmail_extra"><br></div><div class="gmail_extra">
if cmd.strip().lower() == "q":</div><div class="gmail_extra"><br></div><div class="gmail_extra"> # Chiudo il file temporaneo</div><div class="gmail_extra"><br></div><div class="gmail_extra">
fou.close()</div><div class="gmail_extra"><br></div><div class="gmail_extra"> xlsout=xlwt.Workbook(encoding='latin-1')</div><div class="gmail_extra"> foglio1=xlsout.add_sheet('Foglio1')</div>
<div class="gmail_extra"><br></div><div class="gmail_extra"> foglio1.write(0,0, 'Lettura')</div><div class="gmail_extra"> foglio1.write(0,1, 'Pressione')</div><div class="gmail_extra">
foglio1.write(0,2, 'Flusso')</div><div class="gmail_extra"> foglio1.write(0,3, "Permeabilita'")</div><div class="gmail_extra"><br></div><div class="gmail_extra"> riga = 2</div>
<div class="gmail_extra"><br></div><div class="gmail_extra"> fin = open("/home/Dati.txt", "r")</div><div class="gmail_extra"> lines = fin.readlines()</div><div class="gmail_extra">
fin.close()</div><div class="gmail_extra"><br></div><div class="gmail_extra"> for line in lines:</div><div class="gmail_extra"><br></div><div class="gmail_extra"> riga += 1</div>
<div class="gmail_extra"><br></div><div class="gmail_extra"> sample = line[0:5].strip()</div><div class="gmail_extra"> pressure = line[5:18].strip()</div><div class="gmail_extra"> flow = line[18:27].strip()</div>
<div class="gmail_extra"> permeability = line[27:].strip()</div><div class="gmail_extra"><br></div><div class="gmail_extra"> foglio1.write(riga, 1, sample)</div><div class="gmail_extra">
foglio1.write(riga, 2, pressure)</div><div class="gmail_extra"> foglio1.write(riga, 3, flow)</div><div class="gmail_extra"> foglio1.write(riga, 4, permeability)</div>
<div class="gmail_extra"><br></div><div class="gmail_extra"> xlsout.save('/home/dati.xls')</div><div class="gmail_extra"><br></div><div class="gmail_extra"> chiudi()</div><div class="gmail_extra">
<br></div><div class="gmail_extra">def chiudi():</div><div class="gmail_extra"><br></div><div class="gmail_extra"> curses.echo()</div><div class="gmail_extra"> curses.nocbreak()</div><div class="gmail_extra"> curses.curs_set(1)</div>
<div class="gmail_extra"> curses.endwin()</div><div class="gmail_extra"> sys.exit(0)</div><div class="gmail_extra"><br></div><div class="gmail_extra">stdscr = aprischermo()</div><div class="gmail_extra">conn = connessione(stdscr)</div>
<div class="gmail_extra">ciclo(conn)</div><div><br></div><div class="gmail_extra"><br></div>Ma la domanda è:</div><div class="gmail_extra"><br></div><div class="gmail_extra">Se lo porto su windows cosa devo cambiare?</div>
<div class="gmail_extra"><br></div><div class="gmail_extra">Grazie!<br clear="all"><div><br></div>-- <br>Riccardo Brazzale<br>Linux User #299418 Linux Machine #184578<br>
</div></div>