Skip to content

Instantly share code, notes, and snippets.

@yjiro0403
Last active November 11, 2016 09:15
Show Gist options
  • Save yjiro0403/1c87b91b6018886d6a2cbdff1de14ad7 to your computer and use it in GitHub Desktop.
Save yjiro0403/1c87b91b6018886d6a2cbdff1de14ad7 to your computer and use it in GitHub Desktop.
UnityとArduinoをシリアル通信 ref: http://qiita.com/yjiro0403/items/54e9518b5624c0030531
Serial.print(Signal1);
Serial.print("\t");
Serial.print(Signal2);
Serial.print("\t");
...
Serial.print(Signaln);
Serial.println();
if ( Serial.available() ) {
char mode = Serial.read();
switch (mode) {
case '0' : digitalWrite(13, LOW); break;
case '1' : digitalWrite(13, HIGH); break;
}
}
using System.IO.Ports;
using System.Threading; //スレッド通信
serialPort_ = new SerialPort(portName,baudRate, Parity.None, 8, StopBits.One);
IOException: アクセスが拒否されました
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);
}
}
}
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