Serial Communication (Arduino to RaspberryPi)

CMD를 통한 Serial 통신

Python을 통한 Serial 통신

ser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import serial
import time

ser = serial.Serial('/dev/ttyACM0', 115200)
f = open('filename.csv', 'w', encoding = 'utf-8')
t = time.time()

try:
while True:
if ser.in_waiting != 0:
t1 = time.time() - t
t2 = round(t1, 5)
t3 = str(t2)
sensor = ser.readline()
print(t3)
print(sensor.decode())
f.write(t3)
f.write(',')
f.write(sensor.decode())
except:
f.close()
  • 파일명을 serial.py로 지정하면 오류가 날 수 있으므로 유의