본문 바로가기

프로그램/유니티 시리얼통신

[유니티 시리얼통신] 아두이노(Arduino)

728x90
반응형

1. Api Compatibility Level 설정

① Edit > Project Settings... 클릭

 

코더제로 유니티 시리얼통신 아두이노 Arduino Project Settings
그림. Project Settings

 

 

 ② Player > Other Settings > Api Compatibility Level 에서 .NET 4.x 선택

 

코더제로 유니티 시리얼통신 아두이노 Arduino Api Compatibility Level
그림. Api Compatibility Level

 

 

2. 소스

using UnityEngine; 
using System.IO.Ports; 

public class DemoArduino : MonoBehaviour 
{ 
    SerialPort m_SerialPort = new SerialPort("COM3", 9600, Parity.None, 8, StopBits.One); 

    void Start() 
    { 
        m_SerialPort.Open(); 
    } 
     
    void Update() 
    { 
        if (Input.GetKeyDown(KeyCode.O)) 
            SerialPortWrite("open"); // 아두이노에 저장되어 있는 string을 보냅니다.

        if (Input.GetKeyDown(KeyCode.C)) 
            SerialPortWrite("close"); // 아두이노에 저장되어 있는 string을 보냅니다.
    } 

    void OnApplicationQuit() 
    { 
        m_SerialPort.Close(); 
    } 

    void SerialPortWrite(string message) 
    { 
        m_SerialPort.Write(message); 
    } 
}

 

 

3. 아두이노 IDE 설치

 (1) 다운로드 : https://www.arduino.cc/en/Main/Software

 

 (2) Download the Arduino IDE > Windows Installler, for Windows XP and up 클릭

 

코더제로 유니티 시리얼통신 아두이노 Arduino Download the Arduino IDE
그림. Download the Arduino IDE

 

 

 (3) JUST DOWNLOAD 클릭

 

코더제로 유니티 시리얼통신 아두이노 Arduino JUST DOWNLOAD
그림. JUST DOWNLOAD

 

 

(4) 아두이노 IDE

 

코더제로 유니티 시리얼통신 아두이노 Arduino 아두이노 IDE
그림. 아두이노 IDE

 

 

 ① 컴파일 : 소스를 컴파일 합니다. 소스가 문법적으로 문제 없는지 확인해 줍니다.

 ② 업로드 : 컴파일 + 업로드. 아두이노이 소스를 컴파일 후 업로드 합니다.

 ③ 시리얼 모니터 :  

 ④ 소스 : 아두이노 소스 입력합니다.

  - 전역변수, include 선언 등을 setup 위에 선언할 수 있습니다.

  - setup은 처음 한번만 실행됩니다.

  - loop는 아두이노가 실행 될 때 계속 도는 무한 루프입니다.

 ⑤ 메시지 : 알림 메시지나컴파일, 업로드 결과 등을 표시합니다. 

 

 

파일 > 예제에 들어가면, 기본적인 예제를 제공합니다.

 

코더제로 유니티 시리얼통신 아두이노 Arduino 기본적인 예제
그림. 기본적인 예제

 

 

툴 > 보드, 포트 를 설정하면 됩니다.

 

코더제로 유니티 시리얼통신 아두이노 Arduino 툴
그림. 툴

 

728x90
반응형