본문 바로가기

Arduino<>Python

arduino -->python 데이터 전송

아두이노에서 python으로 데이터를 전송 한 후 연산 할 수 있도록 정수형으로 변환해봅니다.

아두이노 보드에 다음 코드를 업로드 한 후 시리얼 모니터에서 확인 합니다.

 

void setup() {
  Serial.begin(9600);
  for (int x = 100; x <2000; x=x+50) {  
  Serial.println(x);
  delay(250);
           }     }

void loop() {
                 }

 

시리얼 출력입니다.

 

COM7과 속도 9600  기억합니다.

 

수신 데이터  python 코드입니다.


import serial
import time

# 아두이노 시리얼 모니터에서 포트(COM7)확인 후 모니터 창을 닫습니다.
ser = serial.Serial('COM7', 9800, timeout=1)
time.sleep(2)
while True:
    get_data = ser.readline()   # read a byte
    if get_data:
        get_data = get_data.decode()  # convert the byte string to a unicode string
        num = int(get_data) # convert the unicode string to an int
        print(num)
        print(type(num))

python 출력입니다.

class 'int' 이므로 사칙연산을 할 수 있습니다.

 

 

'Arduino<>Python' 카테고리의 다른 글

시리얼 통신과 인코드 , 디코드  (0) 2025.02.01
Arduino --> python 시리얼 통신  (0) 2025.01.29
문자 변수와 숫자 변수  (0) 2022.07.20
LED OnOff  (0) 2021.07.21
아두이노와 Python(firmata)-[2]  (0) 2021.04.02