본문 바로가기

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

[유니티 시리얼통신] RS232 시리얼 통신

728x90
반응형

1. Api Compatibility Level 설정

① Edit > Project Settings... 클릭

 

코더제로 유니티 시리얼통신 RS232 시리얼 통신 Project Settings
그림. Project Settings

 

 

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

 

코더제로 유니티 시리얼통신 RS232 시리얼 통신 Api Compatibility Level
그림. Api Compatibility Level

 

 

2. 소스

using UnityEngine; 
using System; 
using System.IO.Ports; 

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

    void Start() 
    { 
        m_SerialPort.Open(); 
    } 

    private void Update() 
    { 
        try 
        { 
            if (m_SerialPort.IsOpen) 
            { 
                m_Data = m_SerialPort.ReadLine(); 
                m_SerialPort.ReadTimeout = 30; 
            } 
        } 

        catch (Exception e) 
        { 
            Debug.Log(e); 
        } 
    } 

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

 

728x90
반응형