728x90
반응형
1. Api Compatibility Level 설정
① Edit > Project Settings... 클릭

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

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 클릭

(3) JUST DOWNLOAD 클릭

(4) 아두이노 IDE

① 컴파일 : 소스를 컴파일 합니다. 소스가 문법적으로 문제 없는지 확인해 줍니다.
② 업로드 : 컴파일 + 업로드. 아두이노이 소스를 컴파일 후 업로드 합니다.
③ 시리얼 모니터 :
④ 소스 : 아두이노 소스 입력합니다.
- 전역변수, include 선언 등을 setup 위에 선언할 수 있습니다.
- setup은 처음 한번만 실행됩니다.
- loop는 아두이노가 실행 될 때 계속 도는 무한 루프입니다.
⑤ 메시지 : 알림 메시지나컴파일, 업로드 결과 등을 표시합니다.
파일 > 예제에 들어가면, 기본적인 예제를 제공합니다.

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

728x90
반응형
'프로그램 > 유니티 시리얼통신' 카테고리의 다른 글
[유니티 시리얼통신] RS232 시리얼 통신 (0) | 2020.03.11 |
---|