samplecsv_taker.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. ChatGPT 3.5
  2. User
  3. i want an serial read sotware that has an tkinter button with start stop and save, i want to start measuring with start and continue measuring till stop is pressed or 10000 sample points are reached, sample with an rate of 200hz when i click save i want an dialog field and save the data as csv, the header shall look like timestamp,value,
  4. while measuring give every datapoint an timestamp using time.time()
  5. ChatGPT
  6. Sure, I can guide you through creating a Python script that fulfills your requirements using Tkinter for the GUI and a serial library for communication. Here's a basic outline of the steps we'll take:
  7. Create a Tkinter GUI with buttons for start, stop, and save.
  8. Use the serial library to communicate with your device.
  9. Start reading data when the start button is clicked.
  10. Stop reading data when the stop button is clicked or when 10,000 sample points are reached.
  11. Save the data to a CSV file when the save button is clicked.
  12. Let's start by creating the script:
  13. python
  14. import tkinter as tk
  15. from tkinter import filedialog
  16. import csv
  17. import serial
  18. import time
  19. class SerialDataLogger:
  20. def __init__(self, root):
  21. self.root = root
  22. self.root.title("Serial Data Logger")
  23. self.serial_port = "COM1" # Change this to your serial port
  24. self.baud_rate = 9600 # Change this to match your device's baud rate
  25. self.is_logging = False
  26. self.sample_count = 0
  27. self.max_samples = 10000
  28. self.sample_rate = 200
  29. self.data = []
  30. self.create_widgets()
  31. def create_widgets(self):
  32. self.start_button = tk.Button(self.root, text="Start", command=self.start_logging)
  33. self.start_button.pack()
  34. self.stop_button = tk.Button(self.root, text="Stop", command=self.stop_logging)
  35. self.stop_button.pack()
  36. self.save_button = tk.Button(self.root, text="Save", command=self.save_data)
  37. self.save_button.pack()
  38. def start_logging(self):
  39. self.is_logging = True
  40. self.sample_count = 0
  41. self.data = []
  42. try:
  43. self.serial_connection = serial.Serial(self.serial_port, self.baud_rate)
  44. self.root.after(0, self.read_serial_data)
  45. except serial.SerialException:
  46. print("Error: Unable to open serial port.")
  47. def stop_logging(self):
  48. self.is_logging = False
  49. if hasattr(self, 'serial_connection'):
  50. self.serial_connection.close()
  51. def read_serial_data(self):
  52. if self.is_logging:
  53. if self.sample_count < self.max_samples:
  54. try:
  55. raw_data = self.serial_connection.readline().decode().strip()
  56. timestamp = time.time()
  57. self.data.append((timestamp, raw_data))
  58. self.sample_count += 1
  59. except UnicodeDecodeError:
  60. print("Error decoding serial data.")
  61. else:
  62. self.stop_logging()
  63. self.root.after(int(1000 / self.sample_rate), self.read_serial_data)
  64. def save_data(self):
  65. if self.data:
  66. file_path = filedialog.asksaveasfilename(defaultextension=".csv")
  67. if file_path:
  68. with open(file_path, 'w', newline='') as csvfile:
  69. csv_writer = csv.writer(csvfile)
  70. csv_writer.writerow(["Timestamp", "Value"])
  71. csv_writer.writerows(self.data)
  72. print("Data saved to", file_path)
  73. else:
  74. print("No data to save.")
  75. if __name__ == "__main__":
  76. root = tk.Tk()
  77. app = SerialDataLogger(root)
  78. root.mainloop()