Sugar

[Unity] RS232 Serial(유니티 RS232 시리얼 통신)

by Sugar0810

※ Api Compatibility Level 설정

image

Project Setting - Player - Other Settings - Configuration - Api Compatibility Level - .NET Framework

 

 

※ DemoSerialCommuncation.cs

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(); 
    } 
}

 

 

※ 참고 사이트

블로그의 정보

Sugar

Sugar0810

활동하기