UnitySerianHandloerのUnity2018で使えたやつ
SerialHandler.cs
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 = delegate { }; //ポート名 //例 //Linuxでは/dev/ttyUSB0 //windowsではCOM1 //Macでは/dev/tty.usbmodem1421など public string portName = "/dev/cu.usbmodem2101";//mac 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_.ReadTimeout = 20;//Add this 2019 serialPort_.Open(); serialPort_.NewLine = "\n";//Add this 2019 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); } } }
Unityボタン側スクリプト
Btn.cs
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class Btn : 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); } } /////ここから追加します /// /// /// /// public void sendOn() { //Arduinoに文字列onを送る serialHandler.Write("4"); } public void sendOff() { // serialHandler.Write("5"); } public void ledOn() { // serialHandler.Write("6"); } public void ledOff() { // serialHandler.Write("7"); } }
Arduino側コード
#include <Servo.h> const int LED_PIN = 13; int SERVO_SOCKET = 9; Servo servo; String InByte_strings; int InByte; void setup() { Serial.begin(9600); pinMode( LED_PIN, OUTPUT ); servo.attach( SERVO_SOCKET ); } void loop() { if(Serial.available()>0){ InByte_strings=Serial.readStringUntil(';'); InByte=InByte_strings.toInt(); Serial.println(InByte); switch (InByte){ case 4: servo.write( 90 ); break; case 5: servo.write( 180 ); break; case 6: digitalWrite(LED_PIN,HIGH); break; case 7: digitalWrite(LED_PIN,LOW); break; } } }