瀏覽代碼

Création de la structure très générale du bloc de récupération des données, de conversion dans un tableau et d'écriture dans le fichier

tags/PoDoCor-v0.1
lilian 3 年之前
父節點
當前提交
0bec381d27
共有 1 個文件被更改,包括 72 次插入4 次删除
  1. +72
    -4
      main.py

+ 72
- 4
main.py 查看文件

@@ -1,11 +1,79 @@
import serial
import numpy as np
import matplotlib.pyplot as plt
import msvcrt #clavier
import time

#Variables principales

serial_path = '/dev/tty.usbserial'
file_path = "~/PoDoCor"
data_nb = 10

#Variables cachees

flag_data = 0
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)


#Programme principal

while(msvcrt.getch() != b'P'):
print('b')
#init
arduino_ser = serial.Serial(serial_path, 9600) #communication avec Arduino

while(1):
pressed_key = msvcrt.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")
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)
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

pressed_key = msvcrt.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"

print('y')
if(data_buff_nb > 0):
file = open(file_name,"a")
file.write(data_buff)
file.close()
data_buff = ""
data_buff_nb = 0

Loading…
取消
儲存