You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

80 line
2.3KB

  1. import serial
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import msvcrt #clavier
  5. import time
  6. #Variables principales
  7. serial_path = '/dev/tty.usbserial'
  8. file_path = "~/PoDoCor"
  9. data_nb = 10
  10. #Variables cachees
  11. flag_data = 0
  12. data_buff_max = 10
  13. data_buff_nb = 0
  14. data_buff = ""
  15. pressed_key = ''
  16. data = np.empty(1, data_nb)
  17. raw_data_temp = np.empty(1, data_nb)
  18. #Programme principal
  19. #init
  20. arduino_ser = serial.Serial(serial_path, 9600) #communication avec Arduino
  21. while(1):
  22. pressed_key = msvcrt.getch() #detection de touche pressee (demarrage procedure acquisition)
  23. if(pressed_key == 'P'):
  24. flag_data = 1
  25. data_buff_nb = 0
  26. file_name = file_path + "/" + time.asctime()+".csv"
  27. file = open(file_name, "w")
  28. file.close()
  29. else:
  30. flag_data = 0
  31. while(flag_data == 1): #boucle de reception des donnees
  32. arduino_ser.write('1') #envoi de '1' a l'Arduino
  33. raw_data_txt = ser.readline() #format des donnees: data1,data2,data3\n raw data
  34. data_buff_nb +=1
  35. data_buff = data_buff + raw_data_txt + "\n" #add 1 data line in buffer
  36. #procedure extraction donnees pour affichage
  37. for i in range(data_nb):
  38. carac_coma_nb = 1
  39. 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
  40. carac_coma_nb +=1
  41. single_data = int(raw_data_txt[0:carac_coma_nb])
  42. raw_data_txt = raw_data_txt[carac_coma_nb+1:]
  43. raw_data_temp[0,i] = single_data
  44. np.append(data, raw_data, axis=1)
  45. raw_data_temp = raw_data*0
  46. # procedure d'ecriture fichier
  47. if(data_buff_nb == data_buff_max):
  48. file = open(file_name,"a") #mode append
  49. file.write(data_buff)
  50. file.close()
  51. data_buff = "" #remise a zero
  52. data_buff_nb = 0
  53. pressed_key = msvcrt.getch() #detection de touche pressee (arret procedure acquisition)
  54. if(pressedKey == 'P'):
  55. flag_data = 0
  56. else:
  57. flag_data = 1
  58. assert msvcrt.getch() != b'P' "Sortie de la boucle de reception des donnees"
  59. if(data_buff_nb > 0):
  60. file = open(file_name,"a")
  61. file.write(data_buff)
  62. file.close()
  63. data_buff = ""
  64. data_buff_nb = 0