Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
using UnityEngine;
using System.Collections;
public class SerialPortTest : MonoBehaviour
{
public string PortName = "/dev/cu.usbmodem1412";
private const int BaudRate = 115200;
private SerialPortWrapper _serialPortWrapper;
void OnEnable()
{
if (_serialPortWrapper != null)
{
_serialPortWrapper.KillThread();
}
_serialPortWrapper = new SerialPortWrapper(PortName, BaudRate);
_serialPortWrapper.onMessageCallback = OnMessage;
}
void OnDisable()
{
if (_serialPortWrapper != null)
{
_serialPortWrapper.KillThread();
_serialPortWrapper = null;
}
}
void OnMessage(string msg)
{
if (string.IsNullOrEmpty(msg) == false)
{
// メッセージを受け取ったらなにかする
Debug.Log(msg + " / " + msg.Length);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment