|
|
@@ -1,13 +1,18 @@ |
|
|
|
import serial |
|
|
|
import numpy as np |
|
|
|
import matplotlib.pyplot as plt |
|
|
|
import msvcrt #clavier |
|
|
|
import os |
|
|
|
if(os.name == "posix"): |
|
|
|
import getch as gt #clavier |
|
|
|
else: |
|
|
|
import msvcrt as gts |
|
|
|
import time |
|
|
|
|
|
|
|
#Variables principales |
|
|
|
|
|
|
|
serial_path = '/dev/tty.usbserial' |
|
|
|
file_path = "~/PoDoCor" |
|
|
|
serial_path = '/dev/ttyUSB0' |
|
|
|
script_path = os.path.abspath(os.path.dirname(__file__)) |
|
|
|
data_path_dir = script_path + "/data" |
|
|
|
data_nb = 10 |
|
|
|
|
|
|
|
#Variables cachees |
|
|
@@ -17,59 +22,76 @@ data_buff_max = 10 |
|
|
|
data_buff_nb = 0 |
|
|
|
data_buff = "" |
|
|
|
pressed_key = '' |
|
|
|
data = np.empty(1, data_nb) |
|
|
|
raw_data_temp = np.empty(1, data_nb) |
|
|
|
full_rec_data = np.empty((1,data_nb)) |
|
|
|
raw_data_temp = np.empty((1, data_nb)) |
|
|
|
flag_exception_serial = 0 |
|
|
|
|
|
|
|
|
|
|
|
#Programme principal |
|
|
|
|
|
|
|
#init |
|
|
|
arduino_ser = serial.Serial(serial_path, 9600) #communication avec Arduino |
|
|
|
|
|
|
|
while(1): |
|
|
|
pressed_key = msvcrt.getch() #detection de touche pressee (demarrage procedure acquisition) |
|
|
|
#creer dossier si non existant |
|
|
|
if(not(os.path.isdir(data_path_dir))): |
|
|
|
os.mkdir(data_path_dir, 0o777) |
|
|
|
|
|
|
|
try: |
|
|
|
arduino_ser = serial.Serial(serial_path, 9600) #communication avec Arduino |
|
|
|
except serial.serialutil.SerialException: |
|
|
|
print("SerialException : la carte Arduino n'est pas connectee ou est connectee sur au autre port.\nFermeture du programme.") |
|
|
|
flag_exception_serial = 1 |
|
|
|
|
|
|
|
while(1 and flag_exception_serial == 0): |
|
|
|
pressed_key = gt.getch() #detection de touche pressee (demarrage procedure acquisition) |
|
|
|
if(pressed_key == 'P'): |
|
|
|
flag_data = 1 |
|
|
|
data_buff_nb = 0 |
|
|
|
file_name = file_path + "/" + time.asctime()+".csv" |
|
|
|
file = open(file_name, "w") |
|
|
|
data_path_file = data_path_dir + "/" + time.asctime()+".csv" |
|
|
|
file = open(data_path_file, "w") |
|
|
|
file.close() |
|
|
|
else: |
|
|
|
flag_data = 0 |
|
|
|
|
|
|
|
while(flag_data == 1): #boucle de reception des donnees |
|
|
|
arduino_ser.write('1') #envoi de '1' a l'Arduino |
|
|
|
raw_data_txt = ser.readline() #format des donnees: data1,data2,data3\n raw data |
|
|
|
data_buff_nb +=1 |
|
|
|
data_buff = data_buff + raw_data_txt + "\n" #add 1 data line in buffer |
|
|
|
|
|
|
|
#procedure extraction donnees pour affichage |
|
|
|
for i in range(data_nb): |
|
|
|
carac_coma_nb = 1 |
|
|
|
while((raw_data_txt[carac_coma_nb] != ',') or (raw_data[carac_coma_nb:carac_coma_nb+2] != "\n")): #fin de la donnee ou de la ligne |
|
|
|
carac_coma_nb +=1 |
|
|
|
single_data = int(raw_data_txt[0:carac_coma_nb]) |
|
|
|
raw_data_txt = raw_data_txt[carac_coma_nb+1:] |
|
|
|
raw_data_temp[0,i] = single_data |
|
|
|
np.append(data, raw_data, axis=1) |
|
|
|
while(flag_data == 1 and flag_exception_serial == 0): #boucle de reception des donnees |
|
|
|
arduino_ser.write(1) #envoi de '1' a l'Arduino |
|
|
|
raw_data_txt = arduino_ser.readline() #format des donnees: data1,data2,data3\n raw data |
|
|
|
#print(raw_data_txt) |
|
|
|
if(raw_data_txt == ''): # si aucune donnée reçue |
|
|
|
print("Arduino n'envoie aucune donnee, veuillez verifier le code televerse.") |
|
|
|
flag_data = 1 |
|
|
|
data_buff_nb = 0 |
|
|
|
else: |
|
|
|
data_buff_nb +=1 |
|
|
|
data_buff = data_buff + str(raw_data_txt) + "\n" #add 1 data line in buffer |
|
|
|
print("a\n"+data_buff) |
|
|
|
|
|
|
|
#procedure extraction donnees pour affichage |
|
|
|
for i in range(data_nb): |
|
|
|
carac_coma_nb = 1 |
|
|
|
while((raw_data_txt[carac_coma_nb] != ',') or (raw_data[carac_coma_nb:carac_coma_nb+2] != "\n")): #fin de la donnee ou de la ligne |
|
|
|
carac_coma_nb +=1 |
|
|
|
single_data = int(raw_data_txt[0:carac_coma_nb]) |
|
|
|
raw_data_txt = raw_data_txt[carac_coma_nb+1:] |
|
|
|
raw_data_temp[0,i] = single_data |
|
|
|
np.append(full_rec_data, raw_data, axis=1) |
|
|
|
raw_data_temp = raw_data*0 |
|
|
|
|
|
|
|
# procedure d'ecriture fichier |
|
|
|
if(data_buff_nb == data_buff_max): |
|
|
|
file = open(file_name,"a") #mode append |
|
|
|
file.write(data_buff) |
|
|
|
file.close() |
|
|
|
data_buff = "" #remise a zero |
|
|
|
data_buff_nb = 0 |
|
|
|
# procedure d'ecriture fichier |
|
|
|
if(data_buff_nb == data_buff_max): |
|
|
|
file = open(data_path_file,"a") #mode append |
|
|
|
file.write(data_buff) |
|
|
|
file.close() |
|
|
|
data_buff = "" #remise a zero |
|
|
|
data_buff_nb = 0 |
|
|
|
|
|
|
|
pressed_key = msvcrt.getch() #detection de touche pressee (arret procedure acquisition) |
|
|
|
if(pressedKey == 'P'): |
|
|
|
flag_data = 0 |
|
|
|
else: |
|
|
|
flag_data = 1 |
|
|
|
pressed_key = gt.getch() #detection de touche pressee (arret procedure acquisition) |
|
|
|
if(pressedKey == 'P'): |
|
|
|
flag_data = 0 |
|
|
|
else: |
|
|
|
flag_data = 1 |
|
|
|
|
|
|
|
|
|
|
|
assert msvcrt.getch() != b'P' "Sortie de la boucle de reception des donnees" |
|
|
|
assert pressed_key != 'P' "Sortie de la boucle de reception des donnees" |
|
|
|
|
|
|
|
if(data_buff_nb > 0): |
|
|
|
file = open(file_name,"a") |
|
|
@@ -77,3 +99,5 @@ while(1): |
|
|
|
file.close() |
|
|
|
data_buff = "" |
|
|
|
data_buff_nb = 0 |
|
|
|
|
|
|
|
arduino_ser.close() |