Last active
November 11, 2016 09:15
-
-
Save yjiro0403/1c87b91b6018886d6a2cbdff1de14ad7 to your computer and use it in GitHub Desktop.
UnityとArduinoをシリアル通信 ref: http://qiita.com/yjiro0403/items/54e9518b5624c0030531
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Serial.print(Signal1); | |
Serial.print("\t"); | |
Serial.print(Signal2); | |
Serial.print("\t"); | |
... | |
Serial.print(Signaln); | |
Serial.println(); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if ( Serial.available() ) { | |
char mode = Serial.read(); | |
switch (mode) { | |
case '0' : digitalWrite(13, LOW); break; | |
case '1' : digitalWrite(13, HIGH); break; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.IO.Ports; | |
using System.Threading; //スレッド通信 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
serialPort_ = new SerialPort(portName,baudRate, Parity.None, 8, StopBits.One); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
IOException: アクセスが拒否されました |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class hogehoge : MonoBehaviour | |
{ | |
//先ほど作成したクラス | |
public SerialHandler serialHandler; | |
... | |
void Start() | |
{ | |
//信号を受信したときに、そのメッセージの処理を行う | |
serialHandler.OnDataReceived += OnDataReceived; | |
} | |
void Updata() | |
{ | |
//文字列を送信 | |
serialHandler.Write("hogehoge"); | |
} | |
//受信した信号(message)に対する処理 | |
void OnDataReceived(string message) | |
{ | |
var data = message.Split( | |
new string[]{"\t"}, System.StringSplitOptions.None); | |
if (data.Length < 2) return; | |
try { | |
... | |
} catch (System.Exception e) { | |
Debug.LogWarning(e.Message); | |
} | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
using System.IO.Ports; | |
using System.Threading; | |
public class SerialHandler : MonoBehaviour | |
{ | |
public delegate void SerialDataReceivedEventHandler(string message); | |
public event SerialDataReceivedEventHandler OnDataReceived; | |
//ポート名 | |
//例 | |
//Linuxでは/dev/ttyUSB0 | |
//windowsではCOM1 | |
//Macでは/dev/tty.usbmodem1421など | |
public string portName = "COM1"; | |
public int baudRate = 9600; | |
private SerialPort serialPort_; | |
private Thread thread_; | |
private bool isRunning_ = false; | |
private string message_; | |
private bool isNewMessageReceived_ = false; | |
void Awake() | |
{ | |
Open(); | |
} | |
void Update() | |
{ | |
if (isNewMessageReceived_) { | |
OnDataReceived(message_); | |
} | |
isNewMessageReceived_ = false; | |
} | |
void OnDestroy() | |
{ | |
Close(); | |
} | |
private void Open() | |
{ | |
serialPort_ = new SerialPort(portName, baudRate, Parity.None, 8, StopBits.One); | |
//または | |
//serialPort_ = new SerialPort(portName, baudRate); | |
serialPort_.Open(); | |
isRunning_ = true; | |
thread_ = new Thread(Read); | |
thread_.Start(); | |
} | |
private void Close() | |
{ | |
isNewMessageReceived_ = false; | |
isRunning_ = false; | |
if (thread_ != null && thread_.IsAlive) { | |
thread_.Join(); | |
} | |
if (serialPort_ != null && serialPort_.IsOpen) { | |
serialPort_.Close(); | |
serialPort_.Dispose(); | |
} | |
} | |
private void Read() | |
{ | |
while (isRunning_ && serialPort_ != null && serialPort_.IsOpen) { | |
try { | |
message_ = serialPort_.ReadLine(); | |
isNewMessageReceived_ = true; | |
} catch (System.Exception e) { | |
Debug.LogWarning(e.Message); | |
} | |
} | |
} | |
public void Write(string message) | |
{ | |
try { | |
serialPort_.Write(message); | |
} catch (System.Exception e) { | |
Debug.LogWarning(e.Message); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment