반응형
임베디드시스템에 라즈베리파이3이 많이 쓰이죠. 자주 쓰는 것 중 하나가 uart로 받은 신호를 엑셀파일로 저장하는 것입니다.
아래는 uart로 받은 data를 excel 파일로 저장하는 코드입니다.
(참고로 Rasberry pi 3이 사용되었는데 4를 써도 해당되는 포트만 설정해주면 됩니다.)
import serial
import pandas as pd
import time
# Configure UART
uart = serial.Serial("/dev/serial0", baudrate=115200)
# Create an empty DataFrame to store the data
data = {'time': [], 'data': []}
df = pd.DataFrame(data)
while True:
# Read data from UART
uart_data = uart.readline()
# Get current time
current_time = time.time()
# Append the data and time to the DataFrame
df = df.append({'time': current_time, 'data': uart_data}, ignore_index=True)
# Save the DataFrame to an Excel file every 5 min (300 seconds).
if len(df) % 300 == 0:
df.to_excel('uart_data.xlsx', index=False)
그리고 excel 파일은 경로를 따로 지정하지 않으면 파이썬 파일이 있는 현재 위치에 저장이 됩니다.
주소를 바꾸고싶으면
df.to_excel('/home/pi/Desktop/uart_data.xlsx', index=False)
이런 식으로 위치를 넣어주면 됩니다.
그럼 홧팅!!

반응형
'전자 | 제어 | 항공우주 > 코딩' 카테고리의 다른 글
[VSCode] 서식 포함 복사/붙여넣기 (4) | 2024.08.11 |
---|---|
ESP-IDF VScode 설치 오류 해결하기 (Invalid ESP-IDF) (6) | 2024.01.09 |
[파이썬] 설치된 버전 확인방법 (0) | 2023.12.15 |
[Python] Excel로 데이터 읽어와 FFT 수행후 그래프 출력 Code (0) | 2023.01.19 |
[VSCode] Go to Definition (F12 or Ctrl + Click) 안될 때 (2) | 2022.07.19 |
댓글